Going on with C++ or Pascal
Going on with C++ or Pascal
Hi,
It's quite embarrassing to me, but I've to ask this question. I'm sorry
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
It's quite embarrassing to me, but I've to ask this question. I'm sorry
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
- NickJohnson
- Member
- Posts: 1249
- Joined: Tue Mar 24, 2009 8:11 pm
- Location: Sunnyvale, California
Re: Going on with C++ or Pascal
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.
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
Thanks for your answer.
At the moment I use following script to compile my asm file:
EDIT: Ok Probably you mustn't allocate memory in win32 like this... But well... Then -f win32 seems to be the wrong parameter.
At the moment I use following script to compile my asm file:
This succeeds, but then this fails:D:\NASM\nasm.exe boot.asm -f bin -o boot.o
Message:ld.exe pascal.o boot.o -o kernel.bin
if I try:boot.o: file not recognized: File format not recognized
I get these errors:D:\NASM\nasm.exe boot.asm -f win32 -o boot.o
Well.. I don't know what it means. Line 37 and 40 look like this: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
Code: Select all
37 mov edi, [xpos]
40 add byte [xpos], 2
[...]
xpos db 0
Re: Going on with C++ or Pascal
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
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
[ Grub 2 | Visual Studio 2013 | PE File ]
The OsDev E.T.
Don't send OsDev MIB !
The OsDev E.T.
Don't send OsDev MIB !
- Love4Boobies
- Member
- Posts: 2111
- Joined: Fri Mar 07, 2008 5:36 pm
- Location: Bucharest, Romania
Re: Going on with C++ or Pascal
NASM isn't part of the GNU toolchain. GAS, the GNU Assembler however is.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
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;
Code: Select all
asm(" mov $0x02,%ax; \
inc %ax "); // although Intel syntax is also supported some opcodes are missing AFAIK
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
[ Project UDI ]
Re: Going on with C++ or Pascal
Sometimes I've got the feeling, that answers don't match to my question
Or maybe I don't have enough overview.
@Love4Boobies: I'm not sure what inline assembler's got to do with this question
Or maybe I don't have enough overview.
Ok, my question is how to create a binary format .o with pascal/C++ and link it with my assembly binary .o?First of all, binary connot be linked with anorther executable format, like all executable format.
@Love4Boobies: I'm not sure what inline assembler's got to do with this question
Re: Going on with C++ or Pascal
And the answer was "all is in the wiki".Cjreek wrote:Ok, my question is how to create a binary format .o with pascal/C++ and link it with my assembly binary .o?
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 !
The OsDev E.T.
Don't send OsDev MIB !
Re: Going on with C++ or Pascal
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.Ok, my question is how to create a binary format .o with pascal/C++ and link it with my assembly binary .o?
Re: Going on with C++ or Pascal
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
Currently - Busy with FAT12 driver and VFS