# Execution begins at address 0 .pos 0 init: irmovl Stack, %esp # Set up Stack pointer irmovl Stack, %ebp # Set up base pointer jmp Main # Execute main program # Array of 4 elements .align 4 array: .long 0xd .long 0xc0 .long 0xb00 .long 0xa000 Main: irmovl $4,%eax # length of array pushl %eax # Push length of array irmovl array,%edx # put address of array in edx pushl %edx # Push address of array call Sum # Sum(array, 4) halt # C code for sum subroutine: # # int Sum(int *Start, int Count) { # int tot=0; # while (Count>0) { # sum += *Start # Start++; # Count--; # } # return(tot); # } Sum: pushl %ebp # stack frame setup rrmovl %esp,%ebp # mrmovl 8(%ebp),%ecx # ecx = Start mrmovl 12(%ebp),%edx # edx = Count irmovl $0, %eax # tot = 0 (eax) andl %edx,%edx # je End # jump if edx == 0 Loop: mrmovl (%ecx),%esi # get *Start in esi addl %esi,%eax # add to tot irmovl $4,%ebx # addl %ebx,%ecx # Start++ irmovl $-1,%ebx # addl %ebx,%edx # Count-- jne Loop # Stop when edx is 0 End: popl %ebp # restore stack frame ret .pos 0x100 Stack: # The stack goes here