/******************************************************* * File: ArrayEx1.s * Date: Oct. 11, 2021 * Author: DSS * Computer: KUNET suns * Execute: ArrayEx1 * Purpose: to demonstrate the use of arrays as allocated on the stack. * It illustrates how components of the arrays are referenced * efficiently and demonstrates offsets via an index search * * This program will define an integer array (size 10) stored in the stack * and use a for loop to read in *up to* ten values and print them out. * * It uses minimal code, optimizing several things *******************************************************/ .global main main: push %rbp # keep stack aligned # Make room for 10 ints on the stack subq $48, %rsp # must also remain divisible by 16 movq $0, %r14 # Count # elts loop: # Read a value leaq prompt(%rip), %rdi # load format string to proper register call printf # print it # Read the int leaq fmt_in(%rip), %rdi # load format string leaq (%rsp,%r14,4), %rsi # set storage to address of elt on stack call scanf inc %r14 # Counter up cmp $10,%r14 # If equal, time to exit jz print # Did they enter ^D (eotext)? scanf returns #items read, -1 on ^D cmp $1,%rax je loop # If it was 1, go get next value dec %r14 movq $0, %r15 # #elts is in %r14, %r15 is now counting print: leaq frmto(%rip),%rdi # Output format mov %r15,%rsi mov (%rsp,%r15,4),%rdx # Load value to print xor %rax,%rax call printf # Are we done? inc %r15 # Inc counter cmp %r14,%r15 # Equal to # elts? jl print # No? Print next value done: addq $48, %rsp # Deallocate the array (not critical) pop %rbp ret .data prompt: .asciz "enter up to ten numbers (^D quits): " fmt_in: .asciz "%d" frmto: .asciz "\narray[%d] = %d"