Changes

Jump to navigation Jump to search

Dynamic recompilation

40 bytes removed, 20:34, 5 December 2022
Example
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:
mov A,[first string pointer] ; Put location of first character of source string
; in register A
mov B,[second string pointer] ; Put location of first character of destination string
; in register B
loop:
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
jnz loop ; If it wasn't 0 then we have more to copy, so go back
; and copy the next byte
end: ; If we didn't loop then we must have finished, ; so carry on with something else.</source>
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.
Our new recompiled code might look something like this:
<source lang=asm> beginning:
mov A,[first string pointer] ; Put location of first character of source string
; in register A
mov B,[second string pointer] ; Put location of first character of destination string
; in register B
loop:
movs [B],[A] ; Copy 16 bytes at address in register A to address
; in register B, then increment A and B by 16
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.
end: ; If we didn't loop then we must have finished,
; so carry on with something else.</source>
Anonymous user

Navigation menu