使用此功能时输出错误:
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 '\0' --> 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
答案 0 :(得分:2)
你想做小数,所以在GuardarIncrementar
变化:
rol $t4, $t4, 4 # rol positions
分为:
mul $t4, $t4, 10 # multiply by base 10
答案 1 :(得分:0)
我认为您将每个输入数字转换为4位数字并使用
rol $t4, $t4, 4 # multiply by 16
add $t4, $t4, $t3 # and add the new digit
累积整数值。
将每个输入数字放在其整数的4位中,这意味着您将输入视为基数16(但不处理A-F)。
在10号基地,你需要乘以10而不是16,所以你不能使用单个班次或旋转。