5

$$ is defined as current segment address in NASM. But what's the real meaning of it? I wrote two asm files to test it:

a.asm

extern another

[section .text]
global _start
_start:
    mov ebx, $$
    call another

b.asm

[section .text]
global another
another:
    mov eax, $$
    ret

compile

nasm -f elf a.asm -g
nasm -f elf b.asm -g
ld -o test a.o b.o

Using gdb to debug the final file test, I found that though I defined two sections with the same name, the $$ is different in both file. So I guess that:

  1. Once I defined a section in a file, the value of $$ is the starting address of that section. And $$ has nothing to do with the so-called segment registers(cs,ss, fs, gs, .etc).
  2. If I have defined the another section with the same name in other file, it's interpreted as a different section. But if the two sections with the same name are defined in the same file, whether there are other section definitions between them, it's always interpreted as the same section, with the same $$ value. Such as follows, the two .text sections are just the same.

    [section .text]
    global _start
    _start:
        mov ebx, $$
    
    [section .d]
    d:
        mov ecx, $$ 
    
    [section .text]
    another:
        mov eax, $$
        ret
    
  3. I guess there are some section names that NASM can recognize and put them to the right place when compiling. Such as .data, then what are these section names that NASM can recognize and do something with them? Thanks a lot!

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Searene
  • 25,920
  • 39
  • 129
  • 186

1 Answers1

10

$$ is the address of the beginning of the current section. It is a relocatable value (not "scalar" - a word you will find in an error message, but not in the Manual). It is an offset, so doesn't care what's in a segment register.

About the only thing it's useful for is $ - $$, the length of the section so far. $ - $$ is a "scalar" (as is any difference between labels) and can be used in expressions which would otherwise cause Nasm to whine about "not a scalar value".

The section names "known" to Nasm depend on the output format - "-f obj" doesn't know any at all. .text, .data, and .bss are pretty universal - some output formats know others. Best place to find 'em is in the "output format" chapters in the Manual. http://www.nasm.us if you didn't get the Manual with your download. These names are case sensitive, and the leading '.' is required.

I have the feeling that there's a "question" in here that I'm missing. What are you actually trying to do?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Frank Kotler
  • 3,079
  • 2
  • 14
  • 9
  • If you use `$$` on its own, it seems to normally just be `0`, regardless of `org`, even in a flat binary. Would it be non-zero if you'd used a 16-bit segment that wasn't paragraph-aligned? Or is it truly just a magic modifier for `$` to get a position / size instead of an absolute address, with no use on its own? – Peter Cordes Jun 09 '20 at 09:20
  • 2
    @PeterCordes That doesn't seem to be true. When assembling the original poster's final example with `-f bin` gives a non-zero value for $$ in the `.d` section as expected. If an `org 100` is inserted then all the $$ values are non-zero. – Ross Ridge Sep 04 '20 at 03:37
  • "`$ - $$` is a "scalar" (as is any difference between labels)" -- Correction: Any difference between two labels *in the same section* is a scalar. – ecm Sep 04 '20 at 08:44