0

Problem:

Write a program to convert an 8-bit binary number entered from the keyboard to the equivalent Gray code binary number, using the following the algorithm:

[broken image: http://www.harborlandings.com/images/grayAlgm.jpg]

I'm learning assembly (8086, required by class) and not sure how to do this.

Do I convert each character (1 or 0) as I receive it from the keyboard? Or, do I read in the entire 8-bit binary number, loop through it, converting as I go?

I come from Perl/Java, so visualizing how this will work in assembly is a bit daunting. Also, I'm not sure what the symbol is in the algorithm above?

Thank you for your help and critique!

4

2 回答 2

2

您可以简单地对自身右移一位的字符进行异或运算,以获得灰色表示,无需循环。当您的角色在 AL 中时的示例:

mov bl, al
shr bl, 1
xor al, bl

AL 现在是格雷码表示。

在 C 中,这将是:

c^=c>>1;

要返回二进制表示,您可以将格雷码与它自身进行异或,通过减小 2 的幂向右移动,从小于数据大小的最大 2 幂开始,例如:

mov bl, al
shr bl, 4
xor al, bl
mov bl, al
shr bl, 2
xor al, bl
mov bl, al
shr bl, 1
xor al, bl

在 C 中,这将是:

c^=c>>4; c^=c>>2; c^=c>>1;
于 2009-06-26T08:06:28.513 回答
1

自从我编写任何汇编代码以来已经有很长时间了,但这些问题似乎更具哲学性。在不知道更大的目标的情况下,这里有一些想法。

在输入每个键时对其进行转换:很多时候程序需要在程序运行时响应单个键击(即动态命令,向上、向下、向左等)。在这种情况下,应单独转换击键。其他时候,需要转换一个数据块或字符串,这个操作通常在一个更大的数据块的回车键结束时完成。这些情况要求字符被“循环”通过和转换。

然而,在任何一种情况下,“工作”都应该在可以从任何一种情况调用的通用子程序中完成。

我希望这有帮助,

埃德

于 2009-06-12T20:15:46.397 回答