Page 1 of 1

NASM Question

Posted: Sat Mar 22, 2008 10:44 pm
by Pitchu
Well I have been using Nasm for a long time but never-ever used nasm to its best. I have certain questions regarding Nasm:

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?

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?

3.How can i write structures in NASM?

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?

There are lots of questions but please do answer me these.
LOL

Re: NASM Question

Posted: Sun Mar 23, 2008 12:50 am
by Brendan
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

Posted: Sun Mar 30, 2008 5:51 am
by Pitchu
Thanks a lot for this.
What i am interested in is to use the hardware information gathered by bootloader in a structure (say CPUID,RAM) and later use that information by kernel. The problem is that i need to know the address of that structure and further i cant use %incbin as the structure is populted at runtime.

Further, if i use %incbin to include bin file, i believe the size will increase.