Author Topic: Calling external programs from assembly code  (Read 4841 times)


  • Guest
Calling external programs from assembly code
« on: December 12, 2004, 12:46:07 PM »

i want to show a simple menu written in C after booting my floppy driven operating system.

How can i call C programs from assembly code?

Thanks in advance.


Peter Wiehe

  • Guest
Re: Calling external programs from assembly code
« Reply #1 on: January 22, 2005, 11:36:48 PM »
I guess Your OS is real mode and the C source is written with A DOS c compiler?

You call C programs from Asm by calling a C function (for example main(). Use a near CALL or JMP (however, this depends on the memory model used
by Your DOS compiler).
You should first learn something about stackframe/framepointer and linking/object files.

The stackframe is the way how arguments are transferred from the calling code to the called function.
If you handle no arguments, You can ignore the stackframe issue. The return value, if any (and if You ever want to return), is usually handed back in ax.

a) You compile Your C code to a flat binary
or b) somehow know the entry offset in the object file which is produced from the C source,
then You can ignore the linking issue.

Peter Wiehe

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2451
  • Country: us
Re: Calling external programs from assembly code
« Reply #2 on: January 24, 2005, 04:03:57 AM »
Right. For "regular" linking of C and asm, there's some info in the Friendly Manual under "mixed language programming". covers the subject - for 32-bit code, but the principles are the same.

Getting "flat binary" out of C probably depends on the linker. Another option might be to provide your own loader for MZ format - in your bootsector, or more likely in a "second stage loader". Not too difficult...

Another issue that might byte ya - if your C compiler is capable of C++ (even if you're writing plain C), it'll "decorate" (mangle) your function names. This can be avoided by something like 'extern "C" myfunc' - exact syntax may vary. C prepends an underscore to anything external, too, unless you're using ELF. ("nasm --prefix_ ..." may help with this, or "%define myfunc _myfunc"...

What you're proposing isn't simple, Alex, so don't be too surprised if you have to struggle with it.

Good Luck,