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?
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.
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, .
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.
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".
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With