Anthony
Anthony

Reputation: 93

How assemblers translate labels to addresses

First off I am writing this from a mobile device so please excuse spelling mistakes.

I am writing an assembler in C and I understand how to assemble basic assembly instructions and assembler macros how ever labels really have me stuck.

Say in the following code which simply increments the A register 10 times:

LDX #$00

label:

INC
INX
CPX #$0A
BNE label

How does the assembler know the address of label:? Also what mechanism does it use to associate the label in the BNE instruction to the label definition before the loop?

And in addition to that, say a hardware interrupt is triggered in the loop (which is unknown to the assembler because it happens at run time). The PC would change and thus any label in the interrupt service routine would be at a completely different address and because most RISC processors use PC relative addressing this would change the labels addresses.

Also as a side note how do I paste code correctly so it doesn't show up on one line like it does now?

Upvotes: 3

Views: 1881

Answers (1)

The general concept is relatively simple.

If the label comes before the reference, as it does in your example, the assembler records the label name along with the current instruction pointer in a table for later reference. In the assembler, the current instruction pointer is going to be relative to the current code block - it's the linker's responsibility to establish the overall memory layout of the finished program and hence the real IP associated with the label. The jump instruction looks up the address to jump to in the table and if the address is absolute also makes a note that the address must be fixed up during linking to point to the real (not section-relative) address.

If the jump occurs before the label it's a bit more complicated but not much:

The asssembler records the reference to the label name in a table and emits a jump with a dummy address. Later, when the label definition is encountered and the instruction pointer is therefore known, the assembler goes through the table of references to this label and patches in the - now known - address.

-

Interrupt handlers must preserve all state, so those have no bearing on the above.

Upvotes: 5

Related Questions