# copyright -- Ivan Novick -- Oct-2007 -- ivan at 0x4849 dot net # x86 assembly -- sum a set of of numbers entered by the user # # as -o sumloop.o sumloop.s # ld -dynamic-linker /lib/ld-linux.so.2 -o sumloop -lc sumloop.o ############################################# .section .data x: .long 0 ############################################# ############################################# .section .text fmtstring: .asciz "%d" sumstr: .asciz "The sum is: %ld.\n" cntstr: .asciz "Number of integers input was: %d.\n" prompt: .asciz "enter integer (0 for end of input): " errormsg1: .asciz "No valid integers were added\n" ############################################# .globl _start _start: # initialize the sum to 0 movl $0, %eax # initialize the counter to -1, it will increment for successful entries movl $-1, %edx nextinput: # printf the input prompt pushl %edx pushl %eax pushl $prompt call printf addl $4, %esp popl %eax popl %edx # call scanf pushl %edx pushl %eax pushl $x pushl $fmtstring call scanf addl $8, %esp popl %eax popl %edx # add sum addl x, %eax # add 1 to counter of integers addl $1, %edx # if the number input was not 0, loop movl $0, %ecx movl x, %ebx cmp %ebx, %ecx jne nextinput # check for counter of 0 movl $0, %ecx cmp %edx, %ecx je novalidinput # print out the final sum pushl %edx pushl %eax pushl $sumstr call printf addl $4, %esp popl %eax popl %edx # print out the number of integers pushl %edx pushl $cntstr call printf addl $4, %esp popl %edx jmp theend novalidinput: pushl $errormsg1 call printf addl $4, %esp theend: # exit system call movl $1, %eax movl $0, %ebx int $0x80