1

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
2
  • Sounds like you're treating your input string as hex (base 16). You can't have a string in your register, BTW. Did you check the docs for the system calls your program is using? And did you look at register values in a debugger? Commented Oct 8, 2016 at 22:10
  • I guess I'm working with the numbers in the ASCII table that relates to each character (number 0 --> the symbol of 48 is 0 and so on...), and yep, this code comes from a program in which I had to convert from Hex to Dec ;D Commented Oct 8, 2016 at 23:03

2 Answers 2

2

You want to do decimal, so in GuardarIncrementar

Change:

    rol $t4, $t4, 4     # rol positions

Into:

    mul $t4, $t4, 10    # multiply by base 10
Sign up to request clarification or add additional context in comments.

Comments

0

I think you're converting each input digit to a 4-bit number, and using

rol $t4, $t4, 4            # multiply by 16
add $t4, $t4, $t3          # and add the new digit

to accumulate the integer value.

That puts each input digit in its own 4 bits of the integer, which means you're treating your input as base 16 (but without handling A-F).

In base 10, you need to multiply by 10, not 16. So you can't use a single shift or rotate.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.