问题
The values are stored in a .BLKW object and are loaded in a LOOP with LDR R0,R1,0 - ADD R1,R1,1 (to increment the .BLKW address). The problem is how do you convert the stored HEX values to their binary values, and then output the conversion to the CONSOLE in 16-bit binary format. Any ideas will be greatly appreciated! I've thought about ANDing values, but am unsure how to go about it.
回答1:
You've already got the loop down. However, one small error.
We know that when we use OUT to print, it prints the value in R0 to the console. So, the number we're printing (the number in R0) should always be 0 or 1. Thus, the hex value we're working on should be stored in some other register, like R2. So, the code "LDR R0, R1, 0" becomes "LDR R2, R1, 0".
Now, for each word (aka address in the .BLKW) you have to convert it to binary, printing one bit at a time.
You could do 16 AND operations. Check if the [16] (the left-most bit) is one by ANDing R2 with the number with 1000 0000 0000 0000. If the result isn't 0, then you know the left-most bit is a 1, so print 1. If the result is 0, you know it must be zero, so print 0. Then AND R2 with 0100 0000 0000 0000 to check the second bit in the same manner and so forth.
However, that's just messy. The best way is to use a loop.
Set one of the registers - let's say R3, to 1000 0000 0000 0000. We'll be using this in a minute.
Let's use R5 as our sentinel, aka "counter", to tell us when we've processed all 16 digits. We'll set R5 to 16, then subtract 1 from R5 each time we go through the loop, and when we hit 0, we'll exit the loop.
Now, what to put inside the loop.
For each iteration, AND R2 and R3, and store the result in some unused register like R4. If the result is zero, we know the left-most bit of R2 is zero, so print 0. If the result is negative (because 1000 0000 0000 0000 is a 2's complement number and thus negative), we know the left-bit is a 1, so print 1.
Then do a left bit-wise shift in R2. This is accomplished by adding R2 to itself and storing it via: "ADD R2, R2, R2".
Decrement our counter in R5 via : "ADD R5, R5, #-1"
Return to the start of the loop.
The result should be what you need.
回答2:
I have no idea about lc3, but presumed that the shift instructions reflect the last shifted out bit in the carry flag, you can
- on a little endian architecture, shift the value left by 1, and if the carry is set, put a '1' to the output stream, a '0' otherwise
- on a big endian architecture, shift the value right, and put a character '1' when carry set, or '0' otherwise, to the output.
If the shift instructions don't shift into the carry flag, you can test the most or least significant bit first (for little or big endian arch, respectively), and depending on the result put '1' or '0' to the output stream, and shift thereafter — the effect is the same.
With "stream" I mean any imaginable output medium, be it a (string) variable or other buffer in memory or a real file.
Sorry that I can't provide you with usable source code for the system in question.
Regards, and have fun...
来源:https://stackoverflow.com/questions/12380400/how-would-you-go-about-converting-a-stored-hex-value-to-its-binary-equivalent-u