Self-modifying code

Started by
0 comments, last by Julian90 16 years, 8 months ago
I am trying to make self-modifying code with x86 assembly. I am running the following thru Virtual PC MS-DOS 6.22 machine.

.model small
.286
.stack 256
.data

msgPrompt       db      "1>Add",0Dh,0Ah,"2>Subtract",0Dh,0Ah,'$'

.code

Template:
        add ax, 1
        sub ax, 1

SketchSpace:
        nop
        db 01h
        db 00h
        ret

main proc
        mov ax, seg msgPrompt
        mov ds, ax
        
        mov ax, 0900h
        lea dx, msgPrompt
        int 21h

        mov ax, 0800h
        int 21h
        mov al, 61h
        
        cmp al, 61h
        jnz setSubtract
setAdd:
        mov byte ptr [SketchSpace], 05h
        jmp setInstruction
setSubtract:
        mov byte ptr [SketchSpace], 02Dh

setInstruction:
        mov ax, 04Dh
        call SketchSpace

        ; now display the result!
        mov ah, 02h
        mov dl, al
        int 21h

        mov ax, 4c00h
        int 21h
main endp
end main

The way I think my code should work, is that if the user presses '1', 'N' should be displayed ('M' + 1); whereas if the user presses '2', 'L' ('M' - 1) should be displayed. When I run it thru the "debug" command, everything goes fine, however, run straight from the prompt, the output is not modified in any way (it simply displays 'M'). Any suggestions? Thx in advance.
A JPEG is worth a thousand and twenty four DWORD's. ;)
Note: Due to vacationing my website will not be updated till late-August. Of course I still have internet, but who wants to program during a vacation?
Advertisement
More likely then not your modifying the instruction after it's been fetched into the instruction queue so the processor only sees the version in the queue not the modified version, when you run the debugger the debugger code is being run in between the two instructions so it is only being loaded into the queue after it has been modified. Any way self modifying code on modern hardware is a really bad idea.

This topic is closed to new replies.

Advertisement