Question & Answer: Write a MIPS procedure called findx which finds the first occurrence of the letter ‘x’ in an area of memory (array). The procedure receives…..

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


Don't use plagiarized sources. Get Your Custom Essay on
Question & Answer: Write a MIPS procedure called findx which finds the first occurrence of the letter ‘x’ in an area of memory (array). The procedure receives…..
Order Essay

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:



.space 64



li                          $v0, 8

la                           $a0, theString

li                            $a1, 64


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 “” character, so it will really read just up to 63 characters from the cradle when the syscall executes.


Finally, having set your contention ($a0, $a1) registers and your call code enlist ($v0), you call syscall. After getting the syscall charge, the framework says, “what do I have to do?” and sees that $v0 == 8. It now knows to peruse in a line from the SPIM support, and to compose the contribution to the memory area referenced by $a0 (which was set to theString), for a string of most extreme length of $a1 (which we set to 64).

It peruses contribution until the point that it experiences a “n” character or achieves the most extreme number of characters it can achieve (which we put away in $a1 as 64), and stores that info (counting the “n” character) into a string invalid ended with a ‘’. Notice that the most extreme length of the string incorporates the “” ending invalid character.

Jr        $ra

This bounce comes back to the arrival address in the $ra enlist.

An Actual Run

In the event that you run this program and sort this in:


…furthermore, hit restore, the memory in the PC at the point referenced by theString will resemble the accompanying. Each piece speaks to a byte in information. The squares are adjoining, as are the bytes in memory. The byte holds the ASCII esteem for the character I show. The principal byte is the byte referenced by “theString”, and the string is termined by an invalid character.

H       e        l         l         o        !        n      

After syscall is done, the byte referenced by “theString” would contain the ascii esteem for ‘H’, the following byte would contain ‘e’, and so forth, and so on.


For a clarification of “vectors” in SPIM, I will build a SPIM program that ascertains the initial 40 terms of my most loved succession, the Fibonacci grouping, and stores it in an exhibit like structure. For those that don’t have the foggiest idea about, the Fibonacci grouping is the succession of numbers {1, 1, 2, 3, 5, 8, 13, etc} with the end goal that an+2 = an+1 + an. The “work of art” Fibonacci arrangement, if there can be said to be a wonder such as this, is where a0 = 1 and a1 = 1.

In the first place we see an exertion in C. The goal for giving this code is to make a characteristic spill out of C to MIPS, and to show how clusters in C and exhibits in MIPS require not be thought of as fundamentally unique substances.

I accept commonality with C, and some essential nature with how to peruse information to and from memory in MIPS (particularly with lw and sw).

By chance, my work that takes after is frequently deliberately wasteful with the end goal of more prominent lucidity, however some of the time being clear one path prompts being vague in some other way. I composed this all late around evening time while tormented with sleep deprivation.

#include <stdio.h>

int theArray[40];

int fundamental() {

int n = 2;

theArray[0] = 1;

theArray[1] = 1;

do {

theArray[n] = theArray[n-1] + theArray[n-2];


} while (n < 40);

return 0;


Presently I’ll rework the program to make it considerably MORE wasteful. It will in any case be in C, aside from it will be worked to help our progress to SPIM when we endeavor to fulfill a similar accomplishment with MIPS.

#include <stdio.h>

int theArray[40];

int principle() {

int t0, t1, t2, t3, t4, t5, t6, t7;/* Our “registers” */

t6 = 1;

t7 = 1;

theArray[0] = t6;/* Storing the initial two terms of the */

theArray[t7] = t6;/* succession into our exhibit */

t0 = 2;


t3 = t0 – 2;

t4 = t0 – 1;

t1 = theArray[t3];

t2 = theArray[t4];

t5 = t1 + t2;

theArray[t0] = t5;

t0 = t0 + 1;

on the off chance that (t0 < 40) goto LLoop;

return 0;


Apparently we’re all comfortable with C, so we can perceive how this program functions. Simply investigate it until the point when it starts to bode well, since (beside the vague variable names) it isn’t so much that extreme. We then “make an interpretation of” this into MIPS constructing agent dialect. When we do as such, there are a few contrasts that must be remembered.

The thing with “clusters” in MIPS, in case we’re to call them that, will be that the “files” are constantly increased as far as bytes. The address “theArray($t0)” will address theArray, however balance by $t0 bytes, that is the address referenced by the name “theArray” in addition to the substance of enlist $t0. Whole numbers take up a word; that is, they are four bytes in length. In the event that you have a section of memory that you expect to use as a variety of whole numbers, to climb (or down) one “component” you should augmentation (or decrement) your addresses not by one, but rather by four!

This really isn’t extraordinary. The main contrast is, C does this for you. It realizes that you have a variety of whole numbers, and you’re referencing “theArray[i]” and afterward reference “theArray[i+1]”, it will respond as you’d anticipate. With SPIM, you should offer lenient gestures yourself. Here is the SPIM code.



.space 160



li            $t6, 1                   # Sets t6 to 1

li            $t7, 4                   # Sets t7 to 4

sw     $t6, theArray($0)    # Sets the primary term to 1

sw       $t6, theArray($t7)   # Sets the second term to 1

li            $t0, 8                   # Sets t0 to 8


addi         $t3, $t0,      – 8

addi        $t4, $t0,      – 4

lw           $t1, theArray($t3)              # Gets the last

lw           $t2, theArray($t4)             # two components

include $t5, $t1, $t2                       # Adds them together…

sw          $t5, theArray($t0)             # …and stores the outcome

addi         $t0, $t0, 4                          # Moves to next “component” of theArray

blt           $t0, 160, circle                  # If not past the finish of theArray, rehash

jr             $ra

Prior to some punk calls attention to how wasteful this MIPS code is, the purpose of this program is to show how to peruse from and keep in touch with areas in memory in a way reminiscent of exhibits. It is not a worldview of effectiveness.

A large portion of it is a reasonable interpretation, yet there are contrasts that are imperative to take note. The first needs to do with the part of memory referenced by “theArray:”.


.space 160

The “.space” order holds an area of free space in a size given by bytes. Since an int takes up 4 bytes and we need to store 40 numbers, 4*40 is 160, so we hold 160 bytes.

t7 = 1;

theArray[0] = t6;/* Storing the initial two terms of the */

theArray[t7] = t6;/* arrangement into our cluster. */

We see that t7 is utilized to store 1, which is the record of the second component. In SPIM, the second component would not be referenced by a balanced of one from the address of the start of the cluster, yet rather by a balance of four bytes.

Li           $t7, 4                      # Sets t7 to 4.

sw         $t6, theArray($0)    # Sets the principal term to 1.

sw         $t6, theArray($t7)    # Sets the second term to 1.

Proceeding onward.

t0 = 2;

I set have the possibility that theArray[t0] would get theArray[t0-1] + theArray[t0-2]. I set t0 to 2 with the goal that it begins at the THIRD ELEMENT of theArray. To fulfill a similar impact with MIPS, I should begin at a counterbalance of 8.

li $t0, 8 # Sets t0 to 8.

Proceeding onward.

t3 = t0 – 2;

t4 = t0 – 1;

In the C code, we needed to reference the two components previously the component of list t0. I store the lists of these two components in t3 and t4.

addi $t3, $t0, – 8

addi $t4, $t0, – 4

Like some time recently, if theArray($t0) focuses to the component of this cluster we’re presently computing, by the meaning of the Fibonacci grouping we need to reference the past two terms. I’ve gone over balances by elements of eights a few times as of now, so I will accept that you can make sense of that what I’m doing here is equal to what I do in my C code. Proceeding onward.

t0 = t0 + 1;

This counterbalances the list by 1, which in SPIM would be proficient by expanding the balanced by 4 bytes. Keep in mind, when we increase in the C code, that is REALLY going ahead the length of an int in memory, or four bytes. MIPS does not do this for us, so we should include four.

addi $t0, $t0, 4

Proceeding onward.

on the off chance that (t0 < 40) goto LLoop;

On the off chance that the record is presently 40 after we’ve augmented it, at that point we’re finished. The impact for the MIPS branch is comparative, aside from we consider that we’re managing files of bytes, not words.

blt $t0, 160, circle

The 40 components are referenced by the addresses (theArray + 0), (theArray + 4), (theArray + 8), and so forth and so on, as far as possible up to (theArray + 156). In the event that our balance has achieved 160, at that point we shouldn’t do any more, and the program closes.

jr $ra

This hop comes back to the arrival address in the $ra enlist.

Still stressed from student homework?
Get quality assistance from academic writers!