//---------------------------------------------------------------------- // revregoffset.s // Author: Bob Dondero //---------------------------------------------------------------------- .equ ARRAY_LENGTH, 5 //---------------------------------------------------------------------- .section .rodata promptStr: .string "Enter %d integers:\n" scanfFormatStr: .string "%ld" newLineStr: .string "\n" messageStr: .string "The integers in reverse order are:\n" printfFormatStr: .string "%ld\n" //---------------------------------------------------------------------- .section .data //---------------------------------------------------------------------- .section .bss alNums: .skip 8 * ARRAY_LENGTH lIndex: .skip 8 //---------------------------------------------------------------------- .section .text //-------------------------------------------------------------- // Read ARRAY_LENGTH integers from stdin, and write them in // reverse order to stdout. Return 0. //-------------------------------------------------------------- // Must be a multiple of 16 .equ MAIN_STACK_BYTECOUNT, 16 .global main main: // Prolog sub sp, sp, MAIN_STACK_BYTECOUNT str x30, [sp] // printf("Enter %d integers:\n", ARRAY_LENGTH) adr x0, promptStr mov w1, ARRAY_LENGTH bl printf // lIndex = 0 adr x0, lIndex str xzr, [x0] readLoop: // if (lIndex >= ARRAY_LENGTH) goto readLoopEnd adr x0, lIndex ldr x0, [x0] cmp x0, ARRAY_LENGTH bge readLoopEnd // scanf("%ld", &alNums[lIndex]) adr x0, scanfFormatStr adr x1, lIndex ldr x1, [x1] lsl x1, x1, 3 adr x2, alNums add x1, x1, x2 bl scanf // lIndex++ adr x0, lIndex ldr x1, [x0] add x1, x1, 1 str x1, [x0] // goto readLoop b readLoop readLoopEnd: // printf("\n") adr x0, newLineStr bl printf // printf("The integers in reverse order are:\n") adr x0, messageStr bl printf // lIndex = ARRAY_LENGTH-1 mov x0, ARRAY_LENGTH-1 adr x1, lIndex str x0, [x1] writeLoop: // if (lIndex < 0) goto writeLoopEnd adr x0, lIndex ldr x0, [x0] cmp x0, 0 blt writeLoopEnd // printf("%ld\n", alNums[lIndex]) adr x0, printfFormatStr adr x1, alNums adr x2, lIndex ldr x2, [x2] lsl x2, x2, 3 ldr x1, [x1, x2] // Register offset addressing bl printf // lIndex-- adr x0, lIndex ldr x1, [x0] sub x1, x1, 1 str x1, [x0] // goto writeLoop b writeLoop writeLoopEnd: // Epilog and return 0 mov w0, 0 ldr x30, [sp] add sp, sp, MAIN_STACK_BYTECOUNT ret .size main, (. - main)