Create an object at a given address: Difference between revisions

m
→‎{{header|Z80 Assembly}}: formatting of code blocks
m (→‎{{header|Z80 Assembly}}: formatting of code blocks)
Line 1,114:
 
Loading a value into a register from memory only loads a copy; the original value at that memory location isn't altered, nor is the memory location of that value. Assume this code is executed immediately after the above example:
<lang z80>LD HL,(&C000) ;load &FFFF into HL
LDINC HL,(&C000) ;load &FFFF into ;HL now equals &0000
inc hl ;HL now equals &0000
LD BC,(&C000) ;load &FFFF into BC.</lang>
 
In order to change a value at a memory location, it either needs to be loaded into a register and stored back after altering that register in some way, or by using certain indirect addressing modes, like so:
ld<lang hlz80>LD HL,&C000 ;get the address of our variable.
<lang z80>
incINC (hl) ;increment the low byte.
ld hl,&C000 ;get the address of our variable.
ldLD hlHL,(&C000) ;load &FF00 into HL.</lang>
inc (hl) ;increment the low byte.
ld hl,(&C000) ;load &FF00 into HL.</lang>
 
If you're not familiar with the Z80's syntax, this can be a bit confusing. The brackets are like the dereference operator in C, and not having brackets is like the unary <code>&</code> operator in C. The Z80's ability to do 16-bit operations is limited; it can load from two consecutive memory locations into a 16-bit register pair, however when using <code>(hl)</code> as an operand it is only operating on the lower 8 bits. When learning Z80 Assembly, it is very helpful to view a hex editor while learning the instruction set.
1,489

edits