In: Computer Science
Check a number if the Database. Write MIPS assembly code for the following requirements.
Given the following code for the data segment.
.data
Database: .word 1,2,3,4,5,6,7,8,9,10
Ask user to type in a random integer number using syscall #5. Check if this number is within the database or not, print out "number found!" if the number was foudn in the database, print out "No such number found in database!" if not.
Code:
.data
prompt: .asciiz "Enter a number: "
found: .asciiz "number found!"
notfound: .asciiz "No such number found in
database!"
Database: .word 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
.text
.globl main
main:
# prompting user to enter a random number
la $a0, prompt
li $v0, 4
syscall
# reading number
li $v0, 5
syscall # number is stored in $v0
# address of Database in $t1
la $t1, Database
li $t0, 0 # $t0 = 0,
corresponds to i = 0
li $t3, 10 # size of
Database
Loop:
lw $t2, ($t1)
beq $v0, $t2,
printFound # if Databasep[i] == number entered by
user
addi $t0, $t0, 1
# i++
addi $t1, $t1, 4
# increment the array
beq $t0, $t3,
printNotFound # if i == size, print not found
j Loop
# print not found message
printFound:
la $a0, found
li $v0, 4
syscall
j exit # exit
printNotFound:
la $a0, notfound
li $v0, 4
syscall
exit:
li $v0, 10
syscall
OUTPUT: