Implementing a Recursive Procedure with IA32 and Y86 Assembly Code

For a recursive procedure based on  the following C code,
int rSum(int *start, int count)
{
    if (count <= 0)
        return 0;
    return *start + rSum(start+1, count-1);
}
IA32 and Y86 assembly code is written to implement it. The two fragments of code are presented below.
#IA32 assembly code to implement rSum    #Y86 assembly code to implement rSum
rSum:                                    rSum:
   pushl  %ebp                              pushl   %ebp
   movl   %esp, %ebp                        rrmovl  %esp, %ebp 
   subl   $8, %esp                          pushl   %ebx
   movl   8(%ebp), %ecx                     mrmovl  8(%ebp), %ebx
   movl   12(%ebp), %edx                    mrmovl  12(%ebp), %eax
   xorl   %eax, %eax       
   cmpl   $0, %edx                          andl    %eax, %eax
   jle    Done                              jle     L1
   leal   -1(%edx), %eax                    irmovl  $-1, %edx
   movl   %eax, 4(%esp)                     addl    %edx, %eax
                                            pushl   %eax
   leal   4(%ecx), %eax                     irmovl  $4, %edx
   movl   %eax, (%esp)                      rrmovl  %ebx, %eax
                                            addl    %edx, %eax
                                            pushl   %eax
   call   rSum                              call    rSum                                            
   addl   (%ecx), %eax                      mrmovl  (%ebx), %edx
                                            addl    %edx, %eax
                                            jmp     Done
                                         L1:
                                            xorl    %eax, %eax   
Done:                                    Done:
   addl   $8, %esp                          mrmovl  -4(%ebp), %ebx
                                            rrmovl  %ebp, %esp
   popl   %ebp                              popl    %ebp
   ret                                      ret

     According to the comparison of IA32 and Y86 assembly programs, four distinctions are found and it is concluded:
     1. Besides explicitly decreasing and increasing the stack pointer, pushl and popl can be utilized to both modify the stack pointer and to save and store the register state.
     2. %eax, %ecx and %edx are caller-saved; %ebx, %edi and %esi are callee-saved.
     3. Y86 integer operation instructions operate only on register data, whereas IA32 also allows operations on memory data.
     4. In the case of using pushl to both modify the stack pointer and save the register data and, popl is unnecessary to restore the state.

좋은 웹페이지 즐겨찾기