Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between "section" and "segment" in NASM

Tags:

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?

like image 239
CodyBugstein Avatar asked Feb 27 '13 23:02

CodyBugstein


People also ask

What is difference between section and segment?

Thus, a segment is almost exactly the same as a section, and the two can be used interchangeably. However, segment carries a secondary meaning and so can also be used when specifically talking about lines and planes in geometry. So you can use part whenever you want to talk about any piece of a larger entity.

What is Section in NASM?

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, .

What is the difference between the data segment and the text segment of in an assembly language program?

The text section is required in all assembly language programs. It is where the instruction codes are declared within the executable program. The data and bss sections are optional, but often used within a program. The data section declares data elements that are declared with an initial value.

What is BSS section in NASM?

In computer programming, the block starting symbol (abbreviated to . bss or bss) is the portion of an object file, executable, or assembly language code that contains statically allocated variables that are declared but have not been assigned a value yet. It is often referred to as the "bss section" or "bss segment".


1 Answers

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.

like image 196
Jester Avatar answered Oct 28 '22 08:10

Jester