如何在SPIM中将十进制数转换为二进制,八进制和十六进制数?

时间:2016-09-28 10:58:05

标签: assembly decimal spim number-systems pcspim

.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

0 个答案:

没有答案