Create an object at a given address: Difference between revisions

m (→‎{{header|Wren}}: Code formatting.)
Line 1,107:
Integer object value changed to: 43 but still at address 0x55a56e0f2dd8.
</pre>
=={{header|Z80 Assembly}}==
When writing assembly yourself, you'll know any object's memory location in advance.
This code creates the 16-bit integer object 0xFFFF at memory address 0xC000:
<lang z80>LD HL,&FFFF
LD (&C000),HL</lang>
 
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
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:
<lang z80>
ld hl,&C000 ;get the address of our variable.
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.
 
{{omit from|AWK}}
1,489

edits