NASM - The Netwide Assembler
NASM Forum => Using NASM => Topic started by: miskin on May 15, 2015, 11:22:50 AM
-
#include<stdio.h>
int main()
{
int a=10,b=20,c;
_asm_test:
enter 0,0
pusha
mov eax,a
mov ebx,b
add eax,ebx
mov c,eax
popa
leave
ret
printf(" %d + %d = %d\n",a,b,c);
return 0;
}
-
NASM is not an inline assembler. Wrong forum.
-
Hi Miskin,
Thanks for joining us.
Hi Alex,
Yeah, "wrong forum" is the simple answer, but we might be able to help a little in spite of that. I don't think any assembler is really an inline assembler - certain compilers will allow inline assembler in a certain syntax. I have recently heard that some version of MinGW GCC will accept Nasm syntax, but I can't confirm that. In any case, this isn't it. Looks more like Masm syntax to me. ("mov c, eax" is a dead giveaway!)
But ignoring syntax, does it make any sense? The compiler is going to generate a prolog to "main" with three local variables. Then, I think you're going to need to open an "_asm" block, probably surrounded with "{}"s. I don't think "_asm_test:" is going to do it(?). Then you do "enter 0, 0" - a prolog (of sorts) with no local variables. I doubt if you want that. Then you add a couple of variables - main's locals? Then you "leave" and "ret"... but nothing but "main" has been "call"ed at this point - probable crash. At best, you'd "ret" from "main" and never execute the "printf" line. I think you'd want to lose the "enter", "leave", and "ret" and do it all "in line" with "main".
Or... we could make it a "Nasm question" and assemble a separate module with Nasm and link it with a C "main" - not "in line" at all. There may be some compiler that will allow you to use Nasm syntax for inline assembly, but I don't think that's what you've got there... Good luck with it, in any case.
Best,
Frank
-
On a second thought, I must admit I was wrong.
The following will use NASM as an "inline" assembler in my Smaller C compiler:
// file: nasminline.c
// compile for Windows: smlrcc -win nasminline.c -o nasminline.exe
// compile for Linux: smlrcc -linux nasminline.c -o nasminline
#include <stdio.h>
int main(void)
{
int a = 10, b = 20, c;
asm("enter 0,0\n"
"pusha\n"
"mov eax, [ebp+4*3]\n"
"mov ebx, [ebp+4*2]\n"
"add eax, ebx\n"
"mov [ebp+4*1], eax\n"
"popa\n"
"leave");
printf("%d + %d = %d\n", a, b, c);
return 0;
}
and the resultant executable will work (works on Windows).
:)