How to convert IA32 'cmp' instruction to Y86?

amorimluc picture amorimluc · Feb 26, 2013 · Viewed 7.2k times · Source

IA32 to Y86

ATT Assembly

I have the following IA32 assembly code:

Bubble:
.LFB0:
    pushl   %esi
    pushl   %ebx
    movl    16(%esp), %esi
    movl    12(%esp), %edx
    subl    $1, %esi
    andl    %esi, %esi
    jle .L1
.L7:
    xorl    %eax, %eax
.L5:
    movl    4(%edx,%eax,4), %ecx
    movl    (%edx,%eax,4), %ebx
    cmpl    %ebx, %ecx
    jge .L4
    movl    %ebx, 4(%edx,%eax,4)
    movl    %ecx, (%edx,%eax,4)
.L4:  
    addl    $1, %eax
    cmpl    %eax, %esi
    jg  .L5
    subl    $1, %esi
    jne .L7
.L1: 
    popl    %ebx
    popl    %esi
    ret

I'm trying to convert it to Y86 assembly code. I'm having trouble translating the compare instruction:

 cmpl    %ebx, %ecx

Thanks.

Answer

nrz picture nrz · Feb 26, 2013

It seems that Y86 does not have cmp instruction. However, it has sub, push and pop.

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

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

cmp is exactly the same as sub, with the difference that cmp does not store the result, it only updates the flags. So cmp can always be replaced with push, sub, pop (if there's enough space in the stack).