I'm trying to print a binary number to the console using LC-3 assembly.
What I've tried so far includes (but isn't limited to):
binary .fill b10000110
lea r0, binary
puts ; prints garbage
ld r0, binary
out ; prints 0 (I know it only prints one character but I don't know why it chooses to print 0)
lea r1, binary
and r2, r2, #0
loop ldr r0, r1, r2
out
add r2, r2, #1
and r3, r3, #0
not r3, r2
add r3, r3, #1
add r3, r3, #8 ; I know all the binary numbers will be exactly 8 bits long
brz end
add r3, r3, #0 ; to be safe
brnzp loop
end
; more code...
None of this works particularly well. I'm pulling my hair out trying to figure out the proper way to do this, but everything I'm thinking of relies on binary
being a string, which I can't do.
In LC-3, the
OUT
trap subroutine takes the value currently stored in registerR0
, finds the corresponding ASCII value and outputs it to the console, while thePUT
trap subroutine takes the value stored inR0
as a memory, and iterates through all the data stored at that address, outputting each byte as ASCII to the console, until it finds a NULL character.In the example you gave,
PUTS
will print out the ASCII representation ofb10000110
, followed by garbage until it happens to hit a NULL character whereasOUT
will simply print the ASCII representation ofb10000110
.Subsequently, to actually print a 0 or a 1, we must print the ASCII representation of those numbers, not the numbers themselves. So, we define two words, one for the ASCII character
0
, and the other for1
.So, for any 1-bit number, we can print it to the console with a simple if-else branch and the
OUT
subroutine.Now, we must extend this to n-bits. That is to say that we must break our n-bit number into a series of 1-bit numbers that we can easily print. To achieve this, all we need is a simple
AND
and a mask for the ith bit (e.g. given the 8-bit numberb10000110
, to determine the 3rd least significant bit, we would use the maskb00000100
). So, for an 8-bit number, we will need the sequence of masksb10000000
,b01000000
, ...,b00000001
. There are several ways to do this, such as starting withb10000000
and left-shifting/multiplying by 2 for each bit, however for the sake of simplicity we will use a lookup table.To choose the mask before we print out each bit, we can use a simple for-loop branch.
Finally, we have our completed program.