Problem mixing C/ASM code

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
pepito

Problem mixing C/ASM code

Post by pepito »

Hello:

I am having problem trying to mix C and ASM code when I want to use global variables.

For example,

C code:

#include "myclib.h"
int NUM;
main() {
NUM=10;
printd(NUM); }

The printd() function is an NASM code (part of my OS).

I belive I have an error when I compile the program with this script:

nasm -f aout cheader.asm -o a.o
gcc -c -ffreestanding shell.c -o b.o
nasm -f aout clib.asm -o c.o
nasm -f aout cstack.asm -o d.o
ld -e start -Ttext 0x0000 --oformat binary -o shell.bin a.o b.o c.o d.o

cheader.asm just have a jump to _main,
shell.c is the program I want to compile,
clib.asm have all the syscalls to my kernel OS,
and cstack.asm just have space for the stack.

Any idea?

Thank you very much,

pepito
common

RE:Problem mixing C/ASM code

Post by common »

Not trying to be smart, but "I am having a problem mixing C and assembly" needs a bit more information about what is going on.  What problem, specifically is occuring and what other details (codewise) you can offer, would be helpful.
pepito

RE:Problem mixing C/ASM code

Post by pepito »

Sorry! The english is not my langage, but a will try to be more explicit...

I am using DJGPP and NASM to build a program that runs over my OS kernel. I need to get 'plain binary code' because my OS just can run this kind of programs.

But the program generate an error if I declare 'not initialized global variables' into the C code...

---

#include "myLibC.inc"

int NUM;   <-- global variable

main() {

NUM=1;     <-- here I get the error

} // main

---

...and then test my OS using BOCHS.

I get this kind of error:

    write_virtual_checks(): write beyond limit, r/w
    >>PANIC<< fetch_raw_descriptor: LDT: index (5af)b5 > limit (1f)

Or some other about bad address.

I suspect that the linker is not creating 'plain binary code' when I add 'not initialized global variables'. Then, when my program try to access this variables, it go to a bad address.

I use this script to compile my program:

nasm -f aout c_header.asm -o a.o        <-- header needed by my OS
gcc -c -ffreestanding shell.c -o b.o     <-- the C code of the program
nasm -f aout c_lib.asm -o c.o           <-- many ASM rutines
nasm -f aout c_stack.asm -o d.o         <-- the stack

ld -e start -Ttext 0x0000 --oformat binary -o shell.bin a.o b.o c.o d.o

Is better explained?

Thank you for your soon response!

pepito
pepito

RE:Problem mixing C/ASM code

Post by pepito »

Other details about the error:

- When I complie the C part of the program, DJGPP create .comm directives to hold the 'not initialized global variables'.

- If I read the address of the 'global variable' with the '&' operator, I get a value greater than the size of the program generated with the script!

pepito
Post Reply