Question
can someone help me fix this code, I cant figure out whats wrong the assingment incudes a MIPS program, which implements a function that searches
can someone help me fix this code, I cant figure out whats wrong
the assingment incudes a MIPS program, which implements a function that searches an array for a data value and returns the index of the data in the array, or -1 if the data is not in the array.
you're only required to implement an if-then-else statement where the **code has be highlighted in bold** but what I have dosent seem to be working
here is what I have so far:
# array search Program
#
.data
program:
.asciiz "Array Search Program"
prompt:
.asciiz "Enter search value: "
txt1:
.asciiz "x["
txt2:
.asciiz "]="
txt3:
.asciiz "The value "
txt4:
.asciiz " was not found"
txt5:
.ascii "Search returns "
.align 2
x:
.word 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31
.global main
.text
main:
# register map
#
# $s0 search value
# Print program header
la $a0, program
jal printlnString
# Input and save search key
la $a0, prompt
jal getIntPrompt
move $s0, $v0
# Search for key in array x
move $a0, $s0
la $a1, x
li $a2, 11
jal search
# *** your code startshere ***
# Check if search function returned -1
beq $v0, -1, notFound
# Print index of key
la $a0, txt5
move $a1, $v0
jal printlnStringInt
j end
notFound:
# Print "The value was not found" message
la $a0, txt3
la $a1, txt4
jal printStringString
end:
# *** your code ends here ***
# Exit
li $v0, 10
syscall
search:
# register map
# $a0 search value
# $a1 array address
# $a2 array size
#
# $t0 loop counter
# $t1 address of i'th array element
# value of i'th array element
li $v0, -1
# for $t0=0, $t0 < $a3, $t0++
li $t0, 0
loop:
bge $t0, $a2, return
sll $t1, $t0, 2
addu $t1, $t1, $a1
lw $t1, ($t1)
# if x[i] > $a0
bne $t1, $a0, endif2
move $v0, $t0
j return
endif2:
addi $t0, $t0, 1
j loop
return:
jr $ra
# I/O Library
# includes input functions
# ensure this code is at the end of your assembly file
.data
newLine:
.asciiz " "
space:
.asciiz " "
.align 2
.text
printSpace:
# prints a space
# arguments
# none
# returns
# none
la $a0, space
li $v0, 4
syscall
jr $ra
println:
# prints a new line
# arguments
# none
# returns
# none
la $a0, newLine
li $v0, 4
syscall
jr $ra
printInt:
# prints an integer
# arguments
# $a0 integer to print
# returns
# none
li $v0, 1
syscall
jr $ra
printString:
# prints a string
# arguments
# $a0 address of string
# returns
# none
li $v0, 4
syscall
jr $ra
printlnString:
# prints a string followed by a new line
# arguments
# $a0 address of string
# returns
# none
la $a0, ($a0)
jal printString
jal println
jr $ra
getInt:
# reads an integer from stdin
# arguments
# none
# returns
# $v0 integer value
li $v0, 5
syscall
jr $ra
getIntPrompt:
# prints a string and reads an integer from stdin
# arguments
# $a0 address of prompt string
# returns
# $v0 integer value
la $a0, ($a0)
jal printString
jal getInt
jr $ra
printStringString:
# prints two strings
# arguments
# $a0 address of first string
# $a1 address of second string
# returns
# none
la $a0, ($a0)
jal printString
la $a0, ($a1)
jal printString
jr $ra
printlnStringInt:
# prints a string, an integer, and a new line
# arguments
# $a0 address of string
# $a1 integer value
# returns
# none
la $a0, ($a0)
jal printString
move $a0, $a1
jal printInt
jal println
jr $ra
the output is meant to look like this:
Desired Output Style:
Array Search Program Enter search value: 0 The value 0 was not found -- program is finished running -- Reset: reset completed. Array Search Program Enter search value: 2 x[0]=2 -- program is finished running -- Reset: reset completed. Array Search Program Enter search value: 16 The value 16 was not found -- program is finished running -- Reset: reset completed. Array Search Program Enter search value: 19 x[7]=19 -- program is finished running -- Reset: reset completed. Array Search Program Enter search value: 31 x[10]=31 -- program is finished running -- Reset: reset completed. Array Search Program Enter search value: 16 The value 16 was not found -- program is finished running -- Reset: reset completed. Array Search Program Enter search value: 32 The value 32 was not found -- program is finished running --
. however, the output from my code only prints: "Array Search Program" once
.
. Here is the original un-edited code from the assingment if you need it
the original Program:
# array search Program # .data program: .asciiz "Array Search Program" prompt: .asciiz "Enter search value: " txt1: .asciiz "x[" txt2: .asciiz "]=" txt3: .asciiz "The value " txt4: .asciiz " was not found" txt5: .ascii "Search returns " .align 2 x: .word 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 .global main .text main: # register map # # $s0 search value # Print program header la $a0, program jal printlnString # Input and save search key la $a0, prompt jal getIntPrompt move $s0, $v0 # Search for key in array x move $a0, $s0 la $a1, x li $a2, 11 jal search # *** you will replace the code below here *** # Print index of key la $a0, txt5 move $a1, $v0 jal printlnStringInt # *** your code ends here *** # Exit li $v0, 10 syscall search: # register map # $a0 search value # $a1 array address # $a2 array size # # $t0 loop counter # $t1 address of i'th array element # value of i'th array element li $v0, -1 # for $t0=0, $t0 < $a3, $t0++ li $t0, 0 loop: bge $t0, $a2, return sll $t1, $t0, 2 addu $t1, $t1, $a1 lw $t1, ($t1) # if x[i] > $a0 bne $t1, $a0, endif2 move $v0, $t0 j return endif2: addi $t0, $t0, 1 j loop return: jr $ra # I/O Library # includes input functions # ensure this code is at the end of your assembly file .data newLine: .asciiz " " space: .asciiz " " .align 2 .text printSpace: # prints a space # arguments # none # returns # none la $a0, space li $v0, 4 syscall jr $ra println: # prints a new line # arguments # none # returns # none la $a0, newLine li $v0, 4 syscall jr $ra printInt: # prints an integer # arguments # $a0 integer to print # returns # none li $v0, 1 syscall jr $ra printlnInt: # prints an integer with a new line # arguments # $a0 integer to print # returns # none li $v0, 1 syscall la $a0, newLine li $v0, 4 syscall jr $ra printString: # prints a string # arguments # $a0 address of string to print # returns # none li $v0, 4 syscall jr $ra printlnString: # prints a string with a new line # arguments # $a0 address of string to print # returns # none li $v0, 4 syscall la $a0, newLine li $v0, 4 syscall jr $ra printStringInt: # prints a string and an integer # arguments # $a0 address of string to print # $a1 integer to print # returns # none # save return address so not overwritten # save $a1 so not overwritten addiu $sp, $sp, -8 sw $a1, 4($sp) sw $ra, 0($sp) # print string jal printString # print int # get argument from stack lw $a0, 4($sp) jal printInt lw $ra, 0($sp) addiu $sp, $sp, 8 jr $ra printlnStringInt: # prints a string and an integer with new line # arguments # $a0 address of string to print # $a1 integer to print # returns # none # save return address so not overwritten # save $a1 so not overwritten addiu $sp, $sp, -8 sw $a1, 4($sp) sw $ra, 0($sp) # print string jal printString # print int # get argument from stack lw $a0, 4($sp) jal printlnInt lw $ra, 0($sp) addiu $sp, $sp, 8 jr $ra getInt: # inputs an integer # arguments # none # returns # $v0 integer that was input li $v0, 5 syscall jr $ra getIntPrompt: # inputs an integer using a text prompt # arguments # $a0 address of string to print # returns # $v0 integer that was input # save return address so not overwritten addiu $sp, $sp, -4 sw $ra, 0($sp) jal printString jal getInt lw $ra, 0($sp) addiu $sp, $sp, 4 jr $ra
if you could help me fix this problem and show what you recived for your output, then I would be very thankful
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started