Write a MIPS procedure called findx 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 $a1is 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 x’es in the string, findx should return -1 in register $v1. Your program should print the index returned by findx using a syscall.
Expert Answer
MIPS Examples
String from the Console
I will give an exceptionally basic case to give a vibe for syscall usefulness for perusing in strings. It will help in the event that you open up your book to A-49 in the “PC Organization and Design” book by Patterson and Hennessy, in light of the fact that I will make reference to the table at the highest point of that page in my case. I give a duplicate of the table here.
Service System call code Arguments Result
print_int 1 $a0=integer
print_float 2 $f12=float
print_double 3 $f12=double
print_string 4 $a0=string
read_int 5 integer (in $v0)
read_float 6 float (in $f0)
read_double 7 double (in $f0)
read_string 8 $a0=buffer, $a1=length
sbrk 9 $a0=amount
exit 10
The “Contentions” section clarifies what ought to be in a particular contention enlist (or registers) before a particular syscall. The “Result” section tells what the substance of registers will hold after the syscall. For instance, in the event that you need to yield 23, you should place 23 into enlist $a0, and after that do a syscall 1. On the off chance that you need to peruse an int, you just do a syscall 5. The enroll $v0 holds the aftereffect of the read.
How NOT to do Strings in MIPS
About strings, one pervasive issue I saw with individuals’ code that I inspected was that individuals would attempt to yield a string by putting ASCII esteems into $a0. This is not right. Similarly as in C, you yield a string by passing the MEMORY ADDRESS of the start of a succession of characters (bytes).
Likewise, on the off chance that you do a syscall 8 (read_string), the substance of the string read in are not in $a0. How could, say, a 256 byte string fit into a 4 byte amount? That doesn’t bode well.
The Example
Presently assume you have a record with this extremely straightforward MIPS code in it:
.information
theString:
.space 64
.content
fundamental:
li $v0, 8
la $a0, theString
li $a1, 64
syscall
jr $ra
I’ll experience it line by line.
The main line “.information” reveals to SPIM that what takes after will be information.
“.space 64” at that point puts aside 64 bytes for utilization of whatever reason we need, the principal byte of which might be referenced by the name “theString:”, which shows up at stake some time recently.
“.content” at that point tells the PC that what takes after will be genuine code.
“fundamental:” is our essential principle name that symbolizes the begin of the program.
The following three lines of “la” and “li” explanations set registers to suitable esteems previously we say “syscall”. This is the place you should take a gander at the table at the highest point of A-49. Discover the “read_string” line, and afterward read whatever is left of this. I give a line of the code, and after that some foundation.
li $v0, 8
When you call “syscall” in your code, an esteem called the “framework call code” will figure out what work syscall performs. The “framework call code” is put away in the enroll $v0. The framework call code for perusing a string is 8, so I put away the number 8 into enroll $v0 utilizing “li”.
la $a0, theString
On the off chance that you take a gander at the “contentions” segment in this table, it says “$a0 = cushion, $a1 = length”. This means the $a0 enlist must be set to the area in memory to which the PC will record the information. This is refined by stacking the address (la) of theString into $a0.
Li $a1, 64
The second piece of the contentions section in the table says “$a1 = length”, which you set to the greatest number of characters that ought to be perused in. I picked 64 characters. You can have it be 50, or 200, or 37, or whatever you like, however you shouldn’t go over 64 (in this illustration) on the grounds that in the initial segment of this program you just put aside 64 bytes utilizing the “.space” order. Note that this space put aside incorporates the ending invalid “