Write a program in MIPS assembly language to convert an ASCII number siring cont
ID: 3664977 • Letter: W
Question
Write a program in MIPS assembly language to convert an ASCII number siring containing positive and negative integer decimal strings, to an integer. Your program should expect register $a0 to hold the address of a null terminated 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 slop with the value -1 in register $v0. For example, if register $a0 points to a sequence of three bytes 50tcn. 52ten, 0tcn (the null terminated siring"24"), then when the program stops, register $v0 should contain the value24 _tan.Explanation / Answer
main: # convert string to integer li $t6, 0x00 # $t6 = 0 li $t7, 0x09 # $t7 = 9 li $v0, 0x00 # initialize $v0 = 0 move $t0, $a0 # $t0 = pointer to string lb $t1, ($t0) # load $t1 = digit character loop: blt $t1, $t6, Done # char < 0 bgt $t1, $t7, Done # char > 9 subu $t1, $t1, $t6 # convert char to int mul $v0, $v0, 10 # multiply by 10 add $v0, $v0, $t1 # $v0 = $v0 * 10 + digit addiu $t0, $t0, 1 # point to next char lb $t1, ($t0) # load $t1 = next digit bne $t1, $0, loop # repeat if not end of string jr $ra # return integer Done: #exit program if string not a number li $v0, -1 # return -1 in $v0 jr $raRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.