In: Computer Science
Write a program in MIPS assembly language to perform the calculation of the following equation and save the result accordingly:
f = 5x + 3y + z
Assumptions:
- Registers can be used to represent variables x, y, z, and f
- Initialize x, y, and z to values of your choice. f can be initialized to zero.
- Use comments to specify your register usage and explain your logic
In this program , we have to perform the operation f = 5x + 3y + z
First, we need to load x, y and z into registers. Let them be loaded into $s1, $s2 and $s3 respectively
then, we will multiply $s1 with 5 to get the value of 5x.
Then, multiply $s2 with 3 to get the value of 3y
add $s1, $s2 and $s3 and store it in $s0. $s0 will now store the value of 5x + 3y + z
store the value in $s0 to f
Since, x ,y and z are to initialized with any integers, I will assign x = 1 , y = 2 and z = 3
program:
.data
x : .word 1
y : .word 2
z : .word 3
f : .word 0
.text
.globl main
main:
lw $s1, x
lw $s2, y
lw $s3, z #s3 = z
mul $s1, $s1, 5 # $s1 = 5x
mul $s2, $s2, 3 # $s2 = 3x
add $s0, $s1, $s2 # $s0 = 5x + 3y
add $s0, $s0, $s3 # s0 = 5x + 3y + z
la $t0, f #$t0 = &f
sw $s0, ($t0) # f = 5x + 3y + z