Question: How to fixed these provide listsort.asm assembly code as same as the listsort.c code with the provide listmian.asm 1. Here is the listsort.asm I have

How to fixed these provide listsort.asm assembly code as same as the listsort.c code with the provide listmian.asm

1. Here is the listsort.asm I have now: .globl recurSelectionSort recurSelectionSort: #struct Node* recurSelectionSort(struct Node* head) { addi sp, sp, -16 # allocate space for the return address and saved registers sw ra, 0(sp) # save the return address sw s0, 4(sp) # save s0 sw s1, 8(sp) # save s1 # if (head->next == NULL) lw t0, 0(a0) # Load head->next beqz t0, endif1 # if (head->next == NULL) # return head; mv s0, a0 # save head to s0 addi sp, sp, 16 # deallocate space before returning jr ra # return endif1: # struct Node* min = head; mv s1, a0 # struct Node* beforeMin = NULL; mv s0, x0 # struct Node* ptr; mv t0, a0 # for (ptr = head; ptr->next != NULL; ptr = ptr->next) { forinit: lw t1, 4(t0) # Load ptr->next beqz t1, endfor # for (ptr->next != NULL) lw t2, 4(t1) # Load ptr->next->studentid lw t3, 4(s1) # Load min->studentid bge t3, t2, forloop # if (ptr->next->studentid < min->studentid ) mv s1, t1 # min = ptr->next; mv s0, t0 # beforeMin = ptr; forloop: lw t0, 4(t0) # ptr = ptr->next; lw t1, 0(t0) # Load ptr->next bnez t1, forinit # for (ptr->next != NULL) # if (ptr->next->studentid < min->studentid ) { ifmin: # min = ptr->next; mv s1, t1 # beforeMin = ptr; mv s0, t0 endifmin: # } endfor: # } if2: # if (min != head) bne s1, a0, if2 # swapNodes(&head, head, min, beforeMin); jal swapNodes lw t0, 0(a0) # Load head->next endif2: # head->next = recurSelectionSort(head->next); jal recurSelectionSort sw s0, 0(a0) # beforeMin->next = min->next (no need to swap if min is already at the head) sw s1, 4(t0) # min->next = ptr # return head; mv s0, a0 # s0 = head addi sp, sp, 16 # deallocate space before returning jr ra # return

2. Here is the listmain.asm:

.text

.globl main main: addi sp,sp,-16

# studentlist = createList(studentArr, SIZE); li a1,10 la a0, studentArr jal createList sw a0,12(sp)

# sort(&studentlist); addi a0,sp,12 jal sort

# printList(studentlist); lw a0,12(sp) jal printList

# return li a7, 10 ecall

.globl addStudent # asm is nutty as the struct was passed in the registers. addStudent: addi sp,sp,-16 sw ra,12(sp) sw s0,8(sp) sw s1,4(sp) mv s1,a0 mv s0,a1 li a0,20 jal malloc lw a2,0(s0) lw a3,4(s0) lw a4,8(s0) lw a5,12(s0) sw a2,0(a0) sw a3,4(a0) sw a4,8(a0) sw a5,12(a0) sw s1,16(a0) lw ra,12(sp) lw s0,8(sp) lw s1,4(sp) addi sp,sp,16 jr ra

.globl createList createList: blez a1,endcreateList addi sp,sp,-48 sw ra,44(sp) sw s0,40(sp) sw s1,36(sp) mv s0,a0 slli s1,a1,2 add s1,s1,a1 slli s1,s1,2 add s1,a0,s1 li a0,0 cloop: lw a1,0(s0) lw a2,4(s0) lw a3,8(s0) lw a4,12(s0) lw a5,16(s0) sw a1,0(sp) sw a2,4(sp) sw a3,8(sp) sw a4,12(sp) sw a5,16(sp) mv a1,sp jal addStudent addi s0,s0,20 bne s0,s1,cloop lw ra,44(sp) lw s0,40(sp) lw s1,36(sp) addi sp,sp,48 jr ra endcreateList: li a0,0 ret

.globl printList printList: beqz a0,endprintList addi sp,sp,-16 sw ra,12(sp) sw s0,8(sp) mv s0,a0

lw a0,8(s0) jal printint li a0, 0x20 jal printchar mv a0,s0 jal printstring li a0, 0x20 jal printchar lw a0,12(s0) jal printint li a0, ' ' jal printchar lw a0,16(s0) jal printList lw ra,12(sp) lw s0,8(sp) addi sp,sp,16 jr ra

endprintList: ret

#essentially swizzles the next pointers of the args. .globl swapNodes swapNodes: sw a2,0(a0) sw a1,16(a3) lw a5,16(a2) lw a4,16(a1) sw a4,16(a2) sw a5,16(a1) ret

.globl sort sort: addi sp,sp,-16 sw ra,12(sp) sw s0,8(sp) mv s0,a0 lw a0,0(a0) beqz a0,sortbasecase jal recurSelectionSort sw a0,0(s0) sortbasecase: lw ra,12(sp) lw s0,8(sp) addi sp,sp,16 jr ra

.globl malloc malloc: # loosely simulates sbrk system call # a0 = amount of memory in bytes # a0 = address to the allocated block li a7, 9 ecall ret .globl printchar printchar: li a7, 11 ecall ret .globl printint printint: li a7, 1 ecall ret .globl printstring printstring: li a7, 4 ecall ret .globl studentArr .data studentArr: .string "Dougy" .word 13 .word 2122 .word 0 .string "Timmy" .word 15 .word 2122 .word 0 .string "Emily" .word 18 .word 2123 .word 0 .string "Jimmy" .word 14 .word 2120 .word 0 .string "Kimmy" .word 11 .word 2123 .word 0 .string "Carlo" .word 19 .word 2123 .word 0 .string "Vicky" .word 22 .word 2120 .word 0 .string "Anton" .word 12 .word 2322 .word 0 .string "Brady" .word 10 .word 2120 .word 0 .string "Sonya" .word 16 .word 2123 .word

4. Correct Output:

10 Brady 2120 11 Kimmy 2123 12 Anton 2322 13 Dougy 2122 14 Jimmy 2120 15 Timmy 2122 16 Sonya 2123 18 Emily 2123 19 Carlo 2123 22 Vicky 2120

5. The output for listsort.asm I have now:

16 2123 10 Brady 2120 12 Anton 2322 22 Vicky 2120 19 Carlo 2123 11 Kimmy 2123 14 Jimmy 2120 18 Emily 2123 15 Timmy 2122 13 Dougy 2122

Please provide the completely modified listsort.asm assembly code, the listsort.asm must have the same output as the listsort.c.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!