The fellowing paragraph is a explaination about I/O.
Why do he use "and bx, 1" in here, this line just clear bx from 1st bit to 15st bit, is it redundant?



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,
0003: and bx, 1 ; Why do he add this line in here, is it redundant?
0006: cmp bx, 0
0009: je 0000
000C: mov , ax
. . .






The next paragraph is:

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(20).
(20) Note that this is a hypothetical example. The PC??s parallel printer port is not mapped to memory addresses 0FFE0h and 0FFE2h on the x86.
Posted on 2004-06-07 22:59:24 by bloggs
It's a simple check. Test bit zero from 0xffe2 and jump if zero.

You might want to get aquainted with some arithmetics.

Fake
Posted on 2004-06-08 11:08:22 by Fake51
The person who wrote it likely was not very knowledgeable in 8086 code.
There is no 3-byte jz or 3-byte mov bx,memory.
After the and bx,1, the Z flag will be set if bx becomes 0, and clear if it becomes 1. Therefore the cmp bx,0 is redundant.
He should have used the following sequence (assuming FFE2 is read-only)
0000: shr byte ptr ,1 ; bit 0 in C
0004: jae 0000
0006: mov ,al (I assume ax was a typo)
If FFE2 is r/w, then the following must be used instead:
0000: test byte ptr ,1
0005: jz 0000
0007: mov ,al
Posted on 2004-06-08 11:38:02 by Sephiroth3
This paragraph comes from "The art of assembly language, dos edition"
The author use a virtual processor to explain the essential concept of I/O.


It seems that "and bx, 1" is redundant, isn't it?
Posted on 2004-06-08 12:47:56 by bloggs
no, the cmp is redundant.
and bx, 1 will mask out all bits except for the least-significant bit.
The following cmp bx, 0 will check if this bit is set or not.
But as Sephirioth3 already mentioned, the and itself sets the zero-flag, so the cmp is not required.
Also, I would always prefer the second version he posted, with the test, since it is faster than shr on some CPUs, and there is no problem with the data having to be read-only (it also makes the code easier to understand).
Posted on 2004-06-08 13:17:29 by Scali