15,748,049 members
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

## Solution 1

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
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.

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.

## Solution 2

Note, you don't have to use the multiplication. You know, the left shift...