vjain27
vjain27

Reputation: 3674

sections in assembly language program

I am not clearly understanding how sections (declared using section directive) in an assembly program are handled by the assembler and the linker. Following are are some of the queries :

I know I can write assembly programs to test if a program will work in each case but I want to get the concept clear.

Upvotes: 8

Views: 8780

Answers (1)

wallyk
wallyk

Reputation: 57764

Sections are no more than independent memory sequences. Each new byte of data is placed into the currently open "program section". It is quite convenient while writing a function to have the associated data quite close in the source code, even though when it is loaded into memory it might be megabytes or gigabytes distant.

User defined program sections work the same way as the standard sections, though you will usually have to provide additional information to the linker and other code post-processing tools so that they are loaded into memory in a sensible way.

You can place executable code in a data section and vice versa, and most assemblers will not even warn about it. Executing code from a data section might require a little trickery; the reverse is usually easy.

Assemblers handle sections typically by writing the equivalent into the object module in the same order as the source code, leaving the rearrangement of like sections together to the linker. Only the most simplistic assemblers don't provide this ability. The original MSDOS .COM file assembler comes to mind.

Different assemblers have different philosophies about coddling the programmer. The traditional tactic is to assume that an assembly language programmer knows what they are doing, and doing literally what is written except what is not understood. Other assemblers are more helpful (or a pain in the ass, depending on your point of view), and complain about misaligned multi-byte structures, data or code type mismatches, etc., etc.

Based on the "helpfulness" of an assembler, failure to initiate a program section results in default behaviour (usually an assumed code .psect), or refusal to assemble with a fatal error. Even the most structured assembler does not care if there is no text, data, or bss. In fact, there are useful object modules consisting only of symbol definition with no data (or code) bytes at all.

Upvotes: 4

Related Questions