# MIPS Assembly Language Program in SPIM To Perform Some Math Operations Assignment Solution

July 02, 2024
Rehana Magnus
Assembly Language
Rehana Magnus, PhD in Computer Science from the esteemed Acadia Institute of Technology, Canada. With 6 years of experience, specializes in assembly language programming. Proficient in low-level coding, optimizing performance, and enhancing system functionality.
Key Topics
• Instructions
• Objective
• Requirements and Specifications
Tip of the day
News

## Instructions

### Objective

Write a MIPS assignment in SPIM to perform some math operations.

## Requirements and Specifications

Write a SPIM program to calculate 2 * ( 111 + 333) - 12345.

Use R8, R9, and R10 to store the values 10, 20, and 15 respectively. Store the result in R12. Run the program and submit the screenshot (showing program code and register values).

Write a SPIM program to "prompt the user to enter" a radius of a circle (integer only), and to display the diameter (use add - do not use MUL). The radius and the diameter can be stored in the registers.

Write a SPIM program to prompt the user to enter two numbers x and y (integers only), and to display their sum z, z = x + y. All x, y, and z can be stored in the registers. Register Mapping: x: \$t0 , y: \$t1, z: \$t2 (Use RAM to store those value).

Screenshots of output

Source Code

Program 1

```.text main: li \$8, 10 # R8 = 10 li \$9, 20 # R9 = 20 li \$10, 15 # R10 = 15 li \$12, 111 # R12 = 111 addi \$12, \$12, 333 # R12 = 111 + 333 sll \$12, \$12, 1 # R12 = 2* (111 + 333) li \$11, 12345 # R11 = 12345 sub \$12, \$12, \$11 # R12 = 2* (111 + 333) - 12345 li \$2, 10 # syscall to exit syscall # exit program ```

Program 2

```.data prompt: .asciiz "Circle radius? = " result: .asciiz "Diameter = " .text la \$a0, prompt # load address of prompt string li \$v0, 4 # load syscall to print a string syscall # print the prompt li \$v0, 5 # load syscall to read an integer syscall # read integer in \$v0 add \$t0, \$v0, \$v0 # diameter = 2*radius = radius + radius la \$a0, result # load address of result string li \$v0, 4 # load syscall to print a string syscall # print the result message li \$v0, 1 # load syscall to print an integer move \$a0, \$t0 # copy result value to be printed syscall # print integer li \$v0, 10 # syscall to exit syscall # exit program ```

Program 3

```.data promptx: .asciiz "x value? = " prompty: .asciiz "y value? = " result: .asciiz "x + y = " .text la \$a0, promptx # load address of prompt string li \$v0, 4 # load syscall to print a string syscall # print the prompt li \$v0, 5 # load syscall to read an integer syscall # read integer in \$v0 move \$t0, \$v0 # save x in \$t0 la \$a0, prompty # load address of prompt string li \$v0, 4 # load syscall to print a string syscall # print the prompt li \$v0, 5 # load syscall to read an integer syscall # read integer in \$v0 move \$t1, \$v0 # save y in \$t1 add \$t2, \$t0, \$t1 # add z = x + y la \$a0, result # load address of result string li \$v0, 4 # load syscall to print a string syscall # print the result message li \$v0, 1 # load syscall to print an integer move \$a0, \$t2 # copy result value to be printed syscall # print integer li \$v0, 10 # syscall to exit syscall # exit program ```

Program 4

```.data prompt1: .asciiz "num1 value? = " prompt2: .asciiz "num2 value? = " result1: .asciiz "swapped num1 = " result2: .asciiz "\nswapped num2 = " num1: .word 0 num2: .word 0 .text la \$a0, prompt1 # load address of prompt string li \$v0, 4 # load syscall to print a string syscall # print the prompt li \$v0, 5 # load syscall to read an integer syscall # read integer in \$v0 la \$t0, num1 # load address of variable num1 sw \$v0, 0(\$t0) # save read number in num1 la \$a0, prompt2 # load address of prompt string li \$v0, 4 # load syscall to print a string syscall # print the prompt li \$v0, 5 # load syscall to read an integer syscall # read integer in \$v0 la \$t0, num2 # load address of variable num2 sw \$v0, 0(\$t0) # save read number in num2 la \$t0, num1 # load address of variable num1 la \$t1, num2 # load address of variable num2 lw \$t2, 0(\$t0) # load num1 lw \$t3, 0(\$t1) # load num2 sw \$t2, 0(\$t1) # swap num1 to num2 sw \$t3, 0(\$t0) # swap num2 to num1 la \$a0, result1 # load address of result string li \$v0, 4 # load syscall to print a string syscall # print the result message li \$v0, 1 # load syscall to print an integer la \$t0, num1 # load address of variable num1 lw \$a0, 0(\$t0) # load num1 value to be printed syscall # print integer la \$a0, result2 # load address of result string li \$v0, 4 # load syscall to print a string syscall # print the result message li \$v0, 1 # load syscall to print an integer la \$t0, num2 # load address of variable num2 lw \$a0, 0(\$t0) # load num2 value to be printed syscall # print integer li \$v0, 10 # syscall to exit syscall # exit program ```

## Similar Samples

Discover our portfolio of programming homework samples at ProgrammingHomeworkHelp.com. From Java to Python, Machine Learning to Web Development, explore examples showcasing our expertise and commitment to excellence. Each sample demonstrates our proficiency in delivering tailored solutions for programming assignments. Explore and experience our quality service firsthand