Dynamic recompilation: Difference between revisions

m Protected "Dynamic recompilation" ([Move=Allow only administrators] (indefinite))
Line 21: Line 21:
Suppose a program is being run in an emulator and needs to copy a null-terminated string. The program is compiled originally for a very simple processor. This processor can only copy a byte at a time, and must do so by first reading it from the source string into a register, then writing it from that register into the destination string. The original program might look something like this:
Suppose a program is being run in an emulator and needs to copy a null-terminated string. The program is compiled originally for a very simple processor. This processor can only copy a byte at a time, and must do so by first reading it from the source string into a register, then writing it from that register into the destination string. The original program might look something like this:


<source lang=asm>
beginning:
beginning:
     mov A,[first string pointer]    ; Put location of first character of source string
     mov A,[first string pointer]    ; Put location of first character of source string
                                     ; in register A
                                     ; in register A
     mov B,[second string pointer]  ; Put location of first character of destination string
     mov B,[second string pointer]  ; Put location of first character of destination string
                                     ; in register B
                                     ; in register B
loop:
loop:
     mov C,[A]            ; Copy byte at address in register A to register C
     mov C,[A]            ; Copy byte at address in register A to register C
     mov [B],C            ; Copy byte in register C to the address in register B
     mov [B],C            ; Copy byte in register C to the address in register B
Line 37: Line 36:
     jnz loop            ; If it wasn't 0 then we have more to copy, so go back
     jnz loop            ; If it wasn't 0 then we have more to copy, so go back
                         ; and copy the next byte
                         ; and copy the next byte
end:                     ; If we didn't loop then we must have finished,
end:                   ; If we didn't loop then we must have finished,
                         ; so carry on with something else.</source>
                         ; so carry on with something else.


The emulator might be running on a processor which is similar, but extremely good at copying strings, and the emulator knows it can take advantage of this.
The emulator might be running on a processor which is similar, but extremely good at copying strings, and the emulator knows it can take advantage of this.
Line 48: Line 47:
Our new recompiled code might look something like this:
Our new recompiled code might look something like this:


<source lang=asm>beginning:
beginning:
     mov A,[first string pointer]    ; Put location of first character of source string
     mov A,[first string pointer]    ; Put location of first character of source string
                                     ; in register A
                                     ; in register A
     mov B,[second string pointer]  ; Put location of first character of destination string
     mov B,[second string pointer]  ; Put location of first character of destination string
                                     ; in register B
                                     ; in register B
loop:
loop:
     movs [B],[A]            ; Copy 16 bytes at address in register A to address
     movs [B],[A]            ; Copy 16 bytes at address in register A to address
                             ; in register B, then increment A and B by 16
                             ; in register B, then increment A and B by 16
     jnz loop                ; If the zero flag isn't set then we haven't reached
     jnz loop                ; If the zero flag isn't set then we haven't reached
                             ; the end of the string, so go back and copy some more.
                             ; the end of the string, so go back and copy some more.
end:                       ; If we didn't loop then we must have finished,
end:                       ; If we didn't loop then we must have finished,
                             ; so carry on with something else.</source>
                             ; so carry on with something else.</source>