Novak007
Novak007

Reputation: 436

Convert 3 Address codes in Assembly

I am trying to convert 3 Address codes in ASSEMBLY code [Code Generation].

Consider,

enter image description here

The Assembly code sequence is:

enter image description here

If order is changed to t2 t3 t1 t4, then

enter image description here

  1. ADD is done with Memory operand + Register operand but SUB is never done with Memory operand. Similarly, I have seen that MUL is also never done with Memory operand. Is there some rule like this ?

  2. Why MOV R0,t1 is used? Isn't it better to use one more Register and keep using R0?

  3. Bring t1 down, t2-t3-t1-t4 is the new sequence and we save instructions i.e. we can use the Reg which keeps the value of t1 immediately in the next instruction. But to use a Register after the immediate instruction we have to store it in memory?

Upvotes: 1

Views: 1703

Answers (1)

Mike
Mike

Reputation: 2761

Typically, operands have to involve at least one register so you cannot, for example, subtract t1 from t2 directly. Therefore, you have to move op1 to a register and apply the operation to op2 - the result going into the register. In this case, e-t1 cannot be used as a register (it's on the wrong side of the operand) whereas t1-e could.

One alternative would be to negate and add t1 which could then be used without resorting to memory. So:

mov   a, R0
add   b, R0           ; t1 (R0) := a+b
mov   c, R1
add   d, R1           ; t2 (R1) := c+d
neg   R1
add   e, R1           ; t3 (R1) := e+(-t2)
neg   R1
add   R0, R1          ; t4 (R1) := t1+(-t3)

Upvotes: 1

Related Questions