# # File: for1_mod.s # Author: William T Krieger, Oct 2003 # # My modification of the for1 test case demonstrating a simple for loop. .text FMT0: .ascii "Enter summation number: \0" FMT1: .ascii "%d\0" FMT2: .ascii "Summation is %d\12\0" # # Function: _main() # void _main() # Demonstrates a simple for loop. The function queries the user for # a number (N) and then prints the summation of 1 to N. .globl _main _main: pushl %ebp # enter _main() movl %esp, %ebp subl $12, %esp # adjust stack for 3 local vars # num = -4(%ebp) # sum = -8(%ebp) # i = -12(%ebp) movl $0,-8(%ebp) # sum = 0 pushl $FMT0 # call printf( fmt0) call _printf addl $4,%esp leal -4(%ebp),%eax # call scanf( fmt1, &num) pushl %eax pushl $FMT1 call _scanf addl $8,%esp movl -4(%ebp),%eax # eax = num movl %eax,-12(%ebp) # i = eax LOOP1_HEAD: cmpl $0,-12(%ebp) # compare( 0, i) jg LOOP1_BODY # if > then jump LOOP1_BODY jmp LOOP1_TAIL # else jump LOOP1_TAIL LOOP1_BODY: movl -12(%ebp),%eax # eax = i addl %eax,-8(%ebp) # sum = sum + eax decl -12(%ebp) # i-- jmp LOOP1_HEAD LOOP1_TAIL: movl -8(%ebp),%eax # call printf( fmt2, sum) pushl %eax pushl $FMT2 call _printf addl $8,%esp movl %ebp, %esp # exit _main() pop %ebp ret