Question
Write procedure in MIPS using MARS simulator (STOP POSTING SPAM!!) Write a MIPS procedure called findx using the given .asm file which finds the first
Write procedure in MIPS using MARS simulator (STOP POSTING SPAM!!)
Write a MIPS procedure called findx using the given .asm file which finds the first occurrence of the letter x in an area of memory (array). The procedure receives three arguments in $a1, $a2, and $a3, where $a1 is the starting memory address of a null-terminated ASCII string, $a2 has the starting index (0) and $a3 is the x character. The findx procedure will locate the first x character in the string and return the position where it was found in register $v1. If there are no xes in the string, findx should return -1 in register $v1. Your program should print the index returned by findx using a syscall. Make sure to write plenty of comments so that your code is as easy to understand. Hint: StrCopy
________________________________________________________________
findx.asm
la $a0, prompt # Load address of prompt text into argument
li $v0, 4 # Display Prompt
syscall
li $v0, 9 # Create space for string
li $a0, 100 # 100 bytes
syscall
addi $a0, $v0, 0 # Get address of heap space allocated
li $v0, 8 # Read string
li $a1, 100 # accept 100 bytes
syscall
addi $a1, $a0, 0 # Transfer string address to correct argument
li $a2, 0 # Set starting index to 0
li $a3, 'x' # Set target character to 'x'
findx: # POST CODE HERE #
Found:
addi $v1, $t1, 0 # Set v1
la $a0, result # Load address of result text into argument
li $v0, 4 # Display Result text
syscall
addi $a0, $v1, 0 # Load result index into argument
li $v0, 1 # Display Integer (index)
syscall
j Exit # Exit
NotFound:
la $a0, notfnd # Load address of notfnd text into argument
li $v0, 4 # Display Result text
syscall
Exit:
.data
prompt: .asciiz "Enter a test string: "
result: .asciiz "Found first 'x' at: "
notfnd: .asciiz "No 'x' found."
____________________________________________________
Here is my attempt at findx (no good):
add $t1, $s0, $a1 # $a1 address put into $t1
lb $t1, 0($t1)
beqz $t1, Found # if $t1 = 0, go to Found
bnez $t1, NotFound # if $t1 != 0, go to NotFound
addi $s0, $s0, 1 # increment
j findx # next iteration of loop
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