# # File: array4_mod.s # Author: William T Krieger, Oct 2003 # My modification of array4 test. # Demonstrates looping through the values in an array. .data # data section: global vars .align 32 .globl x # int x[10] = { 1, 2, 3, 4, 5 } ; x: .long 1 .long 2 .long 3 .long 4 .long 5 .space 20 .text FMT0: .ascii "x[%d] = %d\12\0" FMT1: .ascii "Done.\0" .align 4 # # Function: _main() # void _main() # Loop through the values in an array and print them out. .globl _main _main: pushl %ebp # enter _main() movl %esp, %ebp subl $4,%esp # adjust stack for local var: # int i = -4(%ebp) movl $0,-4(%ebp) # i = 0 LOOP_TOP: cmpl $9,-4(%ebp) # compare( 9, i) jg LOOP_TAIL # if i > 9, jump LOOP_TAIL # place the value of x[i] into %eax movl -4(%ebp),%edx # %edx = i leal 0(,%edx,4),%eax # %eax = 4 * i movl $x,%edx # %edx = &x movl (%eax,%edx),%eax # %eax = x[%edx] pushl %eax # call printf( fmt0, i, x[i]) movl -4(%ebp),%eax pushl %eax pushl $FMT0 call _printf addl $12,%esp incl -4(%ebp) # i++ jmp LOOP_TOP # .p2align 4,,7 LOOP_TAIL: pushl $FMT1 # call printf( fmt1) call _printf addl $4,%esp movl %ebp, %esp # exit _main() pop %ebp ret