Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help understanding this MIPS code. I am given the equivalent C function a

ID: 3536232 • Letter: I

Question

I need help understanding this MIPS code. I am

given the equivalent C function and was told that if src contained

letters, subroutine foo would be

strNLowerCaseCopy.

I still don't understand the MIPS code, so I ask if anyone can

write comments for each line explaining what each

line does to make the overall function work. The

equivalent C function is:


C code


char * foo (char * src, size_t size)

{

char *dest, *d, *end;

dest = (char *) malloc ((size+1)*sizeof(char));

for(d=dest, end=src+size; d!=end; d++, src++)

{

*d = *src | 0x20;

}

*d = 0;

return dest;

}


MIPS

code (The variable names from the C function are

in some comments to help)


foo:

addiu $sp, $sp, -12

sw $a0, 0($sp) # src

sw $a1, 4($sp) # size

sw $ra, 8($sp)

move $a0, $a1 #

addiu $a0, $a0, 1 #

jal malloc

#

move $t0, $v0 # dest

lw $t1, 0($sp) # src

lw $t2, 4($sp)

addu $t2, $t2, $t1 # end

foo_loop:

beq $t2, $t1, foo_end #

lbu $t4, 0($t1)

#

ori $t4, $t4, 0x20 #

sb $t4, 0($t0) #

addiu $t0, $t0, 1

#

addiu $t1, $t1, 1

#

j foo_loop

foo_end:

sb $0, 0($t1) #

lw $ra, 8($sp) #

addiu $sp, $sp, 12

jr $ra


Here is a picture version to make it easier to read:


Thanks in advance!


Explanation / Answer


foo:

addiu $sp, $sp, -12 # allocate the amount of memory required for the stack frame

sw $a0, 0($sp) # src -- the input src address saved on the stack

sw $a1, 4($sp) # size -- the input size saved on the stack

sw $ra, 8($sp) # return address saved to the stack

move $a0, $a1 # copy the size to the $a0

addiu $a0, $a0, 1 # add 1 to size

jal malloc # call malloc function the only argument is size+1

#

move $t0, $v0 # dest -- the returned pointer move it to $t0 == this is also the saved return value

lw $t1, 0($sp) # src -- load the src from the stack $t1

lw $t2, 4($sp) # load the size from the stack to $t2

addu $t2, $t2, $t1 # end -- size = size + 1

foo_loop: # here is the label for foo_loop

beq $t2, $t1, foo_end # if $t2 = $t1 then goto end of the loop for_end

lbu $t4, 0($t1) # load a byte(char) from address stored in $t1

#

ori $t4, $t4, 0x20 # *d = *src | 0x20

sb $t4, 0($t0) # save byte to *dest

addiu $t0, $t0, 1 # dest = dest + 1

#

addiu $t1, $t1, 1 # *src = *src + 1

#

j foo_loop # go back to the begining of loop

foo_end:

sb $0, 0($t1) # *dest = 0 // last char = ''

lw $ra, 8($sp) # load the return address

addiu $sp, $sp, 12 # clean up the stack frame

jr $ra # return back


// The statement for the return is at the top, its should be lw $v0, dest --- return of malloc is stored in v0 already so not required


comment if you have any doubts


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote