MIPS Coding Exercise the bubble sort 992020 week

MIPS Coding

Exercise – the bubble sort 9/9/2020 week 04 -3. ppt 2

Exercise – the bubble sort • Need two loops – just encapsulate one in the other • Need to read the elements – done before. • Need to compare two numbers – done before • Need to swap – not that hard

. data. word 12, 34, 67, 1, 45, 90, 11, 33, 67, 19 A: . text. globl main: LOOP 1: done: la $s 7, A li $s 6, 9 # getting the address # N-1 li $s 0, 0 addi $s 0, 1 bne $s 0, $s 6, LOOP 1 # i = 0 # i = i + 1 # if i != N-1, outer loop again li $v 0, 10 syscall Getting the first loop done

A: . data. word 12, 34, 67, 1, 45, 90, 11, 33, 67, 19. text. globl main: LOOP 1: LOOP 2: done: la $s 7, A li $s 6, 9 # getting the address # N-1 li $s 0, 0 li $s 1, 0 addi $s 1, 1 sub $t 7, $s 6, $s 0 bne $s 1, $t 7, LOOP 2 addi $s 0, 1 bne $s 0, $s 6, LOOP 1 # # # # li $v 0, 10 syscall Getting both loop done i = 0 j = j + 1 $t 7 will get N-1 -i if j != N-1 -i, inner loop again i = i + 1 if i != N-1, outer loop again

A: . data. word 12, 34, 67, 1, 45, 90, 11, 33, 67, 19. text. globl main: LOOP 1: LOOP 2: done: la $s 7, A li $s 6, 9 # getting the address # N-1 li $s 0, 0 li $s 1, 0 sll $t 0, $s 1, 2 add $t 0, $s 7 lw $t 1, 0($t 0) lw $t 2, 4($t 0) addi $s 1, 1 sub $t 7, $s 6, $s 0 bne $s 1, $t 7, LOOP 2 addi $s 0, 1 bne $s 0, $s 6, LOOP 1 # # # i = 0 j = 0 $t 0 = j * 4 $t 0 is the address of A[j] $t 1 = A[j] $t 2 = A[j+1] j = j + 1 $t 7 will get N-1 -i if j != N-1 -i, inner loop again i = i + 1 if i != N-1, outer loop again li $v 0, 10 syscall Adding the code to read the elements A[j] and A[j+1]

A: . data. word 12, 34, 67, 1, 45, 90, 11, 33, 67, 19. text. globl main: LOOP 1: LOOP 2: L 1: done: la $s 7, A li $s 6, 9 # getting the address # N-1 li $s 0, 0 li $s 1, 0 sll $t 0, $s 1, 2 add $t 0, $s 7 lw $t 1, 0($t 0) lw $t 2, 4($t 0) bgt $t 1, $t 2, L 1 sw $t 1, 4($t 0) sw $t 2, 0($t 0) addi $s 1, 1 sub $t 7, $s 6, $s 0 bne $s 1, $t 7, LOOP 2 addi $s 0, 1 bne $s 0, $s 6, LOOP 1 # # # # i = 0 j = 0 $t 0 = j * 4 $t 0 is the address of A[j] $t 1 = A[j] $t 2 = A[j+1] if A[j] > A[j+1] goto L 1, bypass the swapping do the swap j = j + 1 $t 7 will get N-1 -i if j != N-1 -i, inner loop again i = i + 1 if i != N-1, outer loop again li $v 0, 10 syscall Adding the comparison and swapping

Pseudo instruction • A pseudo instruction is not a real instruction supported by the hardware. It is created to make the coding easier. It is mapped to a unique sequence of real instructions by the assembler. • We have seen some: li $s 0, 0 la $s 7, A bgt $t 1, $t 2, L 1 # load immediate number, often mapped to ori. # load the address of label A into $s 7 # branch if $t 1 is greater than $t 2. ``blt’’ also exits.

In-class exercise -- Loop
- Slides: 9