How would you go about converting a stored Hex value to it's binary equivalent using lc3/assembly code?

你说的曾经没有我的故事 提交于 2019-12-14 02:40:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!