LC3 Bit Counter

2019-06-04 00:18发布

问题:

I'm trying to figure out how to implement a bit counter in LC3 assembly language. ex: input "00001100001000001" output "000000000000100" I would be counting the number of ones in the string of bits and outputting that number in binary. I know how to do this given one bit at a time, but I don't know how I can analyze only one bit of a 16 bit string at a time. Thanks.

回答1:

There are several different ways you can count the number of bits in a value stored in the LC3.

  • You can use bit shifting and count the bits that "fall off" at the ends
  • You can use a bit mask to check each of the bits in the value
  • And I'm sure there are other ways, but they're not very efficient

Personally, I would use the bit mask method because it's quick and efficient, and I won't have to worry about bogging my code down.

A bit mask looks like the following:

B_MASK  .FILL   b1000000000000000
        .FILL   b0100000000000000
        .FILL   b0010000000000000
        .FILL   b0001000000000000
        .FILL   b0000100000000000
        .FILL   b0000010000000000
        .FILL   b0000001000000000
        .FILL   b0000000100000000
        .FILL   b0000000010000000
        .FILL   b0000000001000000
        .FILL   b0000000000100000
        .FILL   b0000000000010000
        .FILL   b0000000000001000
        .FILL   b0000000000000100
        .FILL   b0000000000000010
        .FILL   b0000000000000001

Now all you have to do is create a loop that AND's each of these mask values with your stored value. If you get a positive number after ANDing them then you would just add 1 to your bit counter, and then repeat the process with each of the remaining mask values.



标签: bit lc3 bitcount