Question

What is the technical reason NASM programs are broken down into three "sections"; .data, .bss and .text ? What is really happening behind the scenes? Why does the assembler need to know in advance if there are constants, why not just take them as the program goes?

Was it helpful?

Solution

First off, you don't always need to use those sections. You can include instructions in the data section and data in the text section on plenty of systems.

There are some good reasons to have them, though.

  1. If your operating system and hardware are sophisticated enough, you can map certain pages "non-executable", so you can provide some protection from bugs where you jump off into the data section by accident.

  2. Likewise, you can mark the text section "read-only", so you can't accidentally modify your code with an errant pointer.

  3. Lastly, the bss section contains only zero-initialized data, so marking it as such at assembly time can reduce the size of your executable file - the loader can just memset the right number of zeroes before jumping into your program, rather than occupying a bunch of bytes of storage just to hold a bunch of zeroes.

OTHER TIPS

I can't speak for NASM's design, but the three sections .text, .bss, and .data hold code, initialized variables, and uninitialized variables, respectively, and are mapped as such with appropriate access flags in the resulting binary file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top