簡體   English   中英

如何打印帶有輸入整數的二進制數?

[英]How do I print a binary number with an inputed integer?

我正在嘗試將輸入的整數轉換為二進制,但似乎無法在程序中找到錯誤。 我能夠加載輸入的數字以及打印出8位二進制數字,但是1都沒有被打印。 我究竟做錯了什么?

 enterNum: 
.asciiz "Enter your number (base 10):  "
 printBaseTwo: 
.asciiz "The number in base 2 is:  "
 #-----------------------------------------------------------------------

.text
#Print out string, collect intger input
main: li $v0, 4
      la $a0, enterNum
      syscall
  li $v0, 5
  syscall
  move $t0, $v0


#create mask/print out the second string and prepare to print out binary
mask: 
  andi $t1, $zero, 1
  sll $t1, $t1, 7
  addi $t2, $zero, 8
  li $v0, 4
  la $a0, printBaseTwo
  syscall


 # compares mask to integer, starting at the most sig place
 # if the mask is zero, print out zero
 loop: 
  and $t3,$t0, $t1
  beq $t3, $zero, print
  add $t3, $zero, $zero
  addi $t3, $zero, 1
  j print


 print: 
      #prepares to print integer in $a0
  li $v0, 1

      # moves either 1 or 0 into $a0 to be printed
  move $a0, $t3
  syscall

      # shifts over right 1, getting closer to 0
  srl $t1, $t1, 1

      #lowers count
  addi $t2, $t2, -1

      #loop back to beginning if not finished printing binary Num
  bne $t2, $zero, loop
  beq $t2, $zero, exit
 exit: 
  li $v0, 10
  syscall

我從32位MIPS版本開始回答這個問題。

首先,當要求十進制數時,MIPS將其保存為32位數字在其寄存器中。 因此,我做出了以下判斷:

andi $t1, $zero, 1
sll $t1, $t1, 7
addi $t2, $zero, 8

成為:

add $t1, $zero, 1
sll $t1, $t1, 31
addi $t2, $zero, 32

注意如何將andi更改為addi 如果您使用的是8位版本的MIPS,這將是唯一必要的調整。

希望我能解決您的問題,它可以在此處的MARS模擬器中正常工作!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM