32

I am using some baby NASM programs to help me learn the language.

From what I've read, NASM programs can have three sections; the .data, the .bss, and the .text which is mandatory. However I am finding very often that sometimes the names of the divisions are section and other times it's segment.

For example with this "Hello World" I found online:

;  hello.asm  a first program for nasm for Linux, Intel, gcc
;
; assemble: nasm -f elf -l hello.lst  hello.asm
; link:     gcc -o hello  hello.o
; run:          hello 
; output is:    Hello World 

    SECTION .data       ; data section
msg:    db "Hello World",10 ; the string to print, 10=cr
len:    equ $-msg       ; "$" means "here"
                ; len is a value, not an address

    SECTION .text       ; code section
        global main     ; make label available to linker 
main:               ; standard  gcc  entry point

    mov edx,len     ; arg3, length of string to print
    mov ecx,msg     ; arg2, pointer to string
    mov ebx,1       ; arg1, where to write, screen
    mov eax,4       ; write sysout command to int 80 hex
    int 0x80        ; interrupt 80 hex, call kernel

    mov ebx,0       ; exit code, 0=normal
    mov eax,1       ; exit command to kernel
    int 0x80        ; interrupt 80 hex, call kernel

If I change every instance of SECTION to SEGMENT it will still work.

Can someone explain the difference between the two, if any?

2 Answers 2

37

From the nasm documentation:

The SECTION directive (SEGMENT is an exactly equivalent synonym)

Nasm can produce output in various formats, some of which support sections. Certain section names can be arbitrary (such as the three you listed), for them only the section flags count. The predefined ones are just convenience shortcuts, .text is marked as containing code, .data as read-write initialized data and .bss as zero-initialized read-write data. You could put your code in a section named foo as long as it was marked as a code section, and you can use multiple sections as you see fit.

Sign up to request clarification or add additional context in comments.

1 Comment

I did a lot of Googling and didn't find anything... Should have looked in the documentation too, I apologize. Thanks for the answer though and for the link. Perhaps this thread will be helpful to future morons at least
4

Also, it's worth to note that you can define your own sections with special flags (and change flags on default sections) which is elf extensions to the section directive. From the docs:

Like the obj format, elf allows you to specify additional information on the SECTION directive line, to control the type and properties of sections you declare.

For instance, you can create a section like this:

section .special write

After assembling you can examine it: readelf -S test.o

[3] .special          PROGBITS         0000000000000000  00000250
   0000000000000005  0000000000000000  WA       0     0     1

1 Comment

Very useful! Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.