1

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

wallyk
  • 56,922
  • 16
  • 83
  • 148
tina nyaa
  • 991
  • 4
  • 13
  • 25

4 Answers4

4

The bit field of the status byte may contain other flags in other bits. You're only interested in the bit 0 (the least significant bit) in this case, so you ignore the rest of bits by anding the value with 1, and then testing the value against 0.

Diego Sevilla
  • 28,636
  • 4
  • 59
  • 87
3

Let's say that memory address 0xFFE2 contains 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:

      00010100  # content of the memory cell (0x14)
and   00000001  # 0x1
---------------
      00000000

...or...

      00010101  # content of the memory cell (0x15)
and   00000001  # 0x1
---------------
      00000001

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, and operator is just a way of extracting single bit from a byte, nothing more.

Robert Kolner
  • 1,562
  • 12
  • 17
1

Because, as the problem states, "the printer status port is bit zero". If you don't clear away the other bits with that AND instruction, these could cause you to not take the jump even when the bit of interest is zero.

JustJeff
  • 12,640
  • 5
  • 49
  • 63
0

Since you're a student, you probably need more than just a quick answer:

BX is a 16 bit register and only the first bit (bit 0) is of interest. As an example, use a value of 10101111 00101000 (AF28h) for bx. cmp bx, 0 would return false even though bit 0 has a value of zero because cmp compares the value of the whole register, not just bit 0.

In other words, cmp bx, 0 = false because AF28h <> 0

The line and bx, 1 changes the value of bx to 0 if the first bit is 0, or 1 if the first bit is 1

In my example, and bx, 1 sets bx = 0 because bit 0 has a value of zero

enter image description here

Brien Malone
  • 605
  • 1
  • 9
  • 20