Thanks, I will check that. Do you have idea about interacting "C" and asm ? Here I have a topic: https://forum.nasm.us/index.php?topic=2698.0 since this is first time I will do it, I based on a totorial page. My program works but I cant see "C" print in the end.
There are 2 kinds of programs writen in C: "hosted" and "freestanding". Hosted programs are those which uses libraries such as libc and "c runtime initialization" codes linked to your final binary. Freestanding are those which don't use ANY libraries, except your own, and have no initialization code linker together your code to make it work.
When developing a boot sector isn't interesting to use hosted code (using functions as printf(), scanf() etc) because you don't have an operating system providing system calls. But you CAN write functions in C usefun to your assembly code. Just have to respect C's "calling convention" and check if your compiler can create real mode compatible code. This is the case of GCC (probably you can do this with Visual Studio's C++ compiler, but is much harder!).
Example: Consider you fave a simple C function which multiply an integer value by 3:
int f( int x ) { return 3 * x; }
If you compile this and take a look at the assembly code, you'll see:
; Compiled with 'gcc -m16 -masm=intel -S -O2 test.c`
f:
mov eax,[esp+4]
lea eax,[eax+eax*2]
ret
Notice the C's function f() uses the stack to get its arguments. This can be avoided using function attributes, like this one:
__attribute__((regparm(1))) int f( int x ) { return 3 * x; }
This will create a function like this:
; Compiled with 'gcc -m16 -masm=intel -S -O2 test.c`
f:
lea eax,[eax+eax*2]
ret
Where f() expects its argument to be passed in EAX.
You can link this kind of C function with your NASM program using the 'extern' keyword:
; my nasm code
bits 16
...
extern f
start:
...
mov eax,2
call f
; Here EAX will be 6.
...
The problem now is, when you compile your assembly file with -f bin, you cannot LINK anything (bin format lacks the necessary info abour externs and globals)... You'll need to create a code with sections, compile it to some object format (elf or pe, dependind on the operating system) and create a linker script to transform your objects in a binary code suitable for a boot sector. Essentially:
$ nasm boot.asm -o boot.o
$ gcc -O2 -m16 -c -o func.o func.c
$ ld -Tmyscript.ld func-o boot.o -o boot
All the magic of transforming code with 'sections' to binary must be inside this 'myscript.ld' linker script. Which is something like this:
OUTPUT(binary)
SECTIONS {
.text: *(.text)
.rodata: *(.rodata)
.data: *(.data)
. = 510;
BYTE(0x55), BYTE(0xAA);
}
Or something like this (search about ld linker scripts)...