Author Topic: C+ASM  (Read 3142 times)

Jaff

  • Guest
C+ASM
« on: January 22, 2006, 01:45:47 PM »
I need get the asm function to "c" code...
I've got in asm code:

section.text
mov ah, 0x09
mov dx, pause
int 0x21

mov ah, 0x01
int 0x21

section.data
pause db "For continue press any key...",'$'

and i need make from this, function to C e.g.:

int main(void)
{
  wait();
  return 0;
}

where "wait" is the "function" from asm

What muss i append to asm and C code???

I use the nasm and MinGW compiler...
How can i link it???

nobody

  • Guest
Re: C+ASM
« Reply #1 on: January 22, 2006, 09:48:41 PM »
Is this a trick question? How do I make an apple pie from these oranges? You've got dos code there, MinGW is (AFAIK) 32-bit only. No way they're going to mix. If all you want is:

puts("Hit a key, Idiot!");
getc()


It seems too obvious to ask...

Maybe you'd tell us WHY you'd want to take perfectly good asm code and soil it with C??? Seems like a big step backwards, to me. If what you want is to replace those dos interrupts with Windows APIs, that could be arranged...

Best,
Frank

Jaff

  • Guest
Re: C+ASM
« Reply #2 on: January 22, 2006, 10:16:07 PM »
OK OK
How can i link asm and c?
Where i find some example with used C-compiler and NASM compiler???

nobody

  • Guest
Re: C+ASM
« Reply #3 on: January 23, 2006, 12:04:30 AM »
http://www.drpaulcarter.com/pcasm

There are actually several ways we can link C and asm. I don't know C, so these are approximate. The most common would be something like:

#include
main{
...
myasmfunc(parameters?);
...
return 0
}
---
global myasmfunc
myasmfunc:
nop
ret
---

Or you could do something like:

global main
extern printf

section .text
main:
push my_param
push my_format_string
call printf
add esp, 8
ret

Or... maybe...

global _start
extern printf

section .text
_start:
...
call printf

sys_exit 0

... I don't know how that last one would go in Windows - seems "dangerous" to be calling libc without running the C startup code, but it "seems to work" in Linux.

Now... C prefixes external names with an underscore - except in ELF format. ("portable" much?). You can spell printf _printf, and main _main throughout your code, but there's no easy way to remove the underscore for ELF. If you spell it printf, and use:

nasm -f win32 --PREFIX_ myfile.asm

or

nasm -f elf myfile.asm

you can get somewhat more "portable" code. (same goes for "my_asm_func", too - it'll need an underscore, except for ELF)

You can find a little info in the "mixed language programming" section of the manual. There are some examples in the "test" directory (if you've got the Nasm source). But check out Dr. Carter's tut!

Best,
Frank