Page 1 of 1

Going on with C++ or Pascal

Posted: Mon Jun 01, 2009 3:06 pm
by Cjreek
Hi,

It's quite embarrassing to me, but I've to ask this question. I'm sorry #-o

I never got in contact with a linker. I mainly used Borland Delphi and there you just press F9 and few (milli)seconds later your program runs. Now I don't know how to connect my assembly code with any code written in Pascal or C++.

I looked for useful information in osdev-wiki but I didn't find anything useful. (Maybe I just used wrong keywords). I also used the forum search. But without any success.

Would be glad if someone can tell me how to do this or maybe post a link to a guide or something :?

Cjreek

Re: Going on with C++ or Pascal

Posted: Mon Jun 01, 2009 3:26 pm
by NickJohnson
Well, it depends on your platform. If you're using the GNU toolchain under Cygwin/Windows or Linux, you can use the "ld" command to link object files. For example, if you have the files file1.s and file2.cc:

nasm -felf file1.s -o file1.o
g++ file2.cc -fno-builtin -o file2.o

ld file1.o file2.o -o kernel

will compile/assemble and link file1.s and file2.cc into the executable called kernel. That also assumes ELF executables; Windows uses PE, so you probably need a flag like -fpe or -fwin32 or something for NASM, also assuming you're using NASM.

From a code standpoint, you mix assembly and a high level language by declaring symbols global in the assembly and then using them using the "extern" keyword in C/C++ (idk about Pascal). The linker will connect them together. For C++, the symbols created for the C++ code are sometimes not what they seem, so it is harder to go from Asm->C++; I assume a similar thing is true for Pascal.

Re: Going on with C++ or Pascal

Posted: Mon Jun 01, 2009 4:03 pm
by Cjreek
Thanks for your answer.

At the moment I use following script to compile my asm file:
D:\NASM\nasm.exe boot.asm -f bin -o boot.o
This succeeds, but then this fails:
ld.exe pascal.o boot.o -o kernel.bin
Message:
boot.o: file not recognized: File format not recognized
if I try:
D:\NASM\nasm.exe boot.asm -f win32 -o boot.o
I get these errors:
boot.asm:37: error: COFF format does not support non-32-bit relocations
boot.asm:40: error: COFF format does not support non-32-bit relocations
Well.. I don't know what it means. Line 37 and 40 look like this:

Code: Select all

37   mov edi, [xpos] 
40   add byte [xpos], 2

[...]

xpos db 0
EDIT: Ok Probably you mustn't allocate memory in win32 like this... But well... Then -f win32 seems to be the wrong parameter.

:?

Re: Going on with C++ or Pascal

Posted: Tue Jun 02, 2009 1:52 am
by gedd
First of all, binary connot be linked with anorther executable format, like all executable format.

ld.exe pascal.o boot.o -o kernel.bin <-- will never work with 2 .o with different format

Follow this, you should read about executable format, linker, ... and also Wiki Osdev where you will found many answer
It seems you have too few knowlegde on os dev to begin now, so read read read before
Not a shame we have all done this :wink:

Re: Going on with C++ or Pascal

Posted: Tue Jun 02, 2009 2:09 am
by Love4Boobies
NickJohnson wrote:Well, it depends on your platform. If you're using the GNU toolchain under Cygwin/Windows or Linux, you can use the "ld" command to link object files. For example, if you have the files file1.s and file2.cc:

nasm -felf file1.s -o file1.o
NASM isn't part of the GNU toolchain. GAS, the GNU Assembler however is.

There are times when you may find inline assembly a better alternative (say you're working on a bootloader and you want to have all the code in one place - boot loaders aren't supposed to be portable so you don't need external object files). In Delphi it works like this:

Code: Select all

asm
   mov ax,02h
   inc ax
end;
while in G++ it works like this:

Code: Select all

asm(" mov $0x02,%ax; \
      inc %ax "); // although Intel syntax is also supported some opcodes are missing AFAIK
I have never used Borland's C++ inline assembler IIRC but I bet it has Intel syntax.

Re: Going on with C++ or Pascal

Posted: Tue Jun 02, 2009 5:38 am
by Cjreek
Sometimes I've got the feeling, that answers don't match to my question :?
Or maybe I don't have enough overview.
First of all, binary connot be linked with anorther executable format, like all executable format.
Ok, my question is how to create a binary format .o with pascal/C++ and link it with my assembly binary .o?

@Love4Boobies: I'm not sure what inline assembler's got to do with this question :?:

Re: Going on with C++ or Pascal

Posted: Tue Jun 02, 2009 10:39 am
by gedd
Cjreek wrote:Ok, my question is how to create a binary format .o with pascal/C++ and link it with my assembly binary .o?
And the answer was "all is in the wiki".
You can also search in all post wich have same subject.

Question that you must ask is :
- is my pascal compiler can produce binary format ? (not sure)
- is my c++ compiler can produce binary format ? (gcc + ld can)

Re: Going on with C++ or Pascal

Posted: Wed Jun 03, 2009 10:10 pm
by leledumbo
Ok, my question is how to create a binary format .o with pascal/C++ and link it with my assembly binary .o?
AFAIK, no recent Pascal compiler can create flat binary format (AFAIK, only TP can but it's an ooooold stuff). I use ELF from beginning and never have a need for flat binary.

Re: Going on with C++ or Pascal

Posted: Fri Jun 05, 2009 9:09 am
by System123
Hi I use Pascal for deving and here are my commands for compiling. I hope this helps

Code: Select all

nasm -f elf Stage3.asm -o obj/stage3.o
nasm -f elf Int.asm -o obj/Int.o

fpc -Aelf -CX -n -O3 -Os -OpPENTIUM -OoREGVAR -OoUNCERTAIN -OoNOSTACKFRAME -OoPEEPHOLE -OoLOOPUNROLL -OoTAILREC -Rintel -Sagic -Tlinux -uLINUX -vnhw -Xd -XX -Fu../rtl/units/i386-fpos -FUobj Kernel.pas 

LD2 -T min.ld -o ../Release/kernel obj/stage3.o obj/kernel.o obj/multiboot.o obj/console.o obj/ports.o obj/idt.o obj/gdt.o obj/isr.o obj/irq.o obj/pit.o obj/keyboard.o obj/pmm.o obj/vmm.o obj/heap.o obj/Sound.o obj/floppy.o obj/Int.o obj/Interrupts.o obj/Multitasking.o obj/VGANB.o obj/FATFS.o ../rtl/units/i386-fpos/system.o