Code Generation: Code Generation I12-02-code-generation-i.mp412-02-q13
vertLeft1
112-02-q1.pngRemember that cgen(<constant>) is equivalent to “li $a0 <constant>”. Also remember that cgen(e1 + e2) and cgen(e1 - e2) both use templates of the form:
cgen(e1)
sw $a0 0($sp)
addiu $sp $sp -4
cgen(e2)
lw $t1 4($sp)
[add or sub] $a0 $t1 $a0
addiu $sp $sp 4
Look for occurrences of these templates in the assembly code and work backwards (by substituting cgen(e1 + e2) or cgen(e1 - e2)) to determine the correct expression. Note that although options 1 and 3 both evaluate to the same value (6), option 1 translates to the given assembly code, not option 3.