Author Topic: How to make one binary file using multiple source files  (Read 16101 times)

Offline VishalPawale

  • Jr. Member
  • *
  • Posts: 10
  • Country: in
How to make one binary file using multiple source files
« on: August 23, 2011, 04:41:25 PM »
Hi Guys, how can i make a single o/p binary file using 2 asm files.I have tried %include directive bt its not working.I have a statement of org in first file n then i have organised second file at some location then i want to make jump to second file,so how can i make it????
thnx in advance

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: How to make one binary file using multiple source files
« Reply #1 on: August 23, 2011, 06:49:38 PM »
Hi VishalPawale,

We may need more information before we can answer your question. Exactly how is %include "not working"? Errors from Nasm? Or does it assemble and the resulting binary not work as intended? Maybe what you "intended" and what happened instead, too...

The %include directive is essentially an automated "cut-and-paste" operation - as if you had pasted the %included file into the first file, at the point of the %include directive. This "should work" in most cases, I would think. Multiple "org" directives or multiple uses of the same symbol name (variables or labels in code) would screw it up.

Another approach might be to assemble "file2.asm" first and use the incbin directive to include the result into "file1.asm"...

http://www.nasm.us/xdoc/2.09.10/html/nasmdoc3.html#section-3.2.3

This will probably work best (at all?) if "file2" is "Position Independent Code" (PIC). We can discuss what is, and is not, PIC, if necessary...

Another possible approach might be to assemble both files into linkable object formats, link 'em, and use an "exe2bin"-type utility to convert the result back into a binary file. This is probably not what you want.

Knowing how Nasm handles the "known" names, ".text", ".data", and ".bss" (case sensitive!) might help. If there are multiple ".text" (and/or ".data" and/or "bss") sections, they are combined (in "source-code order"), then ".text" comes first, followed by ".data", followed by ".bss'". ".bss" sections do not become part of the file - essentially Nasm just defines offsets to variable names.

Using "arbitrary" section names allows more flexibility. See:

http://www.nasm.us/xdoc/2.09.10/html/nasmdoc7.html#section-7.1.3

In particular, "vstart=" has the effect of a second "org" (which Nasm does not allow), if that's what you need.

I suspect that a plain %include is your best bet, so we should probably start by knowing how/why that's "not working". If you need "fancier" tricks, we can get into that...

If you wish to post some code, please use "code tags" - put the word "code" between square brackets ("[]" - just like a Nasm memory reference) at the beginning of your code, and "/code" (in "[]"s) at the end. If your code is "too long to paste"... I think you have to have made some minimum number of posts (5?) before you can "attach" anything (spam prevention). Partial code may suffice...

Talk to ya soon!

Best,
Frank


Offline VishalPawale

  • Jr. Member
  • *
  • Posts: 10
  • Country: in
Re: How to make one binary file using multiple source files
« Reply #2 on: August 23, 2011, 07:20:19 PM »
Hi Frank,
When I use %include directive it assembles but doesn't work as intended.Also i have checked both the files for conflict of symbols n i used org for single time when i was trying %include directive.
Actually Im trying to develop basic two stage boot-loader.So I want to use org in my both first stage file n second stage file.So that i can give a memory location where i have assembled my second file n first file will contain a jump to that location.

thnx for reply :)

Offline VishalPawale

  • Jr. Member
  • *
  • Posts: 10
  • Country: in
Re: How to make one binary file using multiple source files
« Reply #3 on: August 23, 2011, 07:36:17 PM »
Here is my code-
its a partial code

Code: [Select]

bits 16 ;16-bit m/c

%macro startprint 0
%%loop1:
lodsb ;it copies the byte pointed by si or esi into al and then increaments to point to next byte
or al,al
jz %%printdone ;if no data then return
mov ah,0eh ;0eh is teletype function used to type letter in al register on screen
int 10h
jmp %%loop1
%%printdone:
%endmacro

org 7c00h ;location of boot
start:
jmp loader
msg db 10,13,"success",0
.
...
.........
comeout:
db 0eah
mov ax,2401h
int 15h
dw 0000h
dw 0ffffh
dw 0000h
dw 0ffffh
readytoload:
popa
mov si,keyispressed
startprint
mov ax,2401h
int 15h
        times 510-($-$$) db 0
dw 0xaa55

second file-

Code: [Select]

mov si,msg
loop:
lodsb
or al,al
jz done1
mov ah,0eh
int 10h
jmp loop
done1:
hlt

main file-
Code: [Select]
%include "boot1.asm"
%include "boot2.asm"
thnx

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: How to make one binary file using multiple source files
« Reply #4 on: August 23, 2011, 10:18:39 PM »
Okay... I don't think the problem is with %include, exactly. What I usually do in a similar case is to assemble the two files separately, and "copy /b boot1.bin + boot2.bin img.bin" (actually, I use "cat" in Linux) and then load "img.bin" to the floppy(?) as one file. The %include option, as you've got it, "should" do exactly the same thing (I think). (assembling as two separate files will allow a separate "org" in boot2.asm)

The bios is going to load only the first 512 bytes (first sector) to 7C00h. This code is going to have to load the second sector to 7E00h before jumping to it or otherwise using it. If the "loader:" label (not shown), which you jump to immediately (as shown) is in "boot2.asm", this is going to fail! If it's in "boot1.asm", you should be okay.

You don't show some "setup" that I'd expect in a bootsector - setting ds (and es?) explicitly to zero, in particular. This may not fail... your bios may leave ds and es set to zero - some do. 40h is another common value the bios may leave in ds and es when it delivers us to 7C00h - this would probably fail. Emulators (Bochs or VMware or similar) are "anybody's guess"  - I have no experience with 'em. My advice is "assume nothing" (except the location 7C00h and the boot drive in dl - this is almost certainly 0 or 80h), and explicitly set ds and es - and perhaps set up a "known" stack - very early in your boot code. Assuming cs=0 is "almost always" okay, but there are rumored exceptions. The bios has presumably got a useable stack... someplace... so you may be able to defer this. "Assume nothing" is safest!

I'm really doubtful of your code shown at "comeout:". When (if) the CPU hits "db 0EAh" it's going to interpret it as a far jump (of course), and will "gobble up" your "mov ax, 2401h" and half of your "int 15h" as expected operands. I'm pretty sure this isn't going to work. I'd suggest you check the carry-flag after the "int 15h" before assuming you've got a20 enabled, in any case. I'm not sure of the purpose of the following "dw"s - I doubt if you intend to execute 'em!

Another possible "problem": "hlt" only halts the CPU until an interrupt occurs - which won't be long, unless you've got 'em disabled. After that, it'll "fall through" to... who knows where. I'd jump back to "done1:" to catch it. I don't think you're getting there, so it shouldn't be a problem... yet...

I like to set bx explicitly to 7 - at least bh to zero - before int 10h/0Eh. Depends on (video) bios whether this is actually necessary...

You don't "ret" or explicitly jump to anywhere at the end of "readytoload:". If this is intended to "fall through" into "boot2", it's going to execute your padding and the boot signature first. This might not cause a problem, but I wouldn't do it!

That's about all I "see" at the moment. If I get to it, I may attempt to get up a "working" example(s)... don't count on it...

Best,
Frank


Offline VishalPawale

  • Jr. Member
  • *
  • Posts: 10
  • Country: in
Re: How to make one binary file using multiple source files
« Reply #5 on: August 24, 2011, 03:39:45 PM »
Hi Frank,
due to space constraints on forum im attaching the source of both the source files so that you can get clear idea
Also if i assemble the one single file which is combination of both files it gives me intended o/p and with the single file i have already entered into PM.But i want to include some more functionalities to boot-loader so i am going for 2-stage loader.The file boot2.asm is just taken for cheking whether i am able to link multiple source files.In my second file Im gonna do initialisation of my GDT's.

thnx.....:)

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: How to make one binary file using multiple source files
« Reply #6 on: August 24, 2011, 06:55:18 PM »
You're not loading sector 2 before jumping to it! I wrote up a little "load2" routine, just from memory. Still didn't work. Further examination revealed the after you're done printing dots, you jump to "comeout:", not "readytoload:". Fixed that, and I finally see "sucess"! This is a half-baked, quicky job, but it "works for me"...

Happy bootin',
Frank


Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage

Offline VishalPawale

  • Jr. Member
  • *
  • Posts: 10
  • Country: in
Re: How to make one binary file using multiple source files
« Reply #8 on: August 28, 2011, 07:08:15 AM »
Hi Frank,
Thanx a lot for reminding me to jmp to second sector.........
But its not working for me.......:(
It get build but at emulator it didnt jump to second sector i.e. its no showing me that "success" message.Do I need to initialize my DS n ES again?(Actually I dont think so)

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: How to make one binary file using multiple source files
« Reply #9 on: September 01, 2011, 03:03:27 AM »
Ah, we're back!

"Assume nothing", I said, and then I proceeded to assume that the bios (or emulator) had left me a useable stack, and that I had booted from drive zero. This should eliminate those two assumptions. (not much different from the last one) See if it helps...

Best,
Frank