/* hand-coded function to compute sum of array of numbers */ .section .rodata STR1: .string "answer is %d\n" .text .globl main /* this is needed! */ main: push %ebp mov %esp, %ebp sub $16, %esp /* allocate some space */ and $-16, %esp pushl $40 call malloc add $4, %esp mov %eax, %edx /* edx is going to be base register */ /* fill array with numbers 100 to 1000 */ mov $100, %eax mov $0, %edi /* edi will be index register */ loop: cmp $10, %edi je loopexit mov %eax, (%edx,%edi,4) add $100, %eax inc %edi jmp loop loopexit: /* now sum backwards, using -4(ebp) to hold value */ movl $0, -4(%ebp) loop2: dec %edi /* this will also modify flags */ /* cmp $-1, %edi */ jz exit2 mov (%edx,%edi,4), %eax add %eax, -4(%ebp) /* add to accumulator */ jmp loop2 exit2: pushl -4(%ebp) /* surprised this is valid (mem-mem op) */ pushl $STR1 /* 1st arg to printf - why pushl? */ call printf add $8, %esp mov $0, %eax /* return 0 */ mov %ebp, %esp pop %ebp ret