Jump to content

Runtime evaluation: Difference between revisions

m
Line 12:
=={{header|6502 Assembly}}==
The 6502 can execute code at runtime through the use of self-modifying code, provided that the code runs in RAM. For programs that are defined in ROM, the code can be copied to RAM and executed from RAM. If you have a way to type numeric values into your program and save them in a contiguous section of memory, and those values are stored as numbers and not ASCII, a <code>JMP</code> to their storage location can be used to execute arbitrary code.
 
This example runs on the Commodore 64 and prints the letter A using a crude "eval":
<lang 6502asm>;Init Routine
*=$0801
db $0E,$08,$0A,$00,$9E,$20,$28,$32,$30,$36,$34,$29,$00,$00,$00
*=$0810 ;Start at $0810
 
LDA #$A9 ;opcode for LDA immediate
STA smc_test
LDA #'A'
STA smc_test+1
lda #$20 ;opcode for JSR
STA smc_test+2
lda #<CHROUT
STA smc_test+3
lda #>CHROUT
STA smc_test+4
smc_test:
nop ;gets overwritten with LDA
nop ;gets overwritten with #$41
nop ;gets overwritten with JSR
nop ;gets overwritten with <CHROUT
nop ;gets overwritten with >CHROUT
 
rts ;return to basic</lang>
 
{{out}}
<pre>
LOAD"*",8,1
 
SEARCHING FOR *
LOADING
READY.
RUN
A
READY.
</pre>
 
=={{header|ALGOL 68}}==
1,489

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.