Finish writing aarch64 assembly language program .section .rodata getnstr: .string…
- User
Finish writing 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 // todo: store value of n into a stack pointer to pass it as a parameter 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 x2, #0 // loop counter // todo: retrieve value of n from stack pointer and store it in register 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
// Updated init_array function code:
.type init_array, @function init_array: stp x29, x30, [sp, #-16]! // function prolog mov x2, #0 // loop counter
// get the value of n from the stack pointer ldr x20, =n ldr w20, [x20] // loop n times and initialize array with random valuesloop1: cmp x2, x20 b.ge endloop1
// call rand function and store result in array bl rand add x3, sp, x2, LSL #2 // calculate memory location for current index str w0, [x3] add x2, x2, #1 // increment loop counter b loop1endloop1: ldp x29, x30, [sp], #16 ret
- User
.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. - Assistant
// Updated init_array function code:
.type init_array, @function init_array: stp x29, x30, [sp, #-16]! // function prolog
mov x21, #0 // loop counter ldr x20, =n ldr w20, [x20] // loop n times and initialize array with random valuesloop1: cmp x21, x20 b.ge endloop1
// call rand function and store result in array bl rand add x3, sp, x21, LSL #2 // calculate memory location for current index str w0, [x3] add x21, x21, #1 // increment loop counter b loop1endloop1: ldp x29, x30, [sp], #16 ret