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