Would like to know the answer to excercise 2.37 in Computer Organization and Des
ID: 673327 • Letter: W
Question
Would like to know the answer to excercise 2.37 in Computer Organization and Design by Patterson and Hennesy.
The question is:
Write a program in MIPS assembly language to convert an ASCII number string containing positive and negative integer decimal strings. $a0 holds the address of a null-terminated string containing digits between 0 and 9. The program should compute the integer value equivalent to the string of digits and then place it in $v0. If a non-digit character appears anywhere in the string, the program should stop with a value of -1 in $v0. For example, if $a0 points to a sequence of 3 bytes (base 10) 50, 52, 0 (the null-terminated string is "24"), then when the program stops, $v0 should contain the value in base ten, 24.
Explanation / Answer
Positive And Negative Integer Decimal strings
MAIN: addi $sp, $sp, 4
sw $ra, ($sp)
add $t6, $0, 0x30 # ‘0’
add $t7, $0, 0x39 # ‘9’
add $s0, $0, $0
add $t0, $a0, $0
LOOP: lb $t1, ($t0)
slt $t2, $t1, $t6
bne $t2, $0, DONE
slt $t2, $t7, $t1
bne $t2, $0, DONE
sub $t1, $t1, $t6
beq $s0, $0, FIRST
mul $s0, $s0, 10
FIRST: add $s0, $s0, $t1
addi $t0, $t0, 1
j LOOP
DONE: add $v0, $s0, $0
lw $ra, ($sp)
addi $sp, $sp, 4
jr $ra
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.