previous | start | next

IA32 Assembler Code Generated for the Example

_sumFirst:
        pushl   %ebp                ; Set up the stack for
        movl    %esp, %ebp          ; the local variables 
        subl    $8, %esp            ; of the sumFirst function

        ans = 0;
        movl    $0, -4(%ebp)        ; -4(%ebp) is ans
        i = 1;
        movl    $1, -8(%ebp)        ; -8(%ebp) is i
L2:     i <= n
        movl    -8(%ebp), %eax      ; copy i value from memory into register %eax
        cmpl    8(%ebp), %eax       ; 8(%ebp) is n; compare n and i
        jg      L3                  ; jump to end of for loop (location L3) if n > i
        
        ans += i;
        movl    -8(%ebp), %eax      ; copy i to %eax
        leal    -4(%ebp), %edx      ; copy address of ans into %edx
        addl    %eax, (%edx)        ; add i to ans
        
        i++
        leal    -8(%ebp), %eax      ; load address of i in %eax
        incl    (%eax)              ; increment i
        jmp     L2                  ; jump back to label L2 for loop beginning

L3:     return ans;
        movl    -4(%ebp), %eax      ; copy return value, ans, into register %eax
        leave                       ; pop local variables from the stack
        ret                         ; and return to the instruction 
                                    ; after the call to sumFirst
     


previous | start | next