3
votes

In NASM assembler, it's possible to declare local label using . prefix.

  1. So, what is the address of local label (and label in all assemblers generally)? Is it relative or absolute, or it depends on use?

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.

  1. But if I take label and I use it with 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?

2
You probably want bits 32 at the top of this. Your origin seems a little high for 16-bit code (which is what Nasm will produce by default. jmp label uses relative addressing mode - the actual code emitted will be jmp distance_to_label. Shouldn't matter if the label is "local" or not. For an absolute jump, you'll have to do mov eax, label and then jmp eax. You shouldn't need to code short - Nasm should give you a short jump if it'll fit, and a near jump if it won't. I'm not sure I get the part about "randomly generated" labels...Frank Kotler
So if I want list of 32-bit absolute values generated by NASM, what should I do?user35443

2 Answers

6
votes

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
2
votes

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: