In: Computer Science
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. Your program should accept input from the keyboard (use the MIPS syscall functionality for I/O). READ THE INSTRUCTIONS IN THE BOOK CAREFULLY. The program is to accept text input from a keyboard (i.e. a string), and translate it to its numeric interpretation. E.g., the string "24" (which has a 2 byte ASCII character representation) should be translated to its decimal value equivalent 24. NO POPUP WINDOWS FOR INPUT - use service code 8 and NOT 54!
Copyable code:
.data
string_ascii: .asciiz ""
prompt1: .asciiz "Enter an ASCII number string: "
.text
main:
#prompt the user
li $v0, 4
la $a0, prompt1
syscall
#read the ASCII digit string from the user
li $v0,8 #take in input
#$a0 holds the address of a nullterminated string
la $a0, string_ascii #load the byte space into address
li $a1, 20 #laod the byte space for string
syscall
#jump and link to the label convertStringToInt
jal convertStringToInt
#store the return value $vo into $a0
move $a1, $v0
#print $v0 result
li $v0,4
syscall
#stop program
li $v0,10
syscall
#Converts the ASCII decimal string into integer
convertStringToInt:
# initialize $v0 = 0
li $v0, 0
# assign the value '0' to $t6
li $t6, 0x30
# assign the value '9' to $t7
li $t7, 0x39
#move the the address of a nullterminated string into $t0
move $t0, $a0
#load $t1 with the character of the string
lb $t1, ($t0)
L1:
#check the character is digit or not
#if the charcater is less than 0
#or greater than 9, then goto the label notDigit
blt $t1, $t6, notDigit
bgt $t1, $t7, notDigit
#convert char to integer by substracting $t1 and $t6
subu $t1, $t1, $t6
#multiple the value of v0 by 10
mul $v0, $v0, 10
# and $v0 and $t1
add $v0, $v0, $t1
#increment the $t0 by 1 to get the next character
addiu $t0, $t0, 1
#load $t1 with the character of the string
lb $t1, ($t0)
#branch to the until end of the string
bne $t1, $0, L1
# return integer value
jr $ra
notDigit:
#make the value of $v0 as -1
#if the input string has non-digit character
li $v0, -1
# return -1 in $v0
jr $ra
Sample output 1:
Sample output 2:
Let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions. Thank You! ===========================================================================