Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sprintf1:
- li $v0,1 # return: length of string is 1
- jr $ra
- sprintf:
- move $s0, $a0
- move $s1, $a1
- move $s2, $a2
- move $s3, $a3
- li $s4, 0
- j sprintfloop
- sprintfloop:
- lb $t0, 0($s1)
- addi $s1, $s1, 1
- beq $t0, '%', checker
- sb $t0, 0($s0)
- addi $s0, $s0, 1
- addi $s4, $s4, 1
- bne $t0, $0, sprintfloop
- jr $ra
- checker:
- lb $t0, 0($s1)
- beq $t0, 'd', printd
- beq $t0, 'x', sprintx
- beq $t0, 's', strcpy
- #beq $t0, 'c' sputc
- #beq $t0, '%', sputc
- j sprintfloop
- #######
- # Helper functions
- # void printd(int n)
- #printd:
- # addi $sp,$sp,-12 #
- # sw $ra,8($sp) #
- # sw $s0,4($sp) # save the old value of $s0
- # sw $s1,0($sp) # save the old value of $s1
- #
- # li $s1,10
- # rem $s0,$a0,$s1 # value % 10
- # addi $s0,$s0,'0' # convert that to a digit
- #
- # div $a0,$a0,$s1 # value / 10
- #
- # beqz $a0,onedig # is it non-zero?
- # jal printd # if so, print it
- #onedig: move $a0,$s0 # print the digit we built
- # li $v0,11 # print_char
- # syscall
- # lw $s1,0($sp)
- # lw $s0,4($sp)
- # lw $ra,8($sp)
- # addi $sp,$sp,12
- # jr $ra
- # end of printd
- printd:
- addi $sp, $sp, -8 #
- sw $ra, 0($sp) #
- sw $s0, 4($sp) # save the old value of $s0
- li $t9, 10 # constant, used for div and rem
- rem $s0, $s2, $t9 # value % 10
- addi $s0, $s0, '0' # convert that to a digit
- div $a0, $a0, $t9 # value / 10
- beqz $a0, sonedig # is it non-zero?
- jal printd # if so, print it
- sonedig:
- li $v0, 11 # print_char
- move $a0, $s0
- syscall
- lw $s0, 4($sp)
- lw $ra, 0($sp)
- addi $sp, $sp, 8
- # move $s2, $s3
- jr $ra
- strcpy:
- lb $t0, 0($s1)
- addi $s1, $s1, 1
- sb $t0, 0($s0)
- addi $s0, $s0, 1
- bne $t0, $0, strcpy
- jr $ra
- # void printx(int value)
- #
- sprintx:
- addi $sp, $sp, -8 #
- sw $ra, 0($sp) #
- sw $s0, 4($sp) # save the old value of $s0
- li $t9, 16 # constant, used for div and rem
- rem $s0, $a0, $t9 # value % 16
- addi $s0, $s0, '0' # convert that to a digit
- div $a0, $a0, $t9 # value / 16
- beqz $a0, xonedig # is it non-zero?
- jal sprintx # if so, print it
- xonedig: li $v0, 11 # print_char
- move $a0, $s0
- syscall
- lw $s0, 4($sp)
- lw $ra, 0($sp)
- addi $sp, $sp, 8
- jr $ra
- # void bzero(char *buf, int n)
- bzero: beq $a1,$0,bzexit # while(n>0) {
- sb $0,0($a0) # *s = 0
- addi $a0,$a0,1 # s++
- addi $a1,$a1,-1 # n--
- j bzero # }
- bzexit: jr $ra
- # end of bzero
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement