String to Integer Conversion in PLP Assembly
.org 0x10000000
li $sp,0x10fffffc
main:
li $s0,';' # delimiter to use
li $s1,10 # load s1 with 10 for doing multiplications by 10
li $a0,0 # a0 will contain the converted number, initialize to zero
li $a1,0 # a0 will contain 1 if there was a bad character input, set to zero
read:
li $t0,0xf0000000 # load UART command register address in t0
li $t1,2 # load a 2 to send a clear status command to the UART
sw $t1,0($t0) # send clear status command to UART
nop
inwait:
lw $t1,4($t0) # load current status register from UART
andi $t1,$t1,2 # test the second bit (ready)
beq $t1,$0,inwait # if there is no data, keep waiting
nop
lw $t1,8($t0) # load received char in t1
beq $t1,$s0,send # if the read character was a ';', send the result
nop
addiu $t1,$t1,-48 # convert from ascii to integer
slt $t2,$t1,$0 # set t2 to 1 if conversion<0
bne $t2,$0,error # if the it was <0, set error
nop
slt $t2,$t1,$s1 # set t2 to 1 if conversion<10
beq $t2,$0,error # if the it was >=10, set error
nop
mullo $a0,$a0,$s1 # multiply old number by 10
addu $a0,$a0,$t1 # add current digit to number
j read # read another character
nop
error:
addiu $a1,$0,1 # set a1 to 1 to indicate error
j read # read another character
nop
send:
call project3_print # print the converted number
nop
j main # restart program
nop