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?