sprintf: add $v0, $0, $0 #Empty out return value add $t0, $a0, $0 #Give t0 a0's address add $t1, $a1, $0 #Give t1 a1's address add $t6, $0, $0 #Empty out t6 to hold position in other args sloop: lb $t2, 0($t1) #load a char from the format string addi $t7, $0, '%' #Give t7 the % char beq $t2, $t7, spercent #Check if the char from the format string is a % snope: sb $t2, 0($t0) #It wasn't, so send the char to the output string addi $t0, $t0, 1 #Move the output addy to make room for the next char addi $t1, $t1, 1 #Move the format string view point. j sloop #Get the next character. spercent: addi $t1, $t1, 1 #Move the format string view point to the next char lb $t2, 0($t1) #load a char from the format string beq $t2, $t7, snope #Check if the char from the format string is a % addi $sp, $sp, -4 #Make room on the stack sw $ra, 0($sp) #Store the return value jal splacement #Call splacement lw $ra, 0($sp) #restore the return value addi $sp, $sp, 4 #Give back the room on the stack addi $t7, $0, 'd' #Give t7 the d char beq $t2, $t7, sdigit #Check if the char from the format string is a d addi $t7, $0, 's' #Give t7 the s char beq $t2, $t7, sstring #Check if the char from the format string is an s addi $t7, $0, 'c' #Give t7 the c char beq $t2, $t7, schar #Check if the char from the format string is a c # addi $t7, $0, 'x' #Give t7 the x char # beq $t2, $t7, shex #Check if the char from the format string is an x serr: addi $t0, $t0, 1 #move the output addy to make room for the next char addi $t1, $t1, 1 #Move the format string view point to the next char j sloop #Go back to the loop. sdigit: addi $sp, $sp, -4 #Make room on the stack sw $ra, 0($sp) #Store the return address jal sdigita #Call sdigita lw $ra, 0($sp) #Restore the return address addi $sp, $sp, 4 #Give back room on the stack j serr #jump up to serr for sloop sdigita: addi $sp, $sp, -8 #Make room on the stack sw $ra, 0($sp) #Store the return address lw $t3, 0($t3) #Load the word (number) stored at t3 into t3 rem $t4, $t3, 10 #Get the remainder of t3/10 addi $t4, $t4, '0' #Make t4 the ascii value for t4 div $t3, $t3, 10 #make t3 /= 10 beqz $t3, soneterm #Check if t3 is 0 sw $t4, 4($sp) #Store t4 onto the stack jal sdigita #Recursive call lw $t4, 4($sp) #Retrieve t4 from the stack sb $t4, 0($t0) #Store t4 out to the output string addi $t0, $t0, 1 #Move the output addy to make room for the next char addi $t1, $t1, 1 #Move the format string view point to the next char lw $ra, 0($sp) #Restore the return address addi $sp, $sp, 8 #Give back room on the stack jr $ra #return to caller soneterm: sb $t4, 0($t0) #Store t4 out to the output string lw $ra, 0($sp) #Get the return value back off the stack addi $sp, $sp, 8 #Give back room on the stack jr $ra #Return to caller (and to serr) sstring: addi $sp, $sp, 4 #Make room on the stack sw $ra, 0($sp) #Store the return value jal sstringa #Jump down to sstringa lw $ra, 0($sp) #Restore the return value from the stack j serr #Go back to serr for completion sstringa: lb $t2, 0($t3) #Store a char off of the string (t3) into t2 strloop: beqz $t2, strend #Check if the char is a null (end of string) sb $t2, 0($t0) #Store the char to the output string addi $t3, $t3, 1 #Move the string view point to the next char addi $t0, $t0, 1 #Move the output addy to make room for the next char lb $t2, 0($t3) #Store a char off of the string (t3) into t2 j strloop #Check the char and continue strend: jr $ra #No more, return for serr schar: lb $t3, 0($t3) #Load the word (character) stored at t3 into t3 sb $t3, 0($t0) #Put the character out to the outpust string j serr #Go back to serr splacement: beq $t6, $0, splaca #t6 is the argument counter, see if on arg a2 addi $t7, $0, 1 #clear out t7 to 1 beq $t6, $t7, splacb #t6 is the arguement counter, see if on arg a3 splaca: add $t3, $a2, $0 #Give t3 the address a2 is holding jr $ra #return to caller splacb: add $t3, $a3, $0 #Give t3 the address a3 is holding jr $ra #return to caller