简体   繁体   English

LC3位计数器

[英]LC3 Bit Counter

I'm trying to figure out how to implement a bit counter in LC3 assembly language. 我试图弄清楚如何在LC3汇编语言中实现位计数器。 ex: input "00001100001000001" output "000000000000100" I would be counting the number of ones in the string of bits and outputting that number in binary. 例如:输入“ 00001100001000001”,输出“ 000000000000100”,我将计算位字符串中的位数并以二进制形式输出该数值。 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. 我知道如何一次给定一位,但是我不知道如何一次只能分析16位字符串中的一位。 Thanks. 谢谢。

There are several different ways you can count the number of bits in a value stored in the LC3. 您可以用几种不同的方法来计算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. 如果对它们进行“与”运算后得到正数,则只需将1加到位计数器,然后对其余的每个掩码值重复该过程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM