在MIPS程序集中将整数输入转换为十六进制

时间:2017-02-12 22:03:21

标签: assembly int hex mips

我正在用MIPS编写一个程序,将用户输入的整数转换为十六进制,然后输出。出于某种原因,无论我输入什么,系统调用只输出0x,这是我在开头的答案空间中输出的。我做错了什么?

.data
prompt1: .asciiz "Enter an integer number: "
answer: .space 8
.text
.globl main

main:
        la $a0, prompt1     #prompt user for integer
        li $v0, 4
        syscall

        la $a1, answer      # load the address of answer into $a1
        addi $t0, $zero, 48 #set $t0 to 48
            sb $t0, 0($a1)          #store 48 at location 0 in $a1
            addi $t0, $0, 120       #set $t0 equal to 120
            sb $t0, 1($a1)          #store 120 at location 1 in $a1
            addi $a1, $a1, 4

            li $v0, 5       #allow for input of integer
        syscall
        add $a0, $v0, $zero #add the input to the $a0 register
        jal print_hex

        la $a0, answer
        li $v0, 4
        syscall         #print out hex answer to console

        j main


print_hex:
        andi $t0, $a0, 0xf
        addi $a1, $a1, 4

        ble $t0, 10, lessThanTen        
        addi $t0, $t0, 39   
    lessThanTen:
        addi $t0, $t0, 48
        sb $t0, 0($a1)
        srl $a0, $a0, 4
        bne $a0, $zero, print_hex
    done:
        jr $ra

0 个答案:

没有答案