Going on with C++ or Pascal

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Cjreek
Member
Member
Posts: 70
Joined: Thu May 28, 2009 2:41 pm
Location: Germany

Going on with C++ or Pascal

Post 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
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: Going on with C++ or Pascal

Post 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.
Cjreek
Member
Member
Posts: 70
Joined: Thu May 28, 2009 2:41 pm
Location: Germany

Re: Going on with C++ or Pascal

Post 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.

:?
gedd
Member
Member
Posts: 104
Joined: Thu Apr 10, 2008 1:47 am

Re: Going on with C++ or Pascal

Post 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:
[ Grub 2 | Visual Studio 2013 | PE File ]
The OsDev E.T.
Don't send OsDev MIB !
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: Going on with C++ or Pascal

Post 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.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
Cjreek
Member
Member
Posts: 70
Joined: Thu May 28, 2009 2:41 pm
Location: Germany

Re: Going on with C++ or Pascal

Post 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 :?:
gedd
Member
Member
Posts: 104
Joined: Thu Apr 10, 2008 1:47 am

Re: Going on with C++ or Pascal

Post 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)
[ Grub 2 | Visual Studio 2013 | PE File ]
The OsDev E.T.
Don't send OsDev MIB !
leledumbo
Member
Member
Posts: 103
Joined: Wed Apr 23, 2008 8:46 pm

Re: Going on with C++ or Pascal

Post 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.
System123
Member
Member
Posts: 196
Joined: Mon Jul 07, 2008 1:25 am

Re: Going on with C++ or Pascal

Post 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
Gizmic OS
Currently - Busy with FAT12 driver and VFS
Post Reply