% cat SubArgsEx.s .data x: .long 1 .long 5 .long 2 .long 18 .long 8888 .long 168 n: .long 3 sum: .long 0 .text # EAX will contain the number of loop iterations remaining # EBX will contain the sum # ECX will point to the current item to be summed .globl _start _start: # push the arguments before call: place to start summing, and # number of items to be summed push $x+4 # here we specify to start summing at the 5 push n # here we specify how many to sum call init pop %edx # don't use EAX, EBX or ECX here! top: addl (%ecx), %ebx addl $4, %ecx decl %eax jnz top done: movl %ebx, sum init: movl $0, %ebx # pick up arguments from the stack mov 8(%esp), %ecx mov 4(%esp), %eax ret % % as -a --gstabs -o s.o SubArgsEx.s ; ld s.o GAS LISTING SubArgsEx.s page 1 1 .data 2 x: 3 0000 01000000 .long 1 4 0004 05000000 .long 5 5 0008 02000000 .long 2 6 000c 12000000 .long 18 7 0010 B8220000 .long 8888 8 0014 A8000000 .long 168 9 0018 03000000 n: .long 3 10 sum: 11 001c 00000000 .long 0 12 13 .text 14 # EAX will contain the number of loop iterations # remaining 15 # EBX will contain the sum 16 # ECX will point to the current item to be summed 17 18 .globl _start 19 _start: 20 # push the arguments before call: place to # start summing, and 21 # number of items to be summed 22 0000 68040000 push $x+4 # here we specify to start # summing at the 5 22 00 23 0005 FF351800 push n # here we specify how many to sum 23 0000 24 000b E80F0000 call init 24 00 25 0010 5A pop %edx # don't use EAX, EBX or ECX here! 26 0011 0319 top: addl (%ecx), %ebx 27 0013 83C104 addl $4, %ecx 28 0016 48 decl %eax 29 0017 75F8 jnz top 30 0019 891D1C00 done: movl %ebx, sum 30 0000 31 32 init: 33 001f BB000000 movl $0, %ebx 33 00 34 # pick up arguments from the stack 35 0024 8B4C2408 mov 8(%esp), %ecx 36 0028 8B442404 mov 4(%esp), %eax 37 002c C3 ret DEFINED SYMBOLS SubArgsEx.s:2 .data:00000000 x SubArgsEx.s:9 .data:00000018 n SubArgsEx.s:10 .data:0000001c sum SubArgsEx.s:19 .text:00000000 _start SubArgsEx.s:32 .text:0000001f init SubArgsEx.s:26 .text:00000011 top SubArgsEx.s:30 .text:00000019 done NO UNDEFINED SYMBOLS % gdb a.out GNU gdb Red Hat Linux (5.2.1-4) Copyright 2002 Free Software Foundation, Inc. ... (gdb) b 24 Breakpoint 1 at 0x804807f: file SubArgsEx.s, line 24. (gdb) r Starting program: /www/matloff/public_html/50/PLN/a.out Breakpoint 1, _start () at SubArgsEx.s:24 24 call init Current language: auto; currently asm (gdb) p/x $eip $1 = 0x804807f (gdb) p/x $esp $2 = 0xbfffcec8 (gdb) si 33 movl $0, %ebx (gdb) p/x $eip $3 = 0x8048093 (gdb) p/x $esp $4 = 0xbfffcec4 (gdb) x/3w $esp 0xbfffcec4: 0x08048084 0x00000003 0x080490a8 (gdb) p/x &x $5 = 0x80490a4 (gdb) b done Breakpoint 2 at 0x804808d: file SubArgsEx.s, line 30. (gdb) c Continuing. Breakpoint 2, done () at SubArgsEx.s:30 30 done: movl %ebx, sum (gdb) si 33 movl $0, %ebx (gdb) p sum $6 = 25 (gdb) si 35 mov 8(%esp), %ecx (gdb) q The program is running. Exit anyway? (y or n) y %