Write a program in MIPS assembly language to convert an ASCI
Write a program in MIPS assembly language to convert an ASCII number string containing positive and negative integer decimal strings, to an integer. Your program should expect register $a0 to hold the address of a nullterminated string containing some combination of the digits 0 through 9. Your program should compute the integer value equivalent to this string of digits, then place the number in register $v0. If a non-digit character appears anywhere in the string, your program should stop with the value 1 in register $v0. For example, if register $a0 points to a sequence of three bytes 50ten, 52ten, 0ten (the nullterminated string “24”), then when the program stops, register $v0 should contain the value 24ten
Solution
.data
#ASCII string
asciiStr: .asciiz \"+123\"
.text
main:
#call procedure to convert
jal procConvert
move $a1, $v0
#print $v0 result
li $v0,1
syscall
#stop program
li $v0,10
syscall
procConvert:
la $a0, asciiStr #load ascii string
#load hexadecimal value for \'+\' in $t5
li $t5, 0x2B
#load hexadecimal value for \'0\' in $t6
li $t6, 0x30
#load hexadecimal value of \'9\' in $t7
li $t7, 0x39
#pointer to the asciiStr
add $t0, $0, $a0
#get the sign of the ascii number
lbu $t1, 0($t0)
#store the sign of the ascii number
add $t3, $t1, $0
#move to the next character
addiu $t0, $t0, 1
#load the character
lbu $t1, 0($t0)
#initialize $v0
li $v0, 0
#loop to convert asciiStr to decimal Str
REPEATCONVERSION:
#check if charcter is less than \'0\'
slt $t4, $t1, $t6
#if so, then jump to ExitLoop
bne $t4, $0, ExitLoop
#check if character is greater than \'9\'
slt $t4, $t1, $t7
#if so, then jump to ExitLoop
beq $t4, $0, ExitLoop
#convert character to int
subu $t1, $t1, $t6
#$v0=$v0*10
mul $v0, $v0, 10
#now add the int to $v0
add $v0, $v0, $t1
#move to the next character in the asciiStr
addiu $t0, $t0, 1
#Get the next character
lbu $t1, 0($t0)
#check chracter is not null.
#If so, repeat the conversion process
bne $t1, $0, REPEATCONVERSION
#check asciiStr is positive. If not jump to NOTPOSITIVE
bne $t3, $t5, NOTPOSITIVE
#stop program
jr $ra
#for negative asciiStr
NOTPOSITIVE:
#convert $v0 to negative using $v0=$0-$v0
sub $v0, $0, $v0
#jump to program end
jr $ra
#exit loop for null string
ExitLoop:
li $v0, -1
jr $ra


