Skip to content

Commit

Permalink
Comprehensive cleanup in stdlib
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Johnson <srj15@case.edu>
  • Loading branch information
Stephen Johnson committed Apr 24, 2009
1 parent f76c2d6 commit e1ce8b1
Showing 1 changed file with 139 additions and 187 deletions.
326 changes: 139 additions & 187 deletions stdlib.s
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -98,29 +98,31 @@ end_read:
# msg_addr = address of the msg # msg_addr = address of the msg
print: print:
{ {
write_again: @outchar = $t3
lbu $t3 0($a0) write_again:
addi $a0 $a0 1 lbu @outchar 0($a0)
beqz $t3 end_write addi $a0 $a0 1
_write_char $t3 beqz @outchar end_write
b write_again _write_char @outchar
end_write: b write_again
end_write:
return return
} }


# print msg_addr # print msg_addr
# msg_addr = address of the msg # msg_addr = address of the msg
println: println:
{ {
write_again: @out_char = $t3
lbu $t3 0($a0) write_again:
addi $a0 $a0 1 lbu @out_char 0($a0)
beqz $t3 end_write addi $a0 $a0 1
_write_char $t3 beqz @out_char end_write
b write_again _write_char @out_char
end_write: b write_again
addi $t3 $zero 10 end_write:
_write_char $t3 li @out_char 10
_write_char @out_char
return return
} }


Expand All @@ -131,54 +133,55 @@ p_int_buf: .space 11
.text .text
print_int: print_int:
{ {
#t0-t1: used by _write_char @saved = $t2
#t2: modifiedcopy of variable @out_char = $t3
#t3: character to write or store in buffer @ten = $t4
#t4: the number 10 @bufferpos_1 = $t5
#t5-t6: buffer positons @bufferpos_2 = $t6
#t7: negativity @neg_sign = $t7
add $t2 $a0 $zero #copy integer to t2
addi $t4 $zero 10 #put 10 in t4 to use in division later add @saved $a0 $zero #copy integer to t2
la $t5 p_int_buf #init buffer in t5 li @ten 10
add $t6 $t5 $zero #copy to t6 la @bufferpos_1 p_int_buf #init buffer in t5
addu @bufferpos_1 @bufferpos_1 $zero #copy to t6


add $t7 $zero $zero #init negative bit to zero li @neg_sign 0 #init negative bit to zero


bnez $t2 do_digit #check for zero bnez @saved do_digit #check for zero
addi $t2 $t2 48 li @saved 48
_write_char $t2 _write_char @saved
return return


bgez $t2 do_digit #set negative bit if necessary, otherwise skip bgez @saved do_digit #set negative bit if necessary, otherwise skip
addi $t7 $zero 1 addi @neg_sign $zero 1


sub $t2 $zero $a0 #make number positive sub @saved $zero $a0 #make number positive
add $a0 $t2 $zero add $a0 @saved $zero


do_digit: do_digit:
#This loop actually stores the number characters *backwards.*. #This loop actually stores the number characters *backwards.*.
#It is faster that way. #It is faster that way.
blez $t2 end_digits #stop when t2 == 0 blez @saved end_digits #stop when t2 == 0
div $t2 $t4 #t2 / 10 div @saved @ten #t2 / 10
mfhi $t3 #t3 = t2 % 10 mfhi @out_char #t3 = t2 % 10
mflo $t2 #t2 = t2 / 10 mflo @saved #t2 = t2 / 10
addi $t3 $t3 48 #t3 = t3 + 48 (48 = '0') addi @out_char @out_char 48 #t3 = t3 + 48 (48 = '0')
sb $t3 0($t6) #store t3 in buffer sb @out_char 0(@bufferpos_2) #store t3 in buffer
addi $t6 $t6 1 #move forward in buffer addi @bufferpos_2 @bufferpos_2 1 #move forward in buffer
b do_digit b do_digit
end_digits: end_digits:
sb $zero 0($t6) #terminate the string just in case sb $zero 0(@bufferpos_2) #terminate the string just in case


beqz $t7 write_again #print negative sign if necessary beqz @neg_sign write_again #print negative sign if necessary
addi $t3 $zero 45 #45 = '-' li @out_char 45 #45 = '-'
_write_char $t3 _write_char @out_char
write_again: write_again:
addi $t6 $t6 -1 #step backwards until t6 <= t5 addi @bufferpos_2 @bufferpos_2 -1 #step backwards until t6 <= t5
lbu $t3 0($t6) lbu @out_char 0(@bufferpos_2)
blt $t6 $t5 end_write blt @bufferpos_2 @bufferpos_1 end_write
_write_char $t3 _write_char @out_char
b write_again b write_again
end_write: end_write:


return return
} }
Expand Down Expand Up @@ -248,119 +251,92 @@ printf:
#s3: format code identifier ('d', 'c', 'x', etc) #s3: format code identifier ('d', 'c', 'x', etc)
#s4: current argument number #s4: current argument number


add $s0 $a0 $zero @buffer = $s0
addi $s2 $zero 37 #37 = '%' stored here 'permanently' for speed @this_char = $s1
addi $s4 $zero 1 @percent = $s2
@fmt = $s3
@argnum = $s4


write_again: add @buffer $a0 $zero
#read, move pointer, check for zero addi @percent $zero 37 #37 = '%' stored here 'permanently' for speed
lbu $s1 0($s0) addi @argnum $zero 1
addi $s0 $s0 1
beqz $s1 end_write


beq $s1 $s2 format_pattern #if not %: write_again:
_write_char $s1 # write char verbatim #read, move pointer, check for zero
b end_format_pattern # read again lbu @this_char 0(@buffer)
format_pattern: #else: addi @buffer @buffer 1
lbu $s1 0($s0) # read format code beqz @this_char end_write
addi $s0 $s0 1 # bump pointer


beqz $s1 _zero #end of string beq @this_char @percent format_pattern #if not %:
addi $s3 $zero 100 #d _write_char @this_char # write char verbatim
beq $s1 $s3 _dec b end_format_pattern # read again
addi $s3 $zero 120 #x format_pattern: #else:
beq $s1 $s3 _lhex lbu @this_char 0(@buffer) # read format code
addi $s3 $zero 99 #c addi @buffer @buffer 1 # bump pointer
beq $s1 $s3 _char
addi $s3 $zero 115 #s beqz @this_char _zero #end of string
beq $s1 $s3 _str li @fmt 100 #d
b _other #everything else beq @this_char @fmt _dec

li @fmt 120 #x
#formula for these: load arg, call function, bump arg number, loop beq @this_char @fmt _lhex
_dec: li @fmt 99 #c
load_arg_by_reg $s4 $a0 beq @this_char @fmt _char
exec print_int li @fmt 115 #s
addi $s4 $s4 1 beq @this_char @fmt _str
b end_format_pattern b _other #everything else
_lhex:
load_arg_by_reg $s4 $a0 #formula for these: load arg, call function, bump arg number, loop
call print_hex _dec:
addi $s4 $s4 1 load_arg_by_reg @argnum $a0
b end_format_pattern exec print_int
_char: addi @argnum @argnum 1
load_arg_by_reg $s4 $a0 b end_format_pattern
_write_char $a0 _lhex:
addi $s4 $s4 1 load_arg_by_reg @argnum $a0
b end_format_pattern call print_hex
_str: addi @argnum @argnum 1
load_arg_by_reg $s4 $a0 b end_format_pattern
exec print _char:
addi $s4 $s4 1 load_arg_by_reg @argnum $a0
b end_format_pattern _write_char $a0
_other: addi @argnum @argnum 1
_write_char $s2 b end_format_pattern
_write_char $s1 _str:
b write_again load_arg_by_reg @argnum $a0
_zero: exec print
_write_char $s2 addi @argnum @argnum 1
b end_write b end_format_pattern
end_format_pattern: _other:
b write_again _write_char @percent
end_write: _write_char @this_char
b write_again
_zero:
_write_char @percent
b end_write
end_format_pattern:
b write_again
end_write:
return return
} }


.text
# # print_arr array_addr size
# # array_addr = the address of the array
# # size = the size of the array
# print_array:
# {
# add $s0 $a0 $0
# add $s1 $a1 $0
# la $a0 sbracket_l
# exec print
# add $s2 $0 $0
# beq $s1 $0 end_loop
# loop:
# add $a0 $s0 $0
# add $a1 $s2 $0
# exec get
# add $a0 $v0 $0
# exec print_int
#
# add $s2 $s2 1
# beq $s2 $s1 end_loop
# la $a0 comma
# exec print
#
# j loop
# end_loop:
# la $a0 sbracket_r
# exec println
# return
# .data
# comma: .asciiz ", "
# sbracket_l: .asciiz "["
# sbracket_r: .asciiz "]"
# }



.text .text
# print_hex_digit digit # print_hex_digit digit
# digit = contains the digit your want to print in first nibble # digit = contains the digit your want to print in first nibble
print_hex_digit: print_hex_digit:
{ {
andi $s0 $a0 0x000f @this_char = $s0
@const = $s1
andi @this_char $a0 0x000f
#if $a0 >= 10 #if $a0 >= 10
li $s1 10 li @const 10
bge $s0 $s1 bigger_than_10 bge @this_char @const bigger_than_10
li $s1 0x30 # 0 in ascii li @const 0x30 # 0 in ascii
j print_digit j print_digit
bigger_than_10: bigger_than_10:
li $s1 0x57 # a - 10 in ascii li @const 0x57 # a - 10 in ascii
print_digit: print_digit:
add $a0 $s0 $s1 add $a0 @this_char @const
_write_char $a0 _write_char $a0
return return
} }
Expand All @@ -369,21 +345,24 @@ print_digit:
# reg = the word you want to print # reg = the word you want to print
print_hex: print_hex:
{ {
add $s0 $a0 $0 @this_char = $s0
@temp = $s1

add @this_char $a0 $0
la $a0 ox la $a0 ox
call print call print
add $s1 $0 8 add @temp $0 8


loop: loop:
beq $s1 $0 loop_end beq @temp $0 loop_end


lui $a0 0xf000 lui $a0 0xf000
and $a0 $s0 $a0 and $a0 @this_char $a0
srl $a0 $a0 28 srl $a0 $a0 28
call print_hex_digit call print_hex_digit
sll $s0 $s0 4 sll @this_char @this_char 4


sub $s1 $s1 1 sub @temp @temp 1
j loop j loop
loop_end: loop_end:
return return
Expand All @@ -400,33 +379,6 @@ println_hex:
return return
} }


.text
# # put array_addr index reg --> Null
# # array_addr = the address of the array
# # index = what index
# # reg = the register you want to save
# put:
# {
# add $a3 $a1 $0
# mul $a3 $a3 4
# addu $a3 $a3 $a0
# sw $a2 0($a3)
# return
# }

.text
# # get array_addr index --> $v0 = the value from the array
# # array_addr = the address of the array
# # index = what index
# get:
# {
# add $a3 $a1 $0
# mul $a3 $a3 4
# addu $a3 $a3 $a0
# lw $v0 0($a3)
# return
# }

# print_hcb (hcb_addr) --> Null # print_hcb (hcb_addr) --> Null
print_hcb: print_hcb:
{ {
Expand Down

0 comments on commit e1ce8b1

Please sign in to comment.