Hi,
Pitchu wrote:1. If I use [SECTION] directive to create .TEXT and .DATA sections, after assembling will the BIN file contain .TEXT section first (which i believe) followed by .DATA?
Yes - ".text" then ".data" then ".bss", unless you define your own sections and change the default order.
Pitchu wrote:2. If I use %INCLUDE directive to include a file that defines a procedure and i want to use that procedure does the procedure need to be declared as GLOBAL in that included file and EXTERN in this file?
No. It's like the assembler uses "cut & paste" to insert all the source code from the %included file into the file that includes it.
GLOBAL and EXTERN are only needed when you link object files (for e.g. if you assemble both files to a ELF object files, and then link those ELF object files together).
Pitchu wrote:3.How can i write structures in NASM?
First, understand that in each field in a structure is just an offset from the beginning of the structure. With this in mind you could do:
Code: Select all
%define foo 0
%define bar 4
mov edi,[address_of_the_structure]
mov [edi+foo],eax
mov [edi+bar],ebx
NASM's preprocessor can do this for you to make it look like structures. For example:
Code: Select all
struc my_struct
.foo: resd 1
.bar: resd 1
endstruc
mov edi,[address_of_the_structure]
mov [edi+my_struct.foo],eax
mov [edi+my_struct.bar],ebx
The preprocessor also has support for defining structures:
Code: Select all
struc my_struct
.foo: resd 1
.bar: resd 1
endstruc
first_struct:
istruc my_struct
at .foo, dd 123456
at .bar, dd 1024
iend
mov eax,[first_struct+my_struct.foo]
mov [edi+my_struct.bar]
Note: The "at" keyword is just a macro that internally uses "times" to advance do the correct spot.
Pitchu wrote:4.If my Second-Stage Bootloader wants to refer to BPB of BootSector currents in memory will using %INCLUDE directive to include bootsector asm file in this file make it work?
Yes.
Another option would be to use "incbin" to include a pre-assembled binary (but you can't use labels defined in the source code for the boot sector from your second stage that way).
Of course if you do "%include" the bootsector then you can't really call your second stage a second stage - it'd just be more of the first stage. For e.g. for my latest floppy boot loader everything is "%included" into a single binary, but this binary is loaded from disk in pieces - the first sector is loaded by the BIOS, then the first sector loads 3 more sectors, then the remainder of the boot loader is loaded.
For example:
Code: Select all
bits 32
org 0x00007C00
jmp 0x0000:START
times ($$-$+0x00B) int3 ;Padding
%include "data for the BPB"
%include "code for the boot sector"
%include "more code for the boot sector"
%include "even more code for the boot sector"
times ($$-$+0x1fe) db 0x00 ;Padding
dw 0xAA55 ;Bootable magic number
;******* END OF FIRST SECTOR ******
%include "code for the intermediate sectors"
%include "more code for the intermediate sectors"
%include "even more code for intermediate sectors"
;***** END OF THIRD SECTOR ******
%include "code for the rest of the boot loader"
%include "more code for the rest of the boot loader"
%include "even more code for the rest of the boot loader"
align 512, int3 ;More padding
In this case I make sure that the first sector contains everything necessary to load the intermediate sectors, and make sure that the boot sector and the intermediate sectors combined contain everything necessary to load the remainder of the boot loader.
Because the first sector has limited space, before the intermediate sectors are loaded I have to use CHS addressing, can only load data below 1 MB, I load one sector at a time and I have relatively crappy error handling. After the intermediate sectors are loaded I use LBA addressing, I load multple sectors at a time (with "single sector only" fallback in case of errors), can load data anywhere below 4 GB and have extremely good error handling.
However, despite all of this it's still a single stage because it's all a single binary.
Cheers,
Brendan