In: Computer Science
Write a program 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
Greetings!!
Code:
.data
x: .word 5
y: .word 3
z: .word 1
f: .word 0
.text
main:
lw $t0,x #load x to t0
mul $t0,$t0,5 #calculate 5x
lw $t1,y #load y to t1
mul $t1,$t1,3 #calculate 3y
add $t1,$t0,$t1 #calculate 5x+3y
lw $t2,z #load z to t2
mul $t2,$t2,1 #calculate z
add $t2,$t2,$t1 #calculate 5x+3y+z
sw $t2,f #store the result to f
addi $v0,$0,10 #termination
syscall
Output screenshot:
For x=5,y=3 and z=1
f=35(or 23H)
Hope this helps