Search code examples
c++windowsassemblymasm

ASM : How is the al register affected in the following snippet


Was reading a book on Microprocessors. Saw this snippet in C++ code to print a string using ASM.

str (char *string_adr[])
{
   _asm
   {
      mov bx, string_adr
      mov ah, 2
      top:
      mov dl, [bx]
      inc bx
      cmp al, 0
      je bot
      int 21h
      jmp top
      bot:
      mov al, 20h
      int 21h
      }
}

Now I was wondering how the cmp al, 0 works since al is not used before that...


Solution

  • You'll have to look at proc linkage code the compiler generates. It may, for example, decide to put arguments not only on stack, but selected few (one?) into registers. But this doesn't seem to be the case here: My impression is that instead of both references to al, dl was actually meant - was that the case, the result was that an end of string (0) would be printed as space character before termination (int 21h looks like MSDOS, function 2, which outputs the char which ASCII is in dl). This makes some sense, therefore I believe this to be a typo in the example. I can even attempt to offer some guess where this typo could come from: in an earlier version of that example, LODSB was used, which is as much of an autoincrementing indirect register read an 8086 offers - and the target for the read bytes is al. Because use of LODSB is deprecated. the example was updated, and the indirect register read with increment coded out, this time reading directly into dl instead of al, because that's where DOS fn 02 expects the character - unluckily the test for end of string was missed, as was the later load with ASCII of space char, and kept as was in the previous version of that example.