Hacker News new | ask | show | jobs
by stuartmalcolm 2769 days ago
> assume ah=0; assume bx=0

Hmm. So the second time this program is run, it will fail?

1 comments

No, AFAIK MS-DOS always initialises it to zero before starting the program. I'm trying to find a better reference, but I think this[0] effectively explains what state the registers are in on entry to your program.

Edit: I take that back. According to [1]:

  .COM-format executables begin running with the following register values:
  AL = 00h if first FCB has valid drive letter, FFh if not
  AH = 00h if second FCB has valid drive letter, FFh if not

[0] https://thestarman.pcministry.com/asm/debug/debug.htm#INIT

[1] http://www.delorie.com/djgpp/doc/rbinter/id/51/29.html

The value of BX is however strictly undefined, but practically always 0. Potentially some DOS will load this register with a different value, but probably no version of MS-DOS.
But why not explicitly set the register to 1 rather than assume its 0 and increment it by 1?
On x86, some instructions are longer than others. Incrementing is a single byte. Setting the value 1 if you don’t assume anything is going to be 2 bytes (al, ah, bl, etc.) or 4 bytes for (ax, bx, etc.)

This online x86/x64 assembler is great: https://defuse.ca/online-x86-assembler.htm

Saves one byte, I assume. Register targets are typically encoded in the opcode while direct values follow the opcode byte.
Probably because (IIRC) mov is 2 bytes and inc is only 1 byte and they're optimizing for minimum size.