MIPS将二进制转换为十进制

时间:2019-03-14 01:55:24

标签: assembly mips mars-simulator base-conversion

我很难将二进制形式的113的“ binary_digit”加载到我的MIPS代码中并将其转换为十进制。相反,我的代码要求的是我不需要的用户输入。我试图让它在组装和运行它时自动将“ binary_digit”转换为113,而不要求用户输入。下面是我的代码,任何帮助,我们感激不尽。它将01110001转换为113很好,但是只有在用户输入时才可以。我只需要读取“ binary_digit”并自动将其转换

.data

  binary_digit:     
    .word       0 1 1 1 0 0 0 1     # is 113 in decimal
  msg1:
    .asciiz "\nResult: "
  empty:
    .space 16
  sum:
    .space 16 
.text
.globl main
main:

getNum:
la $a0,binary_digit         #"Please insert value (A > 0) : "


la $a0, empty
li $a1, 16              # load 16 as max length to read into $a1
li $v0,8                # 8 is string system call
syscall

la $a0, empty
li $v0, 4               # print string
syscall

li $t4, 0               # initialize sum to 0

startConvert:
  la $t1, empty
  li $t9, 16             # initialize counter to 16

firstByte:
  lb $a0, ($t1)      # load the first byte
  blt $a0, 48, printSum    # I don't think this line works 
  addi $t1, $t1, 1          # increment offset
  subi $a0, $a0, 48         # subtract 48 to convert to int value
  subi $t9, $t9, 1          # decrement counter
  beq $a0, 0, isZero
  beq $a0, 1, isOne
  j convert     # 

isZero:
   j firstByte

 isOne:                   # do 2^counter 
   li $t8, 1               # load 1
   sllv $t5, $t8, $t9    # shift left by counter = 1 * 2^counter, store in $t5
   add $t4, $t4, $t5         # add sum to previous sum 

   move $a0, $t4        # load sum
   li $v0, 1              # print int
   syscall
   j firstByte

convert:

printSum:
   srlv $t4, $t4, $t9

   la $a0, msg1
   li $v0, 4
   syscall

 move $a0, $t4      # load sum
 li $v0, 1      # print int
 syscall

exit:
   li $v0, 10       # exit system call
   syscall

0 个答案:

没有答案