## On my honor:
##
## – I have not discussed the C language code in my program with
## anyone other than my instructor or the teaching assistants
## assigned to this course.
##
## – I have not used C language code obtained from another student,
## the Internet, or any other unauthorized source, either modified
## or unmodified.
##
## – If any C language code or documentation used in my program
## was obtained from an authorized source, such as a text book or
## course notes, that has been clearly noted with a proper citation
## in the comments of my program.
##
## – I have not designed this program in such a way as to defeat or
## interfere with the normal operation of the grading code.
##
##
##
#############################################################
# This data block contains a ‘digits’ memory allocation
# that you can use to store any strings that you convert
# from integers. Remember to either clear it after use or
# null-terminate the strings that you save here!
#############################################################
.data
digits: .space 10
.text
.globl PROC_COUNT_FORMAT_SPECIFIERS
.globl PROC_CONVERT_INT_TO_STRING
.globl PROC_SPRINTF
.globl PROC_FIND_LENGTH_STRING
.globl PROC_FIND_NUM_DIGITS
.globl PROC_REVERSE_STRING
.globl PROC_CLEAR_DIGITS
#############################################################
# Given the address of a string, count the number of format
# specifiers that appear in that string
#
# Pre: $a0 contains the address of the string to evaluate
# Post: $v0 contains the number of format specifiers that
# were found in the input string
#############################################################
PROC_COUNT_FORMAT_SPECIFIERS:
# add your solution here
# return
jr $ra
#############################################################
# 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
# return
jr $ra
#############################################################
# Given the address of a string and a string which may or
# may not include format specifiers, write a formatted
# version of the string into the output register $v0
#
# Pre: $a0 contains the address of the string dest, where
# the final string will be written into
# $a1 contains the address of a format string, which
# may or may not include format specifiers, and which
# details the required final state of the output
# string
# $a2 contains an unsigned integer or the address of
# a string, or it is empty
# $a3 contains an unsigned integer or the address of
# a string, or it is empty
# Post: $a0 contains the address of the formatted dest,
# including null terminator
# $v0 contains the length of the string written into
# dest, not including null terminator
#############################################################
PROC_SPRINTF:
# add your solution here
# return
jr $ra
#############################################################
# This procedure will find the length of a provided string
# by counting characters until the null terminator is
# reached
#
# Pre: $a0 contains the string to evaluate
# Post: $v0 contains the length of the string
#############################################################
PROC_FIND_LENGTH_STRING:
# prologue
subi $sp, $sp, 4
sw $ra, 0($sp)
# function body
move $t2, $a0 # $t2 is the current char we’re pointing to
li $t4, 0 # $t4 is my length counter for my_string
# Loop until we find the null terminator
LOOP_FIND_NULL_BYTE_LENGTH:
lb $t1, 0($t2) # $t1 is the character loaded from offset $t2
beq $t1, $zero, FOUND_NULL_BYTE_LENGTH
addi $t4, $t4, 1 # increment length counter
addi $t2, $t2, 1 # increment character pointer
j LOOP_FIND_NULL_BYTE_LENGTH
FOUND_NULL_BYTE_LENGTH:
# at this point, $t2 is pointed at \0
move $v0, $t4
# epilogue
lw $ra, 0($sp)
addi $sp, $sp, 4
# return
jr $ra
#############################################################
# 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
subi $sp, $sp, 4
sw $ra, 0($sp)
# function body
li $t0, 10 # load a 10 into $t0 for the division
li $t5, 0 # $t5 will hold the counter for number of digits
move $t6, $a0 # $t6 will hold the result of the iterative division
NUM_DIGITS_LOOP:
divu $t6, $t0 # divide the number by 10
addi $t5, $t5, 1
mflo $t6 # move quotient back into $t6
beq $t6, $zero, FOUND_NUM_DIGITS # if the quotient was 0, $t5 stores the number of digits
j NUM_DIGITS_LOOP
FOUND_NUM_DIGITS:
move $v0, $t5 # copy the number of digits $t5 into $v0 to return
# epilogue
lw $ra, 0($sp)
addi $sp, $sp, 4
# 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
subi $sp, $sp, 4
sw $ra, 0($sp)
# function body
move $t0, $a0 # move the pointer to the first char into $t0
move $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 $t9
lb $t8, 0($t0) # load the $t0 position char into $t8
sb $t8, 0($t2) # write the begin char into $t2 position
sb $t9, 0($t0) # write the end char into $t0 position
# increment and decrement the pointers
addi $t0, $t0, 1
subi $t2, $t2, 1
ble $t2, $t0, END_OF_REVERSE_LOOP
j LOOP_REVERSE
END_OF_REVERSE_LOOP:
# epilogue
lw $ra, 0($sp)
addi $sp, $sp, 4
# return
jr $ra
#############################################################
# This procedure will clear the contents of the ‘digits’
# memory allocation that is defined in the data block of
# this file.
#
# Pre: None
# Post: The ‘digits’ section of memory is cleared to all 0s
#############################################################
PROC_CLEAR_DIGITS:
# prologue
subi $sp, $sp, 4
sw $ra, 0($sp)
# function body
la $t0, digits # get the address of the digits space
li $t1, 10 # we need to loop 10 times
li $t2, 0 # and the loop counter starts at 0
CLEAR_LOOP:
sb $zero, 0($t0) # write all zeros into #t0
addi $t0, $t0, 1 # move to the next char space
addi $t2, $t2, 1 # increment loop counter
beq $t2, $t1, END_CLEAR # break out of the loop after 10 writes
j CLEAR_LOOP
END_CLEAR:
# epilogue
lw $ra, 0($sp)
addi $sp, $sp, 4
# Return
jr $ra
#############################################################