2016-10-08 9 views
1

私が間違って出力を取得していますそれを整数として返します。間違った整数出力のMips

しかし、私の登録簿には例えば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

入力文字列を16進数(16進数)として扱っているように思えます。あなたはレジスタに文字列を入れることはできません。プログラムで使用しているシステムコールのドキュメントを確認しましたか?そして、デバッガでレジスタ値を調べましたか? –

+0

私は各文字に関連するASCIIテーブルの数字を扱っていると思います(数字0 - > 48の記号は0などです)。そして、このコードは私はヘックスから12月に変換しなければならなかった; D –

答えて

2

あなたは小数点以下をやりたい、そうでGuardarIncrementar

変更:

rol $t4, $t4, 4  # rol positions 

の中へ:

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

ありがとう!私はちょうどその行を変更し、それは完全に動作します –

0

私は4ビットの数に、各入力数字を変換し、整数値を蓄積する

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

を使用していると思います。

これは、各入力桁を整数の4ビットに置きます。つまり、入力をベース16として扱います(しかし、A-Fは処理しません)。

ベース10では、16を乗算する必要はなく、10を掛ける必要があります。シフトまたは回転を1つ使用することはできません。