Finish writing following aarch64 assembly language program .section .rodata getnstr:…
- User
Finish writing following aarch64 assembly language program .section .rodata getnstr: .string "Enter a value of n: " .align 3 intstr: .string "%d" .align 3 prntstr: .string "The array values are: \n" .align 3 tab10dintstr: .string "\t%10d\n" .align 3 nlstr: .string "\n" .align 3
.section .bss n: .skip 4 n16: .skip 4
.section .text .global main .type main, @function
main: stp x29, x30, [sp, #-16]! // main prolog
// seed the random bl time bl srand //print and get the array size // printf (getnstr) ldr x0, =getnstr bl printf // scanf(intstr, &n) ldr x0, =intstr ldr x1, =n // memory location of n bl scanf // compute next highest multiple of 16 >= n. // SP has to be multiples of 16 ldr x1, =n ldr w1, [x1] sbfiz x1, x1, #2, #20 add x1, x1, #0xf // 0xf = 15 and x1, x1, #0xfffffffffffffff0 //15f's ldr x2, =n16 str w1, [x2] // create the storage for length of n array, using stack pointer. sub sp, sp, x1 // call init_array bl init_array // return the local array back to the stack ldr x1, =n16 ldr x1, [x1] add sp, sp, x1 ldp x29, x30, [sp], #16 // main epilog mov x0, #0 ret.type init_array, @function init_array: stp x29, x30, [sp, #-16]! // function prolog
mov x21, #0 // loop counter ldr x20, =n ldr w20, [x20] // todo: loop n times. call rand function and store it in array using stack pointer. Remember we pre-created the storage for array earlier.endloop1: ldp x29, x30, [sp], #16 ret
- Assistant
ldr x1, =sp // load the address of stack pointer to x1 add x1, x1, x21, LSL #2 // calculate the memory location for the current array element bl rand // call the rand function str w0, [x1] // store the result in the array add x21, x21, #1 // increment the loop counter cmp x21, w20 // compare the counter with the array size blt endloop1 // continue the loop if counter is less than array size
//print the array values ldr x1, =prntstr bl printf mov x21, #0 // reset loop counterendloop2: ldr x1, =tab10dintstr ldp x29, x30, [sp, #-16]! ldr x0, [sp, #16] add sp, sp, #16 bl printf // print the current array element add x21, x21, #1 //increment the loop counter ldr x20, =n16 ldr w20, [x20] cmp x21, w20 // compare the counter with the array size blt endloop2 // continue the loop if counter is less than array size
// print a newline character ldr x1, =nlstr bl printf ldp x29, x30, [sp], #16 // function epilog ret