|
|
@ -7,23 +7,33 @@ |
|
|
|
|
|
|
|
|
|
|
|
# The label 'main' represents the starting point |
|
|
|
# The label 'main' represents the starting point |
|
|
|
main: |
|
|
|
main: |
|
|
|
|
|
|
|
# print user input |
|
|
|
|
|
|
|
li $v0, 4 |
|
|
|
|
|
|
|
la $a0, enter_num |
|
|
|
|
|
|
|
syscall |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# get input from user and set argument |
|
|
|
|
|
|
|
li $v0, 5 |
|
|
|
|
|
|
|
syscall |
|
|
|
|
|
|
|
move $a1, $v0 |
|
|
|
|
|
|
|
|
|
|
|
la $a0, arr # $a0 based address of array |
|
|
|
la $a0, arr # $a0 based address of array |
|
|
|
li $a1, 1 # $a1 number to be found (checked) - change the value (7) to test |
|
|
|
#li $a1, 1 # $a1 number to be found (checked) - change the value (7) to test |
|
|
|
li $a2, 6 # $a2 max array index to be checked - change the value (3) to test |
|
|
|
li $a2, 6 # $a2 max array index to be checked - change the value (3) to test |
|
|
|
li $s0, 0 # set counter to 0 |
|
|
|
li $s0, 0 # set counter to 0 |
|
|
|
jal lookup # hop to lookup function |
|
|
|
jal lookup # hop to lookup function |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# print newline |
|
|
|
|
|
|
|
#li $v0, 4 |
|
|
|
|
|
|
|
#la $a0, newline |
|
|
|
|
|
|
|
#syscall |
|
|
|
|
|
|
|
|
|
|
|
# print out result |
|
|
|
# print out result |
|
|
|
li $v0, 1 # print_int syscall code = 1 |
|
|
|
li $v0, 1 # print_int syscall code = 1 |
|
|
|
move $a0, $s0 # load integer into $ao |
|
|
|
move $a0, $s0 # load integer into $ao |
|
|
|
syscall |
|
|
|
syscall |
|
|
|
j exit # exit out of program |
|
|
|
j exit # exit out of program |
|
|
|
|
|
|
|
|
|
|
|
# lookup (int arr[], int num, int cnt). |
|
|
|
|
|
|
|
# arr[] = 5 2 1 4 6 3 |
|
|
|
|
|
|
|
# 1. lookup (arr, 5, 3) -> 0 (the index of ‘5’ in the array) |
|
|
|
|
|
|
|
# 2. lookup (arr, 1, 3) -> 2 (the index of ‘1’ in the array) |
|
|
|
|
|
|
|
# 3. lookup (arr, 7, 6) -> -1 (‘7’ is not found in the array) |
|
|
|
|
|
|
|
# 4. lookup (arr, 4, 3) -> -1 (the last parameter indicates to check only first 3 values in the |
|
|
|
|
|
|
|
lookup: |
|
|
|
lookup: |
|
|
|
addi $t0, $a2, 1 # store one higher value |
|
|
|
addi $t0, $a2, 1 # store one higher value |
|
|
|
|
|
|
|
|
|
|
@ -57,7 +67,7 @@ exit: |
|
|
|
# .data assembler directive |
|
|
|
# .data assembler directive |
|
|
|
.data |
|
|
|
.data |
|
|
|
arr: .word 5, 2, 1, 4, 6, 3 # change array to test |
|
|
|
arr: .word 5, 2, 1, 4, 6, 3 # change array to test |
|
|
|
#arr: .word 5, 2, 1, 4, 6, 3 # change array to test |
|
|
|
enter_num: .asciiz "Enter number to be found: " |
|
|
|
|
|
|
|
newline: .asciiz "\n" |
|
|
|
str_found: .asciiz "found" |
|
|
|
str_found: .asciiz "found" |
|
|
|
str_not_found: .asciiz "not_found" |
|
|
|
str_not_found: .asciiz "not_found" |
|
|
|
nl: .asciiz "\n" |
|
|
|
|
|
|
|