Translating from IA-32 to Y86.. What the heck!

IA-32 is great… Almost as great as Y86.

Recently, students at a university were challenged to translate an IA-32 program to Y86. To their horror, they discovered that Y86 has no CMP instructions, which was a problem since the IA-32 program contained:

   cmpl %ebx, %ecx

The IA-32 CMPL instruction compares two values (operand1 and operand2) by subtracting operand1 from operand2, but! critically does not store the result, only changes the flags. CMPL is typically executed in conjunction with conditional jumps based on the condition code (cc.)

Y86 does not have a CMPL instruction. However, it does have has SUBL, PUSHL and POPL instructions.

So cmpl %ebx, %ecx can be converted to the following Y86 code:

   pushl %ecx
   subl  %ebx, %ecx
   popl  %ecx

AI-32’s CMPL instructionis exactly the same as Y86’s SUBL instruction, with the difference that CMPL does not store the result, it only updates the flags. So CMPL can always be replaced with the PUSHL, SUBL, POPL combination shown above, which preserves the register.

P.S. As always, make sure that there is enough space in the stack!