Reputation: 6413
In NASM
assembler, it's possible to declare local label using .
prefix.
I'm asking because there are features that confuse me. This is an example code:
ORG 0x400000 ;origin of address for labels
start: ;address here should be 0x400000
..... ;some code here
.loop ;local label
..... ;some code here
jmp short .loop ;<------- address is not taken as absolute
jmp short start
If I take some normal label (like start
) for referencing and I use it with lea
instruction, address is calculated as normal absolute address with respect to origin.
short
(as on the last line), what is happening? Is the offset for jump calculated from absolute address? I'm asking all this because I have local labels in my code (.LNXYZ
, randomly generated), and I need to make list of addresses (from that labels) that will have 4-byte elements containing absolute address for jumps. Is such thing possible, or I have to use normal labels? Is there any directive for it?
Upvotes: 3
Views: 5066
Reputation: 363
From NASM user manual:
3.9 Local Labels
NASM gives special treatment to symbols beginning with a period. A label beginning with a single period is treated as a local label, which means that it is associated with the previous non-local label. So, for example:
label1 ; some code .loop ; some more code jne .loop ret label2 ; some code .loop ; some more code jne .loop ret
In the above code fragment, each JNE instruction jumps to the line immediately before it, because the two definitions of .loop are kept separate by virtue of each being associated with the previous non-local label.
This form of local label handling is borrowed from the old Amiga assembler DevPac; however, NASM goes one step further, in allowing access to local labels from other parts of the code. This is achieved by means of defining a local label in terms of the previous non-local label: the first definition of .loop above is really defining a symbol called label1.loop, and the second defines a symbol called label2.loop. So, if you really needed to, you could write
label3 ; some more code ; and some more jmp label1.loop
Upvotes: 6
Reputation: 383070
The address of a local label in NASM is exactly the same as it would be if the label were not local.
The only thing that changes is that the label's name gets appended to the first previous non-local label.
Minimal example:
outside_label:
; This should be not done in practice,
; but shows how it works under the hood.
jmp outside_label.inside_label
; This is not reached.
.inside_label:
; This is what you should do in practice.
; Labels also get appended when used as arguments.
jmp .inside_label2
; This is not reached.
.inside_label2:
Upvotes: 2