-1

I am trying to write a program which takes in integers one at a time and converts them to 8 bit binary, until 0 is entered and the program terminates, in MIPS. My code is outputting zeros for even input and 1s for odd. I can't figure out why this is.

.data
zeroString: .asciiz "00000000\n"

.text

main:
li  $v0, 5      # 5 = syscall code to read int
syscall
beq $v0, 0, exit    #exit when 0 is entered
move    $t0, $v0    #t0=v0
la $t2, 0       #counter

do:         #loop
ANDI $t1, $t0, 0x01 #check if least significant digit is 1 
addi $t2, $t2, 1    #increase counter
move $a0, $t1       #printing value of t1
li $v0, 1
syscall
srl $t1, $t1, 1     #shift to next digit
bne $t2, 7, do      #check if counter equal to target

# Print a new line
li  $a0, '\n'   # $a0 = ascii code for newline char
li  $v0, 11     # 11 = syscall code to print char
syscall         # Print newline

j main

exit:
la $a0, zeroString
li $v0, 4
syscall
li $v0, 10
syscall
Julia
  • 1
  • 2
  • You mean it's printing 8 copies of the low bit (as single-digit integers)? Single-step your code in the debugger and look at register values change. Look carefully at which registers you're using as input and output for each instruction in the loop. – Peter Cordes Nov 16 '17 at 04:46

1 Answers1

0

You mean it's printing 8 copies of the low bit (as single-digit integers)?

Single-step your code in the debugger and look at register values change. Look carefully at which registers you're using as input and output for each instruction in the loop.

You never write $t0, so andi $t1, $t0, 1 produces the same thing every iteration. Was srl $t1, $t1, 1 supposed to be shifting $t0? You're actually shifting the temporary that you're about to overwrite.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847