Jump to content

Null object: Difference between revisions

mNo edit summary
Line 2,175:
<lang zkl>if(Void == n) ...
return(Void)</lang>
 
=={{header|Z80 Assembly}}==
{{trans|ARM Assembly}}
Even though this doesn't really apply to assembly, as there is no "null pointer" per se (that is, all data is a number), it would be interesting to demystify what <code>NULL</code> really is. At the lowest level, the null pointer is just a pointer to a memory location that is of no use to the programmer. The Z80 does not segfault, so any memory address we read is fair game. So without the hardware enforcing <code>NULL</code>, we need to pick an address we don't mind losing. Typically, using <code>&0000</code> to equal [[C]]'s <code>NULL</code> is acceptable, as address <code>&0000</code> on any randomly chosen Z80-based hardware is typically a jump to the kernel's entry point (or the main program's entry point, depending on the implementation.)
 
<lang z80>ld a,(&0000) ;dereference the null pointer as a uint8
ld hl,(&0000) ;dereference the null pointer as a uint16</lang>
 
Typically, you would get 0xC3 when dereferencing as an 8-bit value and 0xC3nn when dereferencing as a 16-bit value, where nn is the low byte of the address of the aforementioned entry point. Neither of these are particularly useful, so having <code>&0000</code> as the null pointer is perfectly fine. Although there are technically other options, most CPUs can compare with zero more efficiently than most other numbers, and the Z80 is no exception. Of course, the Z80 will not check if a pointer is NULL for you, so you have to do it yourself:
<lang z80>LD HL,myPointers
;there is no LD BC,(HL) so we have to do this:
LD c,(hl)
inc hl
LD b,(hl)
;and compare to null
LD a,b
or c ;compare BC to zero
JR z,isNull</lang>
 
{{omit from|GUISS}}
1,489

edits

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