You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.7 KiB
64 lines
1.7 KiB
.globl main |
|
|
|
.text |
|
|
|
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 |
|
#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 $s0, 0 # set counter to 0 |
|
jal lookup # hop to lookup function |
|
|
|
# print out result |
|
li $v0, 1 # print_int syscall code = 1 |
|
move $a0, $s0 # load integer into $ao |
|
syscall |
|
|
|
# print newline |
|
li $v0, 4 |
|
la $a0, newline |
|
syscall |
|
|
|
# exit |
|
li $v0, 10 |
|
syscall |
|
|
|
lookup: |
|
addi $t0, $a2, 1 # store one higher value |
|
|
|
# loop to find index |
|
bne $s0, $t0, dsetoob # set out of bounds if we overrun array |
|
li $s0, -1 # index is oob, set to -1 |
|
jr $ra # jump back to ra in main |
|
dsetoob: # label for if we dont want to set oob |
|
|
|
# find current array value |
|
addi $t1, $s0, 0 # load index into $t1 |
|
sll $t1, $t1, 2 # multiply the offset |
|
add $t1, $t1, $a0 # add the offset to the base address |
|
lw $t2, 0($t1) # get current array value: aka arr[$s0] |
|
|
|
bne $a1, $t2, dontjump # check if this is the value we are looking for |
|
jr $ra # return to main function if the values match |
|
dontjump: # continue loop if they match |
|
addi $s0, $s0, 1 # incriment counter |
|
j lookup # back to beginning of loop |
|
|
|
|
|
|
|
.data |
|
arr: .word 5, 2, 1, 4, 6, 3 |
|
enter_num: .asciiz "Enter number to be found: " |
|
newline: .asciiz "\n" |
|
str_found: .asciiz "found" |
|
str_not_found: .asciiz "not_found"
|
|
|