Skip to content

Instantly share code, notes, and snippets.

@quinnzipse
Created February 27, 2020 16:00
Show Gist options
  • Save quinnzipse/95bc7849c82a472f73ab79a0a876691c to your computer and use it in GitHub Desktop.
Save quinnzipse/95bc7849c82a472f73ab79a0a876691c to your computer and use it in GitHub Desktop.
MIPS program that searches through an array of up to 30 possible positive numbers.
# Homework1 Quinn Zipse
.data
prompt1: .asciiz "Enter the number of elements (between 1 and 30) in the array: "
prompt2: .asciiz "Enter "
prompt3: .asciiz " positive integers:"
prompt4: .asciiz "Wrong number entered.\n"
prompt5: .asciiz "Enter a postive number to search (-1 to quit): "
promptNotFound: .asciiz "Integer not found.\n"
promptFound: .asciiz "Integer found "
newLine: .asciiz " times.\n"
array: .word 0 : 30
size: .word 0
.text
la $t1, array #load the array address into a register
li $t2, 2 #you get three incorrect tries
j prompt
incorrect:
beqz $t2, exit # exit if called more than 3 times
addi $t2, $t2, -1
la $a0, prompt4 # tell the user they have an incorrect number.
li $v0, 4
syscall
prompt: la $a0, prompt1
li $v0, 4
syscall # prompt the user to enter a number 1-30
li $v0, 5 # take in input
syscall
move $t0, $v0 # move it to $t0
la $t5, size
sw $t0, 0($t5)
bltz $t0, incorrect # check to see if it's a negative number
add $t3, $t0, $zero
addi $t3, $t3, -30
bgtz $t3, incorrect # check to see if it's more than 30
la $a0, prompt2 # prompt to enter more than:
li $v0, 4
syscall
la $a0, 0($t0) # print $t0
li $v0, 1
syscall
la $a0, prompt3 # other half of positive integer prompt.
li $v0, 4
syscall
load: li $v0, 5 # take in input
syscall
move $t2, $v0 # move it to $t2
sw $t2, 0($t1)
addi $t0, $t0, -1
addi $t1, $t1, 4
bgtz $t0, load
# TODO add the searching feature with a way to stop the program.
searchPrompt: la $a0, prompt5
li $v0, 4
syscall
li $v0, 5
syscall
move $t2, $v0 # get the search key and store it in $t2
bltz $t2, exit # if any negative number, quit the program
lw $t3, 0($t5) # copy the size to $t3 for counting purposes
li $t6, 0 # init $t6 to 0 to count the times found
li $t1, 0 # init $t1 to 0 to count the iterations through the loop
la $t0, array # load the array address into $t0
search: lw $t4, 0($t0)
sub $t4, $t4, $t2 # subtract the two to see if they are equal
addi $t0, $t0, 4 # look at the next spot in the array
addi $t1, $t1, 1 # increment the counter
bnez $t4, endif # it be done
addi $t6, $t6, 1 # we found one but we have to keep searching
endif: bne $t3, $t1, search # keep searching if there are still elements
bnez $t6, found # if there are matches branch to found
notfound: la $a0, promptNotFound # prompt that it wasn't found
li $v0, 4
syscall
j searchPrompt # ask for more input
found: la $a0, promptFound # prompt that it was found
li $v0, 4
syscall
move $a0, $t6 # prompt on how many times it was found
li $v0, 1
syscall
la $a0, newLine
li $v0, 4
syscall
j searchPrompt # send them back
exit: li $v0, 10
syscall # Halt the program
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment