LC3位计数器 [英] LC3 Bit Counter

查看:169
本文介绍了LC3位计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何用LC3汇编语言实现位计数器.例如:输入"00001100001000001"输出"000000000000100" 我将在位字符串中计数一个的数目,并以二进制形式输出该数目.我知道如何一次只给定一点,但是我不知道如何一次只分析16位字符串中的一位. 谢谢.

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.

推荐答案

有几种不同的方法可以计算LC3中存储的值中的位数.

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

  • 您可以使用位移并计算末端掉落"的位
  • 您可以使用位掩码来检查值中的每个位
  • 我敢肯定还有其他方法,但是它们不是很有效

就个人而言,我将使用位掩码方法,因为它既快速又有效,而且我不必担心将代码停顿下来.

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.

位掩码如下所示:

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

现在,您要做的就是创建一个循环,将每个掩码值与您存储的值进行与"运算.如果对它们进行与"运算后得到一个正数,则只需将1加到位计数器,然后对其余的每个掩码值重复该过程.

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.

这篇关于LC3位计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆