c - 测试状态位

标签 c x86 assembly

我一直在学习汇编,我有一个问题。教科书中给出了以下示例:

Assume that the printer data port is memory-mapped to address 0FFE0h and the printer status port is bit zero of memory-mapped port 0FFE2h. The following code waits until the printer is ready to accept a byte of data and then it writes the byte in the L.O. byte of ax to the printer port:

0000:   mov     bx, [FFE2]
0003:   and     bx, 1
0006:   cmp     bx, 0
0009:   je      0000
000C:   mov     [FFE0], ax
         .       .
         .       .
         .       .

The first instruction fetches the data at the status input port. The second instruction logically ands this value with one to clear bits one through fifteen and set bit zero to the current status of the printer port. Note that this produces the value zero in bx if the printer is busy, it produces the value one in bx if the printer is ready to accept additional data. The third instruction checks bx to see if it contains zero (i.e., the printer is busy). If the printer is busy, this program jumps back to location zero and repeats this process over and over again until the printer status bit is one.

为什么我们必须执行第二条指令,and bx, 1?我们不能直接转到 cmp bx, 0 吗?

此外,您能否澄清或改写“第二条指令在逻辑上将该值与 1 相加,以清除位 1 到 15,并将位 0 ​​设置为打印机端口的当前状态”?我现在不明白这意味着什么,因为英语不是我的母语。

谢谢你

最佳答案

状态字节的位字段可能包含其他位中的其他标志。在这种情况下,您只对位 0(最低有效位)感兴趣,因此您通过将值与 1 相加来忽略其余位,然后针对 0 测试该值。

关于c - 测试状态位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7236884/

相关文章:

c - fork 多个进程并让父进程等待所有进程(在 C 中)

C buggy 程序

multithreading - 缓存一致性协议(protocol)如何强制执行原子性?

c - 在运行时检查机器指令

android - 出现错误 : inconsistent operand constraints in an 'asm' while porting my c++ code from linux to android

c - 修补二进制文件

c - 未定义的函数引用被编译器接受

c++ - 传递一个函数和一个整数的函数。警告 : passing argument of "" from incompatible pointer type

memory-management - 为什么每次都从相同的内存地址执行一个函数?

c++ - 将 32 位 C++ 代码移植到 64 位 - 值得吗?为什么?