.data
strIn: .asciiz "\nEnter a number:"
reply: .asciiz "\nYour input is:"
hexdigits: .asciiz "0123456789abcdef"
hexword: .asciiz "00000000"`
hexresult: .asciiz "\nHexadecimal code is:"`
buffer: .space 10
.text
.globl main
main:
#STEP 1 -- get the decimal number
#Print a prompt asking user for input
li $v0, 4
la $a0, strIn
syscall
#Read in the number
la $a0, buffer
la $a1, buffer
li $v0, 8
syscall
#Ouput the decimal number
la $a0, reply
li $v0, 4
syscall
la $a0, buffer
li $v0,4
syscall
move $t1, $v0
我首先尝试做十六进制数,我从网上找到了一种方法......
# get the value and put it in $t2
loop3o: lw $t2, ($t1)
# initialize values for the inner loop
la $t6, hexdigits
la $t7, hexword
li $t3, 15 # the mask value
sll $t3, $t3, 28
li $t4, 28 # loop3i counter and shift amount
# mask off the correct 4 bits for a hex digit
# and shift for bit positions 0-3
loop3i:
and $t5, $t2, $t3
srl $t5, $t5, $t4
# get proper hex digit
add $t5, $t5, $t6
lb $t8, ($t5)
sb $t8, ($t7)
# process loop values and branch
srl $t3, $t3, 4
addi $t7, $t7, 1
addi $t4, $t4, -4
bgez $t4, loop3i
# output the hex word
li $v0, 4
la $a0, hexword
syscall
# process loop values and branch
addi $t0, $t0, -1
addi $t1, $t1, 4
bgtz $t0, loop3o
la $a0, hexresult
li $v0, 4
syscall
它根本不起作用。我怎样才能在 SPIM 中做到这一点?
输出应该是这样的......
输入数字:23
输入数字为 23
二进制码为 0000 0000 0000 0000 0000 0000 0001 0111
八进制代码是 000 0000 0027
十六进制代码为 0000 0017