.text sprintf:addi $sp, $sp, -40 sw $31, 36($sp) sw $16, 0($sp) sw $17, 4($sp) sw $18, 8($sp) sw $19, 12($sp) sw $20, 16($sp) sw $21, 20($sp) sw $22, 24($sp) sw $23, 28($sp) li $16, '%' # % li $17, 'd' # d li $18, 'x' # x li $19, 'c' # c li $20, 's' # s addi $21, $sp, 48 # holds the starting point of variables lw $22, 40($sp) # buffer lw $23, 44($sp) # format string lb $8, 0($23) # load the first letter of string li $9, 0 # keeps the running character total whole_s:beqz $8, end_spf addi $23, $23, 1 beq $8, $16, s_field # % field check increm: sb $8, 0($22) addi $22, $22, 1 addi $9, $9, 1 again: lb $8, 0($23) b whole_s s_field:lb $8, 0($23) addi $23, $23, 1 beq $8, $16, increm beq $8, $17, decimal beq $8, $18, hexadec beq $8, $19, charact beq $8, $20, stringp b increm decimal:lw $4, 0($21) addi $21, $21, 4 bgez $4, r_dec li $10, '-' # put - sign if negative sb $10,0($22) addi $9, $9, 1 addi $22, $22, 1 subu $4, $0, $4 r_dec: move $5, $22 sw $9, 32($sp) jal putdec sub $10, $2, $22 move $22, $2 lw $9, 32($sp) add $9, $9, $10 b again hexadec:lw $4, 0($21) addi $21, $21, 4 move $5, $22 sw $9, 32($sp) jal puthex sub $10, $2, $22 move $22, $2 lw $9, 32($sp) add $9, $9, $10 b again charact:lw $8, 0($21) addi $21, $21, 4 b increm stringp:lw $4, 0($21) addi $21, $21, 4 move $5, $22 sw $9, 32($sp) jal putstr lw $9, 32($sp) add $22, $22, $2 add $9, $9, $2 b again end_spf:sb $0,0($22) # put null pointer at the end of buffer move $2, $9 lw $31, 36($sp) lw $16, 0($sp) lw $17, 4($sp) lw $18, 8($sp) lw $19, 12($sp) lw $20, 16($sp) lw $21, 20($sp) lw $22, 24($sp) lw $23, 28($sp) addi $sp, $sp, 40 jr $31 putdec: addi $sp, $sp, -8 sw $31, 0($sp) remu $8, $4, 10 bgez $8, next next: addi $8, $8, '0' divu $4, $4, 10 beqz $4, end_dec # end when = to zero sw $8, 4($sp) jal putdec move $5, $2 # $5 holds current buffer address lw $8, 4($sp) end_dec:sb $8, 0($5) addi $2, $5, 1 lw $31, 0($sp) addi $sp, $sp, 8 jr $31 puthex: addi $sp, $sp, -8 sw $31, 0($sp) andi $8, $4, 0xf # get the last 4 digits li $10, 10 bge $8, $10, alpha # for 10-15 put in a-f addi $8, $8, '0' b n_alpha alpha: addi $8, $8, -10 addi $8, $8, 'a' n_alpha:srl $4, $4, 4 # shift right by 4 beqz $4, end_hex # end when = to zero sw $8, 4($sp) jal puthex move $5, $2 # $5 holds current buffer address lw $8, 4($sp) end_hex:sb $8, 0($5) addi $2, $5, 1 lw $31, 0($sp) addi $sp, $sp, 8 jr $31 putstr: lb $8, 0($4) li $9, 0 re_loop:beqz $8, end_str sb $8, 0($5) addi $4, $4, 1 addi $5, $5, 1 addi $9, $9, 1 lb $8, 0($4) b re_loop end_str:move $2, $9 jr $31