homeworkstudyhelp

Our Services

Get 15% Discount on your First Order

Q1. Consider the following MIPS assembly code fragment. The sort…

Q1. Consider the following MIPS assembly code fragment. The sort
function implements bubble sort using a helper function swap.
Assume there are no syntax errors. But, there are 10 bugs in this code with respect to the
semantics of MIPS and runtime bugs. That is, there are exactly 10 lines of code that either
need to be modified or added to get this code working correctly.
• If an instruction needs to be modified, state the instruction as is and then state what
it should be modified to be.
• If an instruction needs to be added, state before which instruction it should be added
and the instruction to add.

1 # swap k and k+1 of an array where a0 is array addreess and a1 is k
2 swap :
3 add $t0 , $v1 , $zero #t0 = k
4 add $t0 , $v0 , $t0 #t0 = v[k]
5 lw $t1 , 0( $t0 ) # load v[k]
6 lw $s1 , 4( $t0 ) # load v[k+1]
7 sw $s1 , 0( $t0 ) #v[k] = v[k+1]
8 sw $t1 , 4( $t0 ) #v[k+1] = v[k]
9 jr $ra
10
11 # Bubble sort an array of ints with address a0 and length a1
12 sort :
13 addi $sp , $sp , 16 # make room for ints on stack
14 sw $s3 , 12( $sp )
15 sw $s2 , 8( $sp )
16 sw $s1 , 4( $sp )
17 sw $s0 , 0( $sp )
18 add $s2 , $a0 , $zero #s2 = a0 ( address of v array )
19 add $s3 , $a1 , $zero #s3 = a1 ( length of v array )
20 add $s0 , $zero , $zero #s0 = i = 0
21 for1tst :
22 slt $t0 , $s0 , $s3 #t0 = (s0 < s3)
23 beq $t0 , $zero , exit1
24 addi $s1 , $s0 , -1 #s1 = j = i-1
25 for2tst :
26 slti $t0 , $s1 , 0 #t0 = (s1 < 0)
27 bne $t0 , $zero , exit2 #go to exit2 if (s1 < 0)
28 sll $t1 , $s1 , 2 #t1 = j * 4
29 add $t2 , $s2 , $t1 #t2 = v + (j*4)
30 lw $t3 , 0( $t2 ) #t3 = v[j]
31 lw $t4 , 4( $t2 ) #t4 = v[j+1]
32 slt $t0 , $t4 , $t3 #t3 = v[j+1] < v[j]
33 beq $t0 , $zero , exit2 #go to exit2 if v[j+1] >= v[j]
34 add $v0 , $s2 , $zero #set args for swap proc
35 add $v1 , $s1 , $zero
36 j swap # call swap
37 addi $s1 , $s1 , -1 # decrement j
38 j for2tst # iterate inner loop
39 exit2 :
40 addi $s0 , $s0 , 1 # increment i
41 j for1tst # iterate outer loop
42 exit1 :
43 lw $s0 , 0( $sp )
44 lw $s1 , 4( $sp )
45 lw $s2 , 8( $sp )
46 lw $s3 , 12( $sp )
47 addi $sp , $sp , -16
48 jr $ra

Share This Post

Email
WhatsApp
Facebook
Twitter
LinkedIn
Pinterest
Reddit

Order a Similar Paper and get 15% Discount on your First Order

Related Questions