Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NASM Specific -- Section vs [SECTION ]

Tags:

assembly

nasm

I cannot seem to find anything clear in NASM's documentation regarding the difference between using Section or [SECTION ] (with the brackets) in your code. I am aware these are macros, but I see them used almost interchangeably. Is this the case? In other words is

[SECTION .text]

Equivalent to

Section .text

?

Do brackets maybe imply some arcane side effect?

Thanks

like image 956
in70x Avatar asked Oct 11 '22 03:10

in70x


1 Answers

[SECTION .xyz] is the primitive form of the section directive which simply sets the current output section, SECTION .xyz differs slightly because it works like a macro:

SECTION .text

expands to the two lines

%define __SECT__ [SECTION .text] 
[SECTION .text]

which can be used in conjunction with a macro to temporarily switch the output section, and switch it back to its original value. Example from the NASM manual:

%macro  writefile 2+

[section .data]

%%str:        db      %2
%%endstr:

    __SECT__

    mov     dx,%%str
    mov     cx,%%endstr-%%str
    mov     bx,%1
    mov     ah,0x40
    int     0x21

%endmacro

When you use this macro, the output section is set to .data temporarily by the primitive form of SECTION, and set back to its original value with __?SECT?__

like image 56
matja Avatar answered Oct 18 '22 08:10

matja