62.6k views
0 votes
Please implement the following procedure in MIPS 32:############################################################# # Given an integer, convert it into a string## Pre: $a0 contains the integer that will be converted# Post: $v0 contains the address of the newly-created string#############################################################PROC_CONVERT_INT_TO_STRING:# add your solution here# loop div by 10, get remainder# EX: 42 / 10 -> rem = 2# 4 / 10 -> rem = 4# result: 24, reverse string for 42...# returnjr $raI'm given some helper procedures to help implement the above:############################################################# # This procedure will determine the number of digits in the# provided integer input via iterative division by 10.## Pre: $a0 contains the integer to evaluate# Post: $v0 contains the number of digits in that integer#############################################################PROC_FIND_NUM_DIGITS:# prologue# function bodyli $t0, 10 # load a 10 into $t0 for the divisionli $t5, 0 # $t5 will hold the counter for number of digitsmove $t6, $a0 # $t6 will hold the result of the iterative divisionNUM_DIGITS_LOOP:divu $t6, $t0 # divide the number by 10addi $t5, $t5, 1mflo $t6 # move quotient back into $t6beq $t6, $zero, FOUND_NUM_DIGITS # if the quotient was 0, $t5 stores the number of digitsj NUM_DIGITS_LOOPFOUND_NUM_DIGITS:move $v0, $t5 # copy the number of digits $t5 into $v0 to return# epilogue# return jr $ra ############################################################# # This procedure will reverse the characters in a string in-# place when given the addresses of the first and last# characters in the string.## Pre: $a0 contains the address of the first character# $a1 contains the address of the last character# Post: $a0 contains the first character of the reversed# string#############################################################PROC_REVERSE_STRING:# prologue# function body move $t0, $a0 # move the pointer to the first char into $t0move $t2, $a1 # move the pointer to the last char into $t2# Loop until the pointers cross LOOP_REVERSE: lb $t9, 0($t2) # backing up the $t2 position char into $t9lb $t8, 0($t0) # load the $t0 position char into $t8sb $t8, 0($t2) # write the begin char into $t2 positionsb $t9, 0($t0) # write the end char into $t0 position# increment and decrement the pointersaddi $t0, $t0, 1subi $t2, $t2, 1ble $t2, $t0, END_OF_REVERSE_LOOPj LOOP_REVERSEEND_OF_REVERSE_LOOP:# epilogue# return jr $ra

User Marc Eaddy
by
8.0k points

1 Answer

6 votes

Answer:

See Explanation

Step-by-step explanation:

# PROC_CONVERT_INT_TO_STRING

# Given an integer, convert it into a string

# Pre: $a0 contains the integer that will be converted

# Post: $v0 contains the address of the newly-created string

PROC_CONVERT_INT_TO_STRING:

# prologue

addi $sp, $sp, -12 # allocate space on the stack

sw $ra, 8($sp) # store return address on stack

sw $s0, 4($sp) # store $s0 on stack

sw $s1, 0($sp) # store $s1 on stack

# call PROC_FIND_NUM_DIGITS to determine the number of digits in the input integer

move $a0, $a0 # save input integer in $a0

jal PROC_FIND_NUM_DIGITS

move $s0, $v0 # save number of digits in $s0

# allocate memory for the string

li $v0, 9 # system call for sbrk (allocate heap memory)

addi $a0, $s0, 1 # add 1 for null terminator

syscall # allocate memory

move $s1, $v0 # save address of string in $s1

# loop through digits in input integer and convert to characters

move $a0, $a0 # restore input integer in $a0

addi $sp, $sp, -4 # allocate space on the stack

sw $t0, 0($sp) # save $t0 on stack

li $t0, 10 # load a 10 into $t0 for the division

move $t1, $s1 # start writing characters from end of string

LOOP_CONVERT_INT_TO_STRING:

divu $a0, $t0 # divide input integer by 10

mfhi $t2 # get remainder (digit)

addi $t2, $t2, 48 # convert to ASCII character

sb $t2, 0($t1) # store character in string

subi $t1, $t1, 1 # move to next position in string

bne $a0, $zero, LOOP_CONVERT_INT_TO_STRING # loop until quotient is 0

sw $t1, 0($s1) # store null terminator at end of string

# call PROC_REVERSE_STRING to reverse the characters in the string

move $a0, $s1 # start of string

addi $a1, $s1, $s0 # end of string

jal PROC_REVERSE_STRING

# set return value

move $v0, $s1

# epilogue

lw $ra, 8($sp) # restore return address

lw $s0, 4($sp) # restore $s0

lw $s1, 0($sp) # restore $s1

addi $sp, $sp, 12 # deallocate space on the stack

jr $ra # return

User Randomnickname
by
7.6k points