MIPS Coding Exercise the bubble sort 5242021 week

MIPS Coding

Exercise – the bubble sort 5/24/2021 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

A: . data. word 12, 34, 67, 1, 45, 90, 11, 33, 67, 19. text. globl main: LOOP 2: LOOP 1: L 1: done: la $s 7, A li $s 6, 9 li $s 0, 0 # getting the address # N-1 # i = 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 1 addi $s 0, 1 bne $s 0, $s 6, LOOP 2 # # # # li $v 0, 10 syscall 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

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
- Slides: 6