Question: Fix the current assembly sort function: .globl cmp cmp: push %rbp movq %rsp, %rbp movq 16(%rbp), %rdi # get str1 movq 24(%rbp), %rsi # get
Fix the current assembly sort function:
.globl cmp cmp: push %rbp movq %rsp, %rbp movq 16(%rbp), %rdi # get str1 movq 24(%rbp), %rsi # get str2 loop: movzx (%rdi), %eax # get character from str1, zero-extend to 32 bits movzx (%rsi), %edx # get character from str2, zero-extend to 32 bits cmp %eax, %edx # compare characters jne end_loop # if characters are different, end the loop test %eax, %eax # if end of str1, end the loop je end_loop test %edx, %edx # if end of str2, end the loop je end_loop addq $1, %rdi # move to next character in str1 addq $1, %rsi # move to next character in str2 jmp loop # continue the loop end_loop: sub %eax, %edx # compare the final characters pop %rbp ret
.globl sort sort: pushq %rbp # save the base pointer movq %rsp, %rbp # set up the new base pointer
movq 16(%rbp), %rdi # get the first argument (list) movl 20(%rbp), %esi # get the second argument (n)
movl $1, %edx # initialize i to 1 movl $0, %ecx # initialize sorted to 0
outer_loop: cmpl %edx, %esi # check if i >= n jge end_outer_loop
movl $0, %ecx # reset sorted flag to 0 movl $0, %ebx # initialize j to 0
inner_loop: cmpl %esi, %ebx # check if j >= n - i jge end_inner_loop
movq (%rdi, %rbx, 8), %rax # load list[j] into temp movq 8(%rdi, %rbx, 8), %r8 # load list[j+1] into r8
pushq %rax # push list[j] onto stack pushq %r8 # push list[j+1] onto stack call cmp # call cmp function addq $16, %rsp # clean up the stack
cmpl $0, %eax # check if list[j] > list[j+1] jle not_sorted # jump if not sorted
movq (%rdi, %rbx, 8), %rax # load list[j] into temp movq 8(%rdi, %rbx, 8), %r8 # load list[j+1] into r8
movq %r8, (%rdi, %rbx, 8) # move list[j+1] to list[j] movq %rax, 8(%rdi, %rbx, 8) # move temp to list[j+1]
movl $1, %ecx # set sorted flag to 1
not_sorted: addl $1, %ebx # increment j jmp inner_loop
end_inner_loop: cmpl $0, %ecx # check if sorted flag is set jne outer_loop # jump if not sorted
addl $1, %edx # increment i jmp outer_loop
end_outer_loop: movq %rbp, %rsp # restore the stack pointer popq %rbp # restore the base pointer ret # return to caller
From the original C code:
# void sort(char **list, int n){ # int sorted=0, i=1, j; # char *temp; # while(i < n && !sorted){ # j=0; # sorted = 1; # while(j
Where sort() function 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.
And the test cases and their expected CORRECTED 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} |
Because I am currently outputting these INCORRECT outputs:
| ./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} |
The Sort assembly function should manipulate arrays of integers or strings and call other functions
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
