Question: I writed string to float in MIPS. But there is error. but, i don't know...... where is problem? .data intro : .asciiz Enter the number
I writed string to float in MIPS. But there is error. but, i don't know......
where is problem?
.data
intro : .asciiz "\Enter the number : "
out_string: .asciiz "\Input string : "
out_reals: .asciiz " Floating point number detected "
out_float: .asciiz " Floatig point : "
check:.asciiz "."
input: .space 20
.text
.globl main
main:
li $v0,4
la $a0, intro #intro enter
syscall
li $a1,20
li $v0, 8
la $a0, input #read a string into a0
syscall
jal str2float #str -> int
move $s0,$v0
li $v0,4
la $a0,out_float
syscall
li $v0,2
move $a0,$s0
syscall
str2float:
li $t6,0x20
li $t7,0x30
lb $t0,0($a0) # load the first byte into $t1
beq $t0,'-',negint # if sign is -,goto negint
beq $t0,'+',posint # if sign is +,goto posint
j convert
negint:
li $t1,1 # set flag $t1 to 1(represents negative)
add $a0,$a0,1 # goto next ASCII character
j convert # goto convert
posint:
li $t1,0 # set flag $t1 to 0(represents positive)
add $a0,$a0,1 # goto next ASCII character
convert:
li $s0,0 # sum=0
loop:
lb $t0,0($a0) # load the ASCII character into $t1
beq $t0,10,exitloop #if end of string space came
blt $t0,$t6,fail # if $t1 is a non-digit character,return -1
bgt $t0,$t7,fail
sub $t0,$t0,48 # convert ASCII digit to decimal digit
mul $s0,$s0,10 # multiply the previous sum with 10 and
add $s0,$s0,$t0 # the converted digit to sum
add $a0,$a0,1 # goto next ASCII character
j loop # goto loop
exitloop:
beq $t1,0,copy # if sign is +, goto copy
neg $s0,$s0 # if the sign is -, take negation of FP
copy:
move $v0,$s0 # store the converted FP into $v0
jr $ra
fail:
li $v0,-1
Plz change it ....
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
