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.
 
 

73 lines
1.8 KiB

# Declare main as a global function
.globl main
# All program code is placed after the
# .text assembler directive
.text
# The label 'main' represents the starting point
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 newline
#li $v0, 4
#la $a0, newline
#syscall
# print out result
li $v0, 1 # print_int syscall code = 1
move $a0, $s0 # load integer into $ao
syscall
j exit # exit out of program
lookup:
addi $t0, $a2, 1 # store one higher value
# loop to find index
beq $s0, $t0, setoob # jump to oob function if we overrun array
# 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]
beq $a1, $t2, setval # check if this is the value we are looking for
addi $s0, $s0, 1 # incriment counter
j lookup # back to beginning of loop
# set value function
setval:
jr $ra
# set out of bounds function
setoob:
li $s0, -1 # index is oob, set to -1
jr $ra # jump back to ra in main
exit:
# All memory structures are placed after the
# .data assembler directive
.data
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_not_found: .asciiz "not_found"