S Huntsman
S Huntsman

Reputation: 123

EX6.x86 in The Art of Assembly

I'm trying to work through section 3.6.7 Self Modifying Code Exercises in Art of Assembly by hand and cannot help thinking that either I'm doing something very wrong with the high/low order byte interleaving (I am aware of how the author does this for his imaginary x86 variants) or that there is an error in the text.

To wit, the program in the text is

   sub  ax, ax
   mov  [100], ax

a: mov  ax, [100]
   cmp  ax, 0
   je   b
   halt

b: mov  ax, 00c6
   mov [100], ax
   mov  ax, 0710
   mov [102], ax
   mov  ax, a6a0
   mov [104], ax
   mov  ax, 1000
   mov [106], ax
   mov  ax, 8007
   mov [108], ax
   mov  ax, 00e6
   mov [10a], ax
   mov  ax, 0e10
   mov [10c], ax
   mov  ax, 4
   mov [10e], ax
   jmp 100

which supposedly "writes the following code to location 100 and then executes it:"

   mov  ax, [1000]
   put
   add  ax, ax
   add  ax, [1000]
   put
   sub  ax, ax
   mov [1000], ax
   jmp 0004  

However, this doesn't make sense to me for a number of reasons (first of all, it writes 00 to byte 100, which is an illegal instruction that is supposedly jumped to; second, the high/low order bytes in addresses seem to be switched, etc.) On the other hand, if I simply swap the high/low order bytes in the mov ax, c statements in the b loop, then this seems to be correct. So:

Am I wrong (if so, why?) or should the high/low order bytes in the b loop constants be swapped?

Upvotes: 1

Views: 244

Answers (1)

Bo Persson
Bo Persson

Reputation: 92391

It doesn't store 00 at address 100, because the x86 processors are "little-endian". When you store 00c6 as a word, the c6 is stored first.

On the other hand, self modifying code is out of fashion since a long time. On modern processors, you cannot write to the code segment and you cannot execute data (at least not without jumping through a huge number of loops). Another problem is that the instruction cache has probably loaded the unmodified code long before you change it.

You either risk executing the old code, or will have to flush all the caches with a tremendous impact on performance. In short - self modifying code is just not worth it. I haven't used it since the PC/XT in the early 1980's.

Why don't you skip a chapter in the book?

Upvotes: 2

Related Questions