## ============================================================ ## sumarraylea.s ## The lea Instruction ## ============================================================ .equ ARRAYSIZE, 100 ## ============================================================ .section ".rodata" ## ============================================================ pcPrompt: .asciz "How many integers? " pcScanfFormat: .asciz "%d" pcResult: .asciz "The sum is %d.\n" ## ============================================================ .section ".data" ## ============================================================ ## ============================================================ .section ".bss" ## ============================================================ piNumbers: .skip 4 * ARRAYSIZE piIndex: .skip 4 piCount: .skip 4 piSum: .skip 4 ## ============================================================ .section ".text" ## ============================================================ ## ------------------------------------------------------------ ## int main(int argc, char *argv[]) ## ## Read up to ARRAYSIZE integers from stdin, and write to ## stdout the sum of those integers. ## ## Formal parameter offsets: .equ ARGC, 8 .equ ARGV, 12 ## ------------------------------------------------------------ .globl main .type main,@function main: pushl %ebp movl %esp, %ebp ## iIndex = 0; movl $0, piIndex ## printf("How many integers? "); pushl $pcPrompt call printf addl $4, %esp ## scanf("%d", &iCount); pushl $piCount pushl $pcScanfFormat call scanf addl $8, %esp loop1: ## if (iIndex >= iCount) goto loopend1; movl piIndex, %eax cmpl piCount, %eax jge loopend1 ## scanf("%d", &piNumbers[iIndex]); movl piIndex, %eax leal piNumbers(,%eax,4), %eax # the lea instruction pushl %eax pushl $pcScanfFormat call scanf addl $8, %esp ## iIndex++; incl piIndex ## goto loop1; jmp loop1 loopend1: ## iSum = 0; movl $0, piSum ## iIndex = 0; movl $0, piIndex loop2: ## if (iIndex >= iCount) goto loopend2; movl piIndex, %eax cmpl piCount, %eax jge loopend2 ## iSum += piNumbers[iIndex]; movl piIndex, %eax movl piNumbers(,%eax,4), %eax addl %eax, piSum ## iIndex++; incl piIndex ## goto loop2; jmp loop2 loopend2: ## printf("The sum is %d.\n", iSum); pushl piSum pushl $pcResult call printf addl $8, %esp ## return 0; movl $0, %eax movl %ebp, %esp popl %ebp ret