Ok, this is an assignment for my computer organization class, and we were required to:
Read in an integer less than 4096
And then print out the first 12 bits and their values, so for instance, 100 would display as
0 0
1 0
2 1
3 0
4 0
5 1
6 1
7 0
8 0
9 0
10 0
11 0
Here's my code so far
I left out some stuff at the beginning, but I'm pretty sure that's correct.
I'm not sure what's wrong with my code, but no matter what the input, I end up with 1 in the first bit, and 0's in all remaining bits. Anyone with a decent understanding of assembly code who can help me with this?
Read in an integer less than 4096
And then print out the first 12 bits and their values, so for instance, 100 would display as
0 0
1 0
2 1
3 0
4 0
5 1
6 1
7 0
8 0
9 0
10 0
11 0
Here's my code so far
#Sets $s0 (the loop counter) equal to 0
li $s0, 0 #Set $s0 equal to 0
#Copies the user input from the temporary to $s1
add $s1, $t0, $zero #Store $t0 in $s1
#This is the start of a loop that should go through the loop 12 times.
#On each iteration it should check if the current value is odd or even (divide by 2 and check for remainder).
#Then it should cut off the end digit and output a 1 if there was a remainder.
loop: slt $t0, $s0, 12 #$t0 = 1 if loop counter is less than 12
bne $t0, 1, exit #Exit loop on the 12th iteration
#Load 2 into $t3 so it can be used for division. (no divide immediate command), then divide the number by 2.
li $t3, 2
add $t1, $s1, $zero #Store $s1 in temp $t1
div $t1, $t3 #Divide the integer by 2
#Load the remainder and see if it's greater than 0 (ie, there is a remainder).
mfhi $t4 #Stores the remainder in $t4
slt $t2, $zero, $t4 #Store 1 in $t2 if there is a remainder
sll $s1, $s1, 1 #Shifts the word right 1 bit
li $v0, 1 #System call code for print_int
add $a0, $s0, $zero #Load in current loop iteration
syscall #Print current loop iteration
li $v0, 4 #System call for print_str
la $a0, t #Load in tab
syscall #Print the tab
li $v0, 1 #System call for print_int
beq $t2, 1, printOne #If there is a remainder, go print 1, else print 0
li $a0, 0 #Load in the number 0
j Print #Print out the number
printOne: li $a0, 1 #Load in the number 1
Print: syscall #Print out the number
li $v0, 4 #System call for print_str
la $a0, n #Load in the new line
syscall #Print the new line
addi $s0, $s0, 1 #Increment the loop counter
j loop #Jump to the top of the loop
exit: li $v0, 10 #System call for exit
syscall #Exit
I left out some stuff at the beginning, but I'm pretty sure that's correct.
I'm not sure what's wrong with my code, but no matter what the input, I end up with 1 in the first bit, and 0's in all remaining bits. Anyone with a decent understanding of assembly code who can help me with this?
