1

I found a tutorial for writing a bootloader. It is all very clear to me except for 1 line. Here is some code.

Print:
    lodsb
    or al, al ;I don't get this line
    jz PrintDone
    mov ah, 0x0e
    int 0x10
    jmp Print

It has some other code that clears out the registers used by int 0x10. The only thing I do not understand is the or al, al line. If you OR something with it self you get what you started with right?

If anybody can answer this I will love them forever :)

Carsten Schütte
  • 4,408
  • 1
  • 20
  • 24
user701329
  • 127
  • 2
  • 7
  • 3
    The "or" is probably there to make sure that the z-flag is set if al is zero. It sets the z-flag without modifying al. Lodsb loads a value to al but does not affect the zero flag so another instruction is needed for that. – Ville Krumlinde Oct 20 '12 at 07:40

1 Answers1

4

The JZ instruction is a form of the JMP instruction except that the jump occurs only when the zero flag is set. The "OR AL, AL" sets the zero flag if al is zero. This is more efficient than using a CMP compare.

CMP AX,0        ;see if the number in ax is zero (zero flag set if so)
OR AX,AX        ;this does exactly the same but uses 2 bytes instead of 3
TEST AX,AX      ;again this is the same and uses only 2 bytes
Jens Björnhager
  • 5,632
  • 3
  • 27
  • 47
Carsten Schütte
  • 4,408
  • 1
  • 20
  • 24
  • `or al,al` is actually *less* efficient than `cmp al,0`: both are 2 bytes, because of the special `al, imm8` encoding for CMP, but `cmp` is better. [Of course `test al,al` is even better, and is still 2 bytes for `ax` or other registers](https://stackoverflow.com/questions/33721204/test-whether-a-register-is-zero-with-cmp-reg-0-vs-or-reg-reg/33724806#33724806). And `test`/`cmp` can macro-fuse with `jcc`, and don't add latency to the register. – Peter Cordes Apr 08 '18 at 03:42