Dynamic variable names: Difference between revisions

mNo edit summary
Line 1,427:
wilfred = 5
</pre>
 
=={{header|Z80 Assembly}}==
This example is admittedly crude but was made to be as simple as possible. To that end, the variable name was made to be only one letter long. A key press is taken from the user, which is used as an offset into an array of 256 null bytes, and then a value is stored at that offset and retrieved from there. Self-modifying code is used to store the user's input as the offset of the <code>IX</code> register. A more practical implementation would store this value into normal RAM first so that it can be more easily retrieved.
 
Both the user variable name and the value of that variable are printed. The value is set to 0x42 automatically before printing it.
 
<lang z80>org &8000
WaitChar equ &BB06 ;Amstrad CPC BIOS call, loops until user presses a key. That key's ASCII value is returned in A.
PrintChar equ &BB5A ;Amstrad CPC BIOS call, A is treated as an ASCII value and is printed to the screen.
getInput:
call WaitChar
;returns key press in A
 
or a ;set flags according to accumulator
jp m,getInput
;most keyboards aren't capable of going over ascii 127
;but just in case they can prevent it.
;IX/IY offsets are signed, thus a key press outside of 7-bit ASCII would index out of bounds
push af
call PrintChar ;prints the user variable name to the screen.
pop af
call NewLine
 
ld (LoadFromUserNamedVariable+2),a ;offset byte is at addr+2
ld (StoreToUserNamedVariable+2),a
 
; This self-modifying code turns both instances of (IX+0) into (IX+varname)
ld a,&42 ;set the value of the dynamically named variable
; to &42
ld ix,ExtraRam ;storage location of dynamically named variables
StoreToUserNamedVariable:
ld (IX+0),a ;store 42 at the named offset
;"+0" is overwritten with the dynamic user ram name
xor a
dec a
;just to prove that the value is indeed stored where the code
; is intending to, set A to 255 so that the next section of
; code will show that the variable is indeed retrieved and
; is shown to the screen
LoadFromUserNamedVariable:
ld a,(IX+0) ;retrieve the value at the stored offset. The "+0" was overwritten with the user-defined offset.
call ShowHex ;prints to the terminal the value stored at the dynamically named user variable
 
ReturnToBasic
RET
 
ShowHex: ;credit to Keith S. of Chibiakumas
push af
and %11110000
rrca
rrca
rrca
rrca
call PrintHexChar
pop af
and %00001111
;call PrintHexChar
;execution flows into it naturally.
PrintHexChar:
or a ;Clear Carry Flag
daa
add a,&F0
adc a,&40
jp PrintChar
;ret
 
NewLine:
push af
ld a,13 ;Carriage return
call PrintChar
ld a,10 ;Line Feed
call PrintChar
pop af
ret
 
 
org &9000
ExtraRam:
ds 256,0 ;256 bytes of ram, each initialized to zero</lang>
 
{{out}}
<pre>
Ready
run"go.bas
s
42
Ready
</pre>
 
 
 
=={{header|zkl}}==
1,489

edits