错误的整数输出 Mips
wrong integer output Mips
我在使用这个时得到了错误的输出:
li $v0, 1
我必须将用户输入保存为 ASCII(从 0 到 9),将其转换为整数,然后将其打印为整数。
但是当我的寄存器中有例如 10 时,它会打印 16;当我有 20 时,它打印 32 等等......它开始打印:my_number + 6^(n) 我不知道为什么......
这是我的代码:
.data
num1: .space 32
num2: .space 32
entra: .asciiz "Put your Decimal: "
sale: .asciiz "\nDecimal is: "
.globl __start
.text
__start:
la $a0, entra # print 1st text
li $v0, 4
syscall
la $s4, num1 # space to put the copy of user's word without \n
li $a1, 25 # space to put user's word (number)
li $v0, 8 # take input
la $a0, num2
syscall
li $t1, -1 # counter to know how many times we iterate over EliminarEnter
jal EliminarEnter # this function remove \n char and save it to $t4
jal Funcion # getting the symbol of the char
li $v0, 10
syscall # exit
EliminarEnter:
addi $t1, $t1, 1 # increment counter
lb $t2, 0($a0) # take byte of users word
beq $t2, 10 QuitarEnter
sb $t2, 0($s4) # save the byte in $t4
addi $s4, $s4, 1
addi $a0, $a0, 1 # increment pointer in the word
j EliminarEnter
QuitarEnter:
jr $ra
Funcion:
sub $s4, $s4, $t1 # go to the first memory adress
Bucle:
lb $t2, 0($s4)
beq $t2, $zero Acabar # if char is null '[=11=]' --> End of program
addi $t3, $t2, -48 # -48, t get the symbol of my char
j GuardarIncrementar
GuardarIncrementar:
rol $t4, $t4, 4 # rol positions
add $t4, $t4, $t3 # where I save the decimal number
addi $s4, $s4, 1 # increment adress of my new ASCII word (without '\n')
j Bucle
Acabar:
la $a0, sale # printing 2nd text
li $v0, 4
syscall
add $v1, $zero, $t4
move $v0, $v1
move $a0 , $v0
li $v0, 1 # printing MY NUMBER... THING THAT DOESNT WORK!!!
syscall
jr $ra # exit
你想做小数,所以在GuardarIncrementar
变化:
rol $t4, $t4, 4 # rol positions
进入:
mul $t4, $t4, 10 # multiply by base 10
我认为您正在将每个输入数字转换为 4 位数字,并使用
rol $t4, $t4, 4 # multiply by 16
add $t4, $t4, $t3 # and add the new digit
累加整数值。
这会将每个输入数字放入其自己的 4 位整数中,这意味着您将输入视为以 16 为底(但不处理 A-F)。
在基数 10 中,您需要乘以 10,而不是 16。因此您不能使用单个移位或旋转。
我在使用这个时得到了错误的输出:
li $v0, 1
我必须将用户输入保存为 ASCII(从 0 到 9),将其转换为整数,然后将其打印为整数。
但是当我的寄存器中有例如 10 时,它会打印 16;当我有 20 时,它打印 32 等等......它开始打印:my_number + 6^(n) 我不知道为什么......
这是我的代码:
.data
num1: .space 32
num2: .space 32
entra: .asciiz "Put your Decimal: "
sale: .asciiz "\nDecimal is: "
.globl __start
.text
__start:
la $a0, entra # print 1st text
li $v0, 4
syscall
la $s4, num1 # space to put the copy of user's word without \n
li $a1, 25 # space to put user's word (number)
li $v0, 8 # take input
la $a0, num2
syscall
li $t1, -1 # counter to know how many times we iterate over EliminarEnter
jal EliminarEnter # this function remove \n char and save it to $t4
jal Funcion # getting the symbol of the char
li $v0, 10
syscall # exit
EliminarEnter:
addi $t1, $t1, 1 # increment counter
lb $t2, 0($a0) # take byte of users word
beq $t2, 10 QuitarEnter
sb $t2, 0($s4) # save the byte in $t4
addi $s4, $s4, 1
addi $a0, $a0, 1 # increment pointer in the word
j EliminarEnter
QuitarEnter:
jr $ra
Funcion:
sub $s4, $s4, $t1 # go to the first memory adress
Bucle:
lb $t2, 0($s4)
beq $t2, $zero Acabar # if char is null '[=11=]' --> End of program
addi $t3, $t2, -48 # -48, t get the symbol of my char
j GuardarIncrementar
GuardarIncrementar:
rol $t4, $t4, 4 # rol positions
add $t4, $t4, $t3 # where I save the decimal number
addi $s4, $s4, 1 # increment adress of my new ASCII word (without '\n')
j Bucle
Acabar:
la $a0, sale # printing 2nd text
li $v0, 4
syscall
add $v1, $zero, $t4
move $v0, $v1
move $a0 , $v0
li $v0, 1 # printing MY NUMBER... THING THAT DOESNT WORK!!!
syscall
jr $ra # exit
你想做小数,所以在GuardarIncrementar
变化:
rol $t4, $t4, 4 # rol positions
进入:
mul $t4, $t4, 10 # multiply by base 10
我认为您正在将每个输入数字转换为 4 位数字,并使用
rol $t4, $t4, 4 # multiply by 16
add $t4, $t4, $t3 # and add the new digit
累加整数值。
这会将每个输入数字放入其自己的 4 位整数中,这意味着您将输入视为以 16 为底(但不处理 A-F)。
在基数 10 中,您需要乘以 10,而不是 16。因此您不能使用单个移位或旋转。