Author Topic: Sections, to use or not to use?  (Read 7943 times)

Offline fredericopissarra

  • Full Member
  • **
  • Posts: 373
  • Country: br
Sections, to use or not to use?
« on: June 23, 2023, 05:59:03 PM »
NASM (and other assemblers) usually have directives to organize you code in sections. These sections can be viewed as "segments" in real mode, but they are only a way to organize code.

Your operating system loads an executable in blocks. There is an executable block (.text), data blocks (.data, .rodata, .bss), and more. The keyword section tells the assembler what attributes these sections have - In systems like Windows and Unixes, the sections starting with a single dot are system sections with default attributes. You can create your own. In the old MSDOS, this dot is substituted by an undescore, like _TEXT, _DATA and _BSS (and, for some linkers you must tell the actual class: _TEXT has class CODE.

In a Master boot record, for example, you don't need sections. A single sector is loaded from disk via interrupt 0x19 at the segment 0, offset 0x7c00 (this is STANDARD), with CS=DS=ES=0 (why? Because BIOS Data area starts at segment 0x40, and can be accessed via offset 0x4xx, at segment 0). You can code your MBR not using sections (because you are not creating an EXE file!).

BTW, NASM allows use of sections for binary output. .text, .data and .bss are simply mixed, in that order in a flat binary file. Since there's no header, no metadata, the information on those sections are lost in the final file. So, why bother to use them?

If you are coding an UEFI bootloader (PE or PE+ executable, for i386 or x86-64 modes, respectifully), then sections are important, but not for single binary legacy master boot records.

Offline Deskman243

  • Jr. Member
  • *
  • Posts: 49
Re: Sections, to use or not to use?
« Reply #1 on: June 24, 2023, 12:16:26 PM »
As an enthusiast of these types of models I'd say this is a really good platform for these concepts. My objection right now is learning how to bridge these sections in a smooth way being that the sections were designed by previous models of electronic and microprocessor designs .The first book I ever bought for this was for a simple graphics program in multi-platform assembly (Z80,5502,68000,8086 and ARM) by ChibiAkumas. Things get more convoluted the more you rely on sections because of the new electronic.To observe this one can consider a major difference between 80816 and 80836 was the amount of datalines to give segments from 16 bits to 32 (where protected mode was also created). Speaking of books the Advanced Processor series by Dr. Godse does a quintessential job at illustrating the early pre 32 bit version processors.

I think as an overview if I had more resources I would be more likely to try other methods however inferential. All I know is that standard BIOS designs more data lines to give segment lines more data addresses in memory. That is like alright but what I wish I could understand is how to build designs around this to comply in a similar smooth way.