简体   繁体   中英

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...

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.

I guess the author wrote the code by hand. The letters 'a' and 'd' are very similar in handwriting, especially when sloppily written:

在此处输入图片说明 ( http://en.wikipedia.org/wiki/File:Cursive.svg )

The transcriber confused the letters (or didn't know the different forms of the printed and the handwritten 'a') and wrote mov dl, [bx] . Correct is mov al, [bx] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM