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

C++ TO MIPS TRANSLATION Mstrdup.s This file must be transferred for Part Two Thi

ID: 3801782 • Letter: C

Question

C++ TO MIPS TRANSLATION

Mstrdup.s

This file must be transferred for Part Two This program is very important to practice the procedure calling convention. This program will simply get a string from the user (using a buffer allocated on the stack) and make a copy of the string, placing the result in dynamic (heap) memory. It then displays the copy. I have written a stub for you with C code for you to translate that gets the original string. It then calls a function strdup() that you must provide, passing it the [ address of the ] original string. strdup() returns a pointer to the new string. The prototype is char *strdup(const char *) In a nutshell, here is what strdup() will do: • get the length of the string (using strlen()). The length returned by strlen does not include the nul-terminator. • call malloc() (see the SupportFunctions) to get a pointer to a piece of memory large enough to hold the copy (you need enough room for a null-terminator too!) • copy the original string to the newly allocated memory using strcpy() from util.s. To practice the procedure calling convention, your strdup() function must , in turn, call two other functions from util.s: strlen() and strcpy() I have written a stub for you to start with that has the C code that repeatedly asks the user to type in a string, calls strdup (which is an empty function in the stub) and outputs the result. It continues until the user clicks Cancel or types q as the only character in the string. It is strdup.s in the procedures directory. You will have to translate the C code the MIPS and add your strdup() function. NOTE: strlen() and strcpy() are leaf functions and do not use many registers. You may not use this knowledge when you write strdup. Obey the calling convention strictly. Pretend you do not know what registers strlen() and strcpy() will use. Transfer a copy of strdup.s to get credit for Part Two of this exercise set.





#
# strdup.s - copy a string and output the new string
# the new string is dynamically-allocated.
# the original string is in a buffer on the stack
#
# contains the user-implemented function
# char * strdup (char * str);
#
# NEEDS to .include util.s
#
.data
ipprompt: .asciiz "Enter the string to copy (max 100 chars):"
ipprompt2: .asciiz "Here's the original:"
opprompt: .asciiz "Here's the copy:"
ipstring: .space 100
ipstringend:
.align 2
ipstringmax: .word 0
.text
.globl main
main:
# I have done the stack for you. You have room for
# -- four arg regs
# -- 100 byte character buffer (at 16($sp))
# -- one s-register (for pointer to new string returned from strdup)
# -- $ra (at 120($sp))
addiu $sp,$sp,-124
sw $ra,120($sp)
# there is room on the stack to save an s-reg. Save it at 116($sp)
  

# your s-reg will be used to hold newstr (the string returned from strdup)
Lnextstr:
# retval = InputDialogString(ipprompt,buffer,100)

# if (retval <= 0) goto Ldone;


# char *newstr = strdup(buffer);

# save newstr in your s-reg


# report original string again
# MessageDialogString(ipprompt2,buffer)


# now resport result string
# MessageDialogString(opprompt,newstr)

# and go around the loop again
b Lnextstr
Ldone:
lw $ra,120($sp)
# insert code to restore your s-reg here


addiu $sp,$sp,124
jr $ra


# this stub for strdup will just return the same string
# (i.e., not copy it). It can be used to check that your
# main program is working
.globl strdup
strdup:
move $v0,$a0
jr $ra

.include "/pub/cs/gboyd/cs270/util.s"

Explanation / Answer

strdup function c code..

char * strdup (char * str){
   //get the length
   int length = strlen(str);
   char *copyStr;

/* allocating memory */
copyStr = (char *) malloc(length);
strcpy(copyStr, str);
return copyStr;
}

Respective MIPS code

strdup(char*):
daddiu $sp,$sp,-64
sd $31,56($sp)
sd $fp,48($sp)
sd $28,40($sp)
move $fp,$sp
lui $28,%hi(%neg(%gp_rel(strdup(char*))))
daddu $28,$28,$25
daddiu $28,$28,%lo(%neg(%gp_rel(strdup(char*))))
sd $4,8($fp)
ld $4,8($fp)
ld $2,%call16(strlen)($28)
move $25,$2
1: jalr $25
nop

sw $2,20($fp)
lw $2,20($fp)
move $4,$2
ld $2,%call16(malloc)($28)
move $25,$2
1: jalr $25
nop

sd $2,24($fp)
ld $5,8($fp)
ld $4,24($fp)
ld $2,%call16(strcpy)($28)
move $25,$2
1: jalr $25
nop

ld $2,24($fp)
move $sp,$fp
ld $31,56($sp)
ld $fp,48($sp)
ld $28,40($sp)
daddiu $sp,$sp,64
j $31
nop

STATEMENT WISE CODE

//char * strdup (char * str){
strdup(char*):
daddiu $sp,$sp,-64
sd $31,56($sp)
sd $fp,48($sp)
sd $28,40($sp)
move $fp,$sp
lui $28,%hi(%neg(%gp_rel(strdup(char*))))
daddu $28,$28,$25
daddiu $28,$28,%lo(%neg(%gp_rel(strdup(char*))))
sd $4,8($fp)
  
//int length = strlen(str);
ld $4,8($fp)
ld $2,%call16(strlen)($28)
move $25,$2
1: jalr $25
nop

sw $2,20($fp)
//char *copyStr;
//copyStr = (char *) malloc(length);
   lw $2,20($fp)
move $4,$2
ld $2,%call16(malloc)($28)
move $25,$2
1: jalr $25
nop

sd $2,24($fp)
//strcpy(copyStr, str);
ld $5,8($fp)
ld $4,24($fp)
ld $2,%call16(strcpy)($28)
move $25,$2
1: jalr $25
nop
//return copyStr;
   ld $2,24($fp)
move $sp,$fp
ld $31,56($sp)
ld $fp,48($sp)
ld $28,40($sp)
daddiu $sp,$sp,64
j $31
nop
}

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