I am learning assembly (z80 and x86) and am now coming to grips with building binaries using an assembler.
How is it possible to use labels with absolute (as opposed to relative) addresses?
From what I understand, an assembler will translate a label to a memory address on compilation, but how is it possible for the assembler to know which address a label will reside in at runtime?
It seems simple enough for a z80 bare metal system, as you can load a program into a specific memory address and send an RST
signal that will point the program counter to 0000h
. What happens when there is an operating system running?
Wouldn't code executed within an operating system not know its starting address (and thereby have no way of using non-relative opcodes like call
and ret
on labels)?
If machine code is not position-independent, there are two common strategies:
include additional information (AKA relocation information) in the executable file that would tell the OS where the absolute addresses are that need to be adjusted
just load the executable where it wants, meaning that you may need to first evict another one OR you need to provide individual address spaces for every one, so there's no fighting for the right spot in the first place