Search code examples
assemblyoutputmipsmips32

wrong integer output Mips


I'm getting a wrong output when using this:

li $v0, 1

I have to save the user input as ASCII (from 0 to 9), convert it to integer, and then, print it as integer.

But when I have for example 10 in my register, it prints 16; when I have 20, it prints 32 and so on... it goes printing: my_number + 6^(n) and I don't know why...

This is my code:

.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

Solution

  • You want to do decimal, so in GuardarIncrementar

    Change:

        rol $t4, $t4, 4     # rol positions
    

    Into:

        mul $t4, $t4, 10    # multiply by base 10