Question: Fix this assembly code: .globl sort sort: pushq %rbp movq %rsp, %rbp movq 16(%rdi), %rcx # load n movq 8(%rdi), %rdi # load arr movq
Fix this assembly code:
.globl sort sort: pushq %rbp movq %rsp, %rbp movq 16(%rdi), %rcx # load n movq 8(%rdi), %rdi # load arr movq $1, %rbx # set rbx to 1 (true)
outer_loop: movq $0, %rax # set rax to 0 (false) movq $0, %rdx # initialize index to 0 inner_loop: cmpq %rdx, %rcx # compare index with n jge next_outer # jump to next_outer if index >= n movl (%rdi,%rdx,4), %eax # load eax with arr[index] movl 4(%rdi,%rdx,4), %ebx # load ebx with arr[index+1] cmp %eax, %ebx # compare arr[index] with arr[index+1] jle no_swap # jump to no_swap if arr[index] <= arr[index+1] movl %eax, 4(%rdi,%rdx,4) # store arr[index] in arr[index+1] movl %ebx, (%rdi,%rdx,4) # store arr[index+1] in arr[index] movq $1, %rax # set rax to 1 (true) no_swap: incq %rdx # increment index jmp inner_loop # jump to inner_loop next_outer: testq %rax, %rax # test if no swap occurred jne outer_loop # jump to outer_loop if a swap occurred
popq %rbp ret
Based on this C code for function sort: receives a pointer to an array of strings and the number of elements in the array. The function uses bubble sort to sort the array:
# void sort(char **list, int n){ # int sorted=0, i=1, j; # char *temp; # while(i < n && !sorted){ # j=0; # sorted = 1; # while(j
Because it is currently not sorting at all and is outputting the incorrect outputs. The test cases and their expected outputs are:
| ./prog2 sort bla blo obl abl lab bol bal lob | sorted list = {abl, bal, bla, blo, bol, lab, lob, obl} |
| ./prog2 sort 300 500 200 600 400 100 800 700 | sorted list = {100, 200, 300, 400, 500, 600, 700, 800} |
| ./prog2 sort kiwi strawberry apple orange banana peach blueberry mango | sorted list = {apple, banana, blueberry, kiwi, mango, orange, peach, strawberry} |
But instead I am incorrectly outputting:
| ./prog2 sort bla blo obl abl lab bol bal lob | sorted list = {bla, blo, obl, abl, lab, bol, bal, lob} |
| ./prog2 sort 300 500 200 600 400 100 800 700 | sorted list = {300, 500, 200, 600, 400, 100, 800, 700} |
| ./prog2 sort kiwi strawberry apple orange banana peach blueberry mango | sorted list = {kiwi, strawberry, apple, orange, banana, peach, blueberry, mango} |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
