Click here to Skip to main content
15,887,368 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Find the equivalent MIPS assembly code for the C program as follows:

x = 5
y = x - 2
a = x * 4
b = y * 2
z = (x + a) - (y + b)
Knowing that the variables x, y, a, b are 32-bit integers

Thanks

What I have tried:

This is the command I used in the first 2 lines:

addi $t0,$zero,5 

addi $t1,$t0,-2

li $t2,4

But the third line I don't know which instruction to use to do the multiplication with the instantaneous constant
Posted
Updated 15-Sep-21 5:23am

 
Share this answer
 
Comments
ubutu2334 15-Sep-21 11:27am    
I only see the instruction of multiplying 2 registers together. This statement: a = x * 4 is multiplied by the instantaneous constant.
jeron1 15-Sep-21 11:36am    
Well put the 4 in a different register then multiply or better still take advantage of the fact that it is a 2^n quantity and shift left by n as CPallini suggested.
ubutu2334 15-Sep-21 11:49am    
I tried your way
addi $t0,$zero,5 # x = 5
addi $t1,$t0,-2 # y = x - 2
li $t2,4 # t2 = 4
mult $t0, $t2 # x*4
mflo $s3 # a = x*4
li $t3,2 # t3 = 2
mult $t1,$t3 # y*2
mflo $s4 # b = y*2
add $t5, $t0, $s3 # temp1 = x + a
add $t6, $t1, $s4 # temp2 = y + b
sub $s5, $t5, $t6 # z = (x + a) - (y + b)
Is that correct?
jeron1 15-Sep-21 12:12pm    
Your addi generally should have 2 operands, the register and the immediate value.
addi $t0,5

I most often use mul for multiply so I don't have to deal with HI and LO.
mul $t0,$t2 //multiple $t0 and $t2 and store the result in $t0
In this case though a shift left would do the job,
sll $t0,2 // effectively multiplies $t0 by 4

Also, at some point you'll need to deal with signed versus unsigned operations.
Richard MacCutchan 15-Sep-21 11:59am    
Yes, but you can only use the instructions that exist.
Note, you don't have to use the multiplication. You know, the left shift...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900