I have been learning Assembly and I have a question. The textbook presents the following example:
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.
Why must we perform the second instruction, and bx, 1? Can’t we just go straight to cmp bx, 0?
Also, can you please clarify or reword “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“? I don’t understand what it means right now because English isn’t my first language.
Thank you for
Let’s say that memory address
0xFFE2contains a byte with 8 bits, for instance something like this:00010100.Only the last bit contains information about printer status. All other bits don’t matter for this purpose. How would you extract the last bit from this byte?
The solution given by the book (and one that is used overall) is to zero out all bits that don’t matter by using bitwise and operator:
…or…
You see where this is going, don’t you? By comparing the result of the operation with 0, you can get definite answer if the last bit was 0 or not and hence if the printer is ready or not. Thus, in this case,
andoperator is just a way of extracting single bit from a byte, nothing more.