user3009956
user3009956

Reputation: 11

How to convert instruction from IA32 to Y86

I have this IA32 assembly code that I'm trying to convert into Y86 assembly code:

bubble_a:
    pushl   %ebp
    movl    %esp, %ebp
    pushl   %edi
    pushl   %esi
    pushl   %ebx
    movl    8(%ebp), %edx
    movl    12(%ebp), %edi
    subl    $1, %edi
    testl   %edi, %edi
    jg  .L11
    jmp .L8
.L9:
    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    %edi, %eax
    jne .L9
.L7:
    subl    $1, %edi
    je  .L8
.L11:
    movl    $0, %eax
    testl   %edi, %edi
    jg  .L9
    jmp .L7
.L8:
    popl    %ebx
    popl    %esi
    popl    %edi
    popl    %ebp
    ret

I'm struggling to translate the movl instructions in .L9

movl    4(%edx,%eax,4), %ecx
movl    (%edx,%eax,4), %ebx
movl    %ebx, 4(%edx,%eax,4)
movl    %ecx, (%edx,%eax,4)

I know there's not a single instruction that will do this, but I can't figure out what set of instructions will replace those in Y86.

Thank you for any help.

Upvotes: 1

Views: 4380

Answers (1)

John Ding
John Ding

Reputation: 73

As you have already known, Y86 doesn't have the flexible and complicated addressing schemes. So your job is to use multiple instructions to simulate the calculation done in the x86 instructions. Moreover, understanding the meaning of the assembly code will reduce your work.
Let's look the four lines of code you singled out

movl    4(%edx,%eax,4), %ecx  
movl    (%edx,%eax,4), %ebx  
movl    %ebx, 4(%edx,%eax,4)  
movl    %ecx, (%edx,%eax,4)  

The main job it does is to swap the two numbers stored at address R[%edx]+R[%eax]*4 and R[%edx]+R[%eax]*4+4. To calculate these addresses, we need to add them up ourselves. Here is a way that it can be translated into Y86:

rrmovl %eax, %esi  
addl %esi, %esi  
addl %esi, %esi  
addl %edx, %esi  
mrmovl 4(%esi), %ecx  
mrmovl (%esi), %ebx  
rmmovl %ebx, 4(%esi)  
rmmovl %ecx, (%esi)  

The first four lines calculate the value R[%edx]+R[%eax]*4 (first double twice and then plus), stored in %esi, and the following is quite simple.

Upvotes: 1

Related Questions