/******************************************************* * File: arr_Ex1.m * Dir: cis235/suns/ch05 * Date: October 20, 2002 * Author: DSS * Computer: KUNET suns * Assembler: as under the gcc compiler * Compile: gcc * Execute: arr_Ex1 * Purpose: to demonstrate the use of arrays as * automatic variables. It illustrates * how components of the arrays are * referenced. * * This program will define an integer * array int A[10] and use a for loop to * read in ten values and print them out. *******************************************************/ ! Use macros to make things clear!!! define(iRegister,%l0) ! iRegister: Used to hold lcv at times define(lcv,%l1) ! lcv: loop control variable define(adrsOfA,%l2) ! register that will hold A's address define(eltOffset,%l3) ! address from which array element is accessed define(eltAdrs,%o1) define(i, -20) ! i - loop control variable; offset from %fp define(A, -60) ! the array A - starting address .data .align 8 prompt: .asciz "enter ten numbers: " .align 8 frmti: .asciz "%d" .align 8 frmto: .asciz "The value is %d\n" .align 4 .text .global main main: save %sp, (-108 - 44) & -8, %sp ! printf(prompt); sethi %hi(prompt), %o0 call printf, 0 or %o0, %lo(prompt), %o0 ! !********************************************** ! for(i = 0;1 < 10; i++) ! scanf(frmti, &A[i]); mov 0, iRegister ! i = 0; st iRegister, [%fp + i] loop1: ! scanf(frmti, &A[i]) != 1 add %fp, A, adrsOfA ! the address of A: offset from %fp ! can't do it from %sp ld [%fp+i], lcv ! load loop control variable sll lcv, 2, eltOffset ! 4 * i - int is 4 bytes; take 4 * lcv add adrsOfA, eltOffset, eltAdrs ! the address of A[i] sethi %hi(frmti), %o0 ! 1st half of set call scanf, 0 or %o0, %lo(frmti), %o0 ! 2nd half of set nop ! i++ ; i < 10? ld [%fp+i], iRegister inc iRegister ! iRegister++ st iRegister, [%fp+i] cmp iRegister, 10 bl loop1 nop !********************************************** ! for(i = 0;1 < 10; i++) ! printf(frmto, A[i]); mov 0, iRegister ! i = 0; st iRegister, [%fp + i] loop2: ! printf(frmto, A[i]) != 1 sethi %hi(frmto), %o0 or %o0, %lo(frmto), %o0 add %fp, A, adrsOfA ! the address of A ld [%fp+i], iRegister ! load iRegister sll iRegister, 2, iRegister ! iRegister *= 4 add adrsOfA, iRegister, eltAdrs ! the address of A[i] ld [eltAdrs], %o1 ! the value of A[i] call printf, 0 nop ! i++ ; i < 10? ld [%fp+i], iRegister inc iRegister st iRegister, [%fp+i] cmp iRegister, 10 bl loop2 nop call exit,0 mov 0, %o0