简体   繁体   中英

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

I'm trying to convert a inputed integer to binary and I cannot seem to find the error within my program. I'm able to load the inputed number as well as print out a 8 digit binary number, however none of the 1's are being printed. What am I doing wrong?

 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

I'm answering this question starting from a 32-bit MIPS version.

First of all, when asking for a decimal number, MIPS saves this as a 32-bit number in its registers. Therefore I did the following adjusments:

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

Becomes:

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

Notice how I changed the andi into addi . If you were working in an 8-bit version of MIPS, this would be the only necessary adjustment.

I hope I solved your question, it works in the MARS-simulator overhere!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM