Identify errors in following Aarchv8 assembly language code: My Aarchv8 assembly…
- User
Identify errors in following Aarchv8 assembly language code: My Aarchv8 assembly language program stops when init_array is called. What’s the error and how to fix it? Here is my code: .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 mov x0, 0 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 “n” integer, using stack sub sp, sp, x1 // create storage for the array
// call init_array mov x0, sp ldr x1, =n ldr w1, [x1] bl init_array
// TODO: call print_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 ret
// void init_array(int arr[], int n); .type init_array, @function // this is a private function init_array: stp x29, x30, [sp, #-16] // function prolog mov x2, #0 // initialize loop counter mov x3, #0xa0a0 // 0xa0a0 is element stored in arr. could be changed to rand number.
loop1: cmp x2, x1 //compare i & n bge endloop1 str w3, [x0, x2, lsl 2] // store at [base adr of arr + i4] add x2, x2, #1 b loop1
endloop1: ldp x29, x30, [sp, #16] //function epilog ret
.type print_array, @function stp x29, x30, [sp, #-16] //function prolog mov x2, #0 // initialize loop counter mov x3, #0xa0a0 // 0xa0a0 is the element stored in arr. could be changed to rand number.
loop2: cmp x2, x1 // compare i & n bge endloop2 ldr w3, [x0, x2, lsl 2] // load at [base adr of arr + i4] ldr x0, =tab10dintstr bl printf add x2, x2, #1 b loop2
endloop2: ldp x29, x30, [sp, #16] //function epilog ret
- Assistant
There are several errors in the code:
-
The quotes around the "intstr" and "prntstr" strings aren't straight quotes, but instead are curved/typographical quotes. This can cause syntax errors.
-
In the "init_array" function, the first instruction in the prologue should be "stp x29, x30, [sp, #-16]!" instead of "stp x29, x30, [sp, #-16]".
-
In the "print_array" function, the first instruction in the prologue should be "stp x29, x30, [sp, #-16]!" instead of "stp x29, x30, [sp, #-16]".
-
There is no instruction to print the "prntstr" string in the "print_array" function. The string should be loaded and printed using the "printf" function.
To fix the error of the program stopping when "init_array" is called, we need to make sure that the stack is aligned to 16 bytes before calling the function. To do this, we can add the following instruction before the "sub sp, sp, x1" instruction in the "main" function:
and sp, sp, #0xfffffffffffffff0
This will align the stack before allocating space for the array.
-