What is $ in nasm assembly language? [duplicate]

2019-02-25 08:11发布

问题:

This question already has an answer here:

  • What does the dollar sign ($) mean in x86 assembly when calculating string lengths like “$ - label”? [duplicate] 4 answers

This is my assembly level code ...

section .text
global _start
_start  mov eax, 4
        mov ebx, 1
        mov ecx, mesg
        mov edx, size
        int 0x80
exit:   mov eax, 1
        int 0x80
section .data
mesg    db      'KingKong',0xa
size    equ     $-mesg

Output:

root@bt:~/Arena# nasm -f elf a.asm -o a.o
root@bt:~/Arena# ld -o out a.o
root@bt:~/Arena# ./out 
KingKong

What is the $ in the line size equ $-mesg. Some one please explain the about the $ symbol used ...

回答1:

$ indicates the 'current location' of the assembler as it goes along. In this case it's used to store the length of the mesg string.

size equ $-msg

Says "make a label size and set it equal to the current location minus the location of the mesg label". Since the 'current location' is one past the end of the string "KingKong\n", size is set to that length (9 characters).

From the documentation:

NASM supports two special tokens in expressions, allowing calculations to involve the current assembly position: the $ and $$ tokens. $ evaluates to the assembly position at the beginning of the line containing the expression; so you can code an infinite loop using JMP $. $$ evaluates to the beginning of the current section; so you can tell how far into the section you are by using ($-$$).



标签: assembly nasm