Optimize following Aarch64 assembly language program .section .data .prompt1: .asciz…
- User
Optimize following Aarch64 assembly language program .section .data .prompt1: .asciz "The random array is: " .prompt2: .asciz "The sorted array is: " .printint: .asciz "%d " .printnl: .asciz "\n"
.section .bss // Allocate space for 10 4-byte integers in the stack array: .skip 40
.section .text .global main .type main, @function
main: // Create space in the stack frame by adjusting sp sub sp, sp, #32 stp x29, x30, [sp, #16] add x29, sp, #16
// Initialize a temporary register of the array element to zero str wzr, [x29, #-4]
// Initialize the loop iterator to zero str wzr, [sp, #8]
mov x0, 0
// Generate a seed to be used by srand and set the seed for the random number generator bl time bl srand
.randloop: // Load loop iterator and check if i >= 10 ldr w8, [sp, #8] subs w8, w8, #10
// If iterator i >= 10, exit randloop bge .endrandloop
// Generate random integer bl rand
// Load loop iterator ldrsw x9, [sp, #8] adrp x8, array add x8, x8, :lo12:array
// Following code block is used to do modulus operation to fit the range [10..99] mov w3, 90 udiv w1, w0, w3 mul w1, w1, w3 sub w0, w0, w1
// Add the minimum bound, since the random number starts from 10 add w0, w0, #10
// Store the generated random integer in the allocated memory spot for the array str w0, [x8, x9, lsl #2] ldr w8, [sp, #8] add w8, w8, #1
// Update iterator value and continue looping str w8, [sp, #8] b .randloop
.endrandloop: // Address of the prompt1 into x0 adrp x0, .prompt1 add x0, x0, :lo12:.prompt1
// Print a formatted string to output stream bl printf str wzr, [sp, #4]
// This loop iterates over the array elements and print them out to console .printloop: ldr w8, [sp, #4] subs w8, w8, #10
// If iterator >= 10, exit loop bge .endprintloop
// Load loop iterator ldrsw x9, [sp, #4] adrp x8, array add x8, x8, :lo12:array
// Get array element using the iterator and offset by memory size ldr w1, [x8, x9, lsl #2] adrp x0, .printint add x0, x0, :lo12:.printint
// Print the integer loaded in w1 register bl printf ldr w8, [sp, #4] add w8, w8, #1 str w8, [sp, #4] b .printloop
// End of print loop. Print empty line .endprintloop: adrp x0, .printnl add x0, x0, :lo12:.printnl bl printf
// Set outer loop iterator to 1 and store it to register mov w8, #1 str w8, [sp, #16]
.outerinsertionsort: ldr w8, [sp, #16] subs w8, w8, #10
// Iterates from 1 to 9. // If iterator >= 10, jump to end of outerinsertionsort bge .endouterinsertionsort
// Load the value at current position of the arrary, load the value at that location, // and store it and iterator-1 on the stack (pesudo code: key = array[i]/ j = i - 1) ldrsw x9, [sp, #16] adrp x8, array add x8, x8, :lo12:array ldr w8, [x8, x9, lsl #2] str w8, [sp, #12] ldr w8, [sp, #16] subs w8, w8, #1 str w8, [sp, #8]
- Assistant