Bubble Sort ({assembly language})
To complete the MIPS (assemble code) code, you essentially must translate the two nested for
loops and if statement above from Java to MIPS.
add your code to the area marked “PUT CODE HERE.”
Do not modify anything outside that area.
do not write anything to registers $s0 or $s5.
++++++++++++++ (MIPS Code _ Assemble language)+++++++++++++
.data
nums: .word 0 : 12 # \"array\" of 12 words to contain values
size: .word 12 # size of \"array\"
.text
la $s0, nums
la $s5, size # load address of size variable
lw $s5, 0($s5)
# Populate fibs with twelve values
addi $t1, $zero, 55
sw $t1, 0($s0)
addi $t1, $zero, 88
sw $t1, 4($s0)
addi $t1, $zero, 0
sw $t1, 8($s0)
addi $t1, $zero, 22
sw $t1, 12($s0)
addi $t1, $zero, 77
sw $t1, 16($s0)
addi $t1, $zero, 44
sw $t1, 20($s0)
addi $t1, $zero, 99
sw $t1, 24($s0)
addi $t1, $zero, 33
sw $t1, 28($s0)
addi $t1, $zero, 110
sw $t1, 32($s0)
addi $t1, $zero, 66
sw $t1, 36($s0)
addi $t1, $zero, 121
sw $t1, 40($s0)
addi $t1, $zero, 11
sw $t1, 44($s0)
##################################################################
# AT THIS POINT: $s0 is the address of the start of the array
# $s5 is the size (n)
#################################################################
# PUT CODE HERE
# Java (sample code)
#++++++++++(Bubble Sort _ for loop _ sample)
#// arr[i] will be in the correct spot after every iteration
#for (int i = n-1; i > 0; i--)
# for (int j = 0; j < i; j++) // Put arr[j] and arr[j+1] in order
# if (arr[j] > arr[j+1]) { // If they are out of order, swap them
# int tmp = arr[j];
# arr[j] = arr[j+1];
# arr[j+1] = tmp;
# }
#+++++++++++(Bubble Sort _ while loop _ sample)
#int i = n-1;
# while (i > 0) {
# int j = 0;
# while(j < i){
# if (arr[j] > arr[j+1]) { // If they are out of order, swap them
# int tmp = arr[j];
# arr[j] = arr[j+1];
# arr[j+1] = tmp;
# }
# j++
# }
# i--;
#}
##################################################################
la $a0, nums # first argument for print (array)
add $a1, $s5, $zero # second argument for print (size)
jal print # call print routine.
li $v0, 10 # system call for exit
syscall # we are out of here.
######### routine to print the numbers on one line.
######### don\'t touch anything below this line!!!!
.data
space:.asciiz \" \" # space to insert between numbers
head: .asciiz \"Sorted array:\ \"
.text
print:add $s0, $zero, $a0 # starting address of array
add $t1, $zero, $a1 # initialize loop counter to array size
la $a0, head # load address of print heading
li $v0, 4 # specify Print String service
syscall # print heading
out: lw $a0, 0($s0) # load fibonacci number for syscall
li $v0, 1 # specify Print Integer service
syscall # print fibonacci number
la $a0, space # load address of spacer for syscall
li $v0, 4 # specify Print String service
syscall # output string
addi $s0, $s0, 4 # increment address
addi $t1, $t1, -1 # decrement loop counter
bgtz $t1, out # repeat if not finished
jr $ra # return
Solution
.MODEL SMALL
.STACK 100H
.DATA
PROMPT_1 DB \'Th.
Bubble Sort ({assembly language})To complete the MIPS (assemble co.pdf
1. Bubble Sort ({assembly language})
To complete the MIPS (assemble code) code, you essentially must translate the two nested for
loops and if statement above from Java to MIPS.
add your code to the area marked “PUT CODE HERE.”
Do not modify anything outside that area.
do not write anything to registers $s0 or $s5.
++++++++++++++ (MIPS Code _ Assemble language)+++++++++++++
.data
nums: .word 0 : 12 # "array" of 12 words to contain values
size: .word 12 # size of "array"
.text
la $s0, nums
la $s5, size # load address of size variable
lw $s5, 0($s5)
# Populate fibs with twelve values
addi $t1, $zero, 55
sw $t1, 0($s0)
addi $t1, $zero, 88
sw $t1, 4($s0)
addi $t1, $zero, 0
sw $t1, 8($s0)
addi $t1, $zero, 22
sw $t1, 12($s0)
addi $t1, $zero, 77
sw $t1, 16($s0)
addi $t1, $zero, 44
sw $t1, 20($s0)
addi $t1, $zero, 99
sw $t1, 24($s0)
addi $t1, $zero, 33
sw $t1, 28($s0)
addi $t1, $zero, 110
sw $t1, 32($s0)
addi $t1, $zero, 66
sw $t1, 36($s0)
2. addi $t1, $zero, 121
sw $t1, 40($s0)
addi $t1, $zero, 11
sw $t1, 44($s0)
##################################################################
# AT THIS POINT: $s0 is the address of the start of the array
# $s5 is the size (n)
#################################################################
# PUT CODE HERE
# Java (sample code)
#++++++++++(Bubble Sort _ for loop _ sample)
#// arr[i] will be in the correct spot after every iteration
#for (int i = n-1; i > 0; i--)
# for (int j = 0; j < i; j++) // Put arr[j] and arr[j+1] in order
# if (arr[j] > arr[j+1]) { // If they are out of order, swap them
# int tmp = arr[j];
# arr[j] = arr[j+1];
# arr[j+1] = tmp;
# }
#+++++++++++(Bubble Sort _ while loop _ sample)
#int i = n-1;
# while (i > 0) {
# int j = 0;
# while(j < i){
# if (arr[j] > arr[j+1]) { // If they are out of order, swap them
# int tmp = arr[j];
# arr[j] = arr[j+1];
# arr[j+1] = tmp;
# }
# j++
# }
# i--;
#}
3. ##################################################################
la $a0, nums # first argument for print (array)
add $a1, $s5, $zero # second argument for print (size)
jal print # call print routine.
li $v0, 10 # system call for exit
syscall # we are out of here.
######### routine to print the numbers on one line.
######### don't touch anything below this line!!!!
.data
space:.asciiz " " # space to insert between numbers
head: .asciiz "Sorted array: "
.text
print:add $s0, $zero, $a0 # starting address of array
add $t1, $zero, $a1 # initialize loop counter to array size
la $a0, head # load address of print heading
li $v0, 4 # specify Print String service
syscall # print heading
out: lw $a0, 0($s0) # load fibonacci number for syscall
li $v0, 1 # specify Print Integer service
syscall # print fibonacci number
la $a0, space # load address of spacer for syscall
li $v0, 4 # specify Print String service
syscall # output string
addi $s0, $s0, 4 # increment address
addi $t1, $t1, -1 # decrement loop counter
bgtz $t1, out # repeat if not finished
jr $ra # return
Solution
.MODEL SMALL
.STACK 100H
.DATA
PROMPT_1 DB 'The contents of the array before sorting : $'
PROMPT_2 DB 0DH,0AH,'The contents of the array after sorting : $'
ARRAY DB 5,3,9,0,2,6,1,7,8,4
4. .CODE
MAIN PROC
MOV AX, @DATA ; initialize DS
MOV DS, AX
MOV BX, 10 ; set BX=10
LEA DX, PROMPT_1 ; load and display the string PROMPT_1
MOV AH, 9
INT 21H
LEA SI, ARRAY ; set SI=offset address of ARRAY
CALL PRINT_ARRAY ; call the procedure PRINT_ARRAY
LEA SI, ARRAY ; set SI=offset address of the ARRAY
CALL BUBBLE_SORT ; call the procedure BUBBLE_SORT
LEA DX, PROMPT_2 ; load and display the string PROMPT_2
MOV AH, 9
INT 21H
LEA SI, ARRAY ; set SI=offset address of ARRAY
CALL PRINT_ARRAY ; call the procedure PRINT_ARRAY
MOV AH, 4CH ; return control to DOS
INT 21H
MAIN ENDP
;**************************************************************************;
;**************************************************************************;
;------------------------- Procedure Definitions ------------------------;
;**************************************************************************;
;**************************************************************************;
;**************************************************************************;
;----------------------------- PRINT_ARRAY ------------------------------;
;**************************************************************************;
PRINT_ARRAY PROC
; this procedure will print the elements of a given array
; input : SI=offset address of the array
; : BX=size of the array
; output : none
PUSH AX ; push AX onto the STACK
PUSH CX ; push CX onto the STACK
5. PUSH DX ; push DX onto the STACK
MOV CX, BX ; set CX=BX
@PRINT_ARRAY: ; loop label
XOR AH, AH ; clear AH
MOV AL, [SI] ; set AL=[SI]
CALL OUTDEC ; call the procedure OUTDEC
MOV AH, 2 ; set output function
MOV DL, 20H ; set DL=20H
INT 21H ; print a character
INC SI ; set SI=SI+1
LOOP @PRINT_ARRAY ; jump to label @PRINT_ARRAY while CX!=0
POP DX ; pop a value from STACK into DX
POP CX ; pop a value from STACK into CX
POP AX ; pop a value from STACK into AX
RET ; return control to the calling procedure
PRINT_ARRAY ENDP
;**************************************************************************;
;---------------------------- BUBBLE_SORT -------------------------------;
;**************************************************************************;
BUBBLE_SORT PROC
; this procedure will sort the array in ascending order
; input : SI=offset address of the array
; : BX=array size
; output : none
PUSH AX ; push AX onto the STACK
PUSH BX ; push BX onto the STACK
PUSH CX ; push CX onto the STACK
PUSH DX ; push DX onto the STACK
PUSH DI ; push DI onto the STACK
MOV AX, SI ; set AX=SI
MOV CX, BX ; set CX=BX
DEC CX ; set CX=CX-1
@OUTER_LOOP: ; loop label
MOV BX, CX ; set BX=CX
MOV SI, AX ; set SI=AX
MOV DI, AX ; set DI=AX
6. INC DI ; set DI=DI+1
@INNER_LOOP: ; loop label
MOV DL, [SI] ; set DL=[SI]
CMP DL, [DI] ; compare DL with [DI]
JNG @SKIP_EXCHANGE ; jump to label @SKIP_EXCHANGE if DL<[DI]
XCHG DL, [DI] ; set DL=[DI], [DI]=DL
MOV [SI], DL ; set [SI]=DL
@SKIP_EXCHANGE: ; jump label
INC SI ; set SI=SI+1
INC DI ; set DI=DI+1
DEC BX ; set BX=BX-1
JNZ @INNER_LOOP ; jump to label @INNER_LOOP if BX!=0
LOOP @OUTER_LOOP ; jump to label @OUTER_LOOP while CX!=0
POP DI ; pop a value from STACK into DI
POP DX ; pop a value from STACK into DX
POP CX ; pop a value from STACK into CX
POP BX ; pop a value from STACK into BX
POP AX ; pop a value from STACK into AX
RET ; return control to the calling procedure
BUBBLE_SORT ENDP
;**************************************************************************;
;-------------------------------- OUTDEC --------------------------------;
;**************************************************************************;
OUTDEC PROC
; this procedure will display a decimal number
; input : AX
; output : none
PUSH BX ; push BX onto the STACK
PUSH CX ; push CX onto the STACK
PUSH DX ; push DX onto the STACK
XOR CX, CX ; clear CX
MOV BX, 10 ; set BX=10
@OUTPUT: ; loop label
XOR DX, DX ; clear DX
DIV BX ; divide AX by BX
PUSH DX ; push DX onto the STACK
7. INC CX ; increment CX
OR AX, AX ; take OR of Ax with AX
JNE @OUTPUT ; jump to label @OUTPUT if ZF=0
MOV AH, 2 ; set output function
@DISPLAY: ; loop label
POP DX ; pop a value from STACK to DX
OR DL, 30H ; convert decimal to ascii code
INT 21H ; print a character
LOOP @DISPLAY ; jump to label @DISPLAY if CX!=0
POP DX ; pop a value from STACK into DX
POP CX ; pop a value from STACK into CX
POP BX ; pop a value from STACK into BX
RET ; return control to the calling procedure
OUTDEC ENDP
END MAIN