NASM - The Netwide Assembler
NASM Forum => Example Code => Topic started by: miskin on May 15, 2015, 11:14:09 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;
}
//this dont work .thank for helping me!
-
Hello, your question should be in other discussion.
Else what compiler you use for C code ?
In gcc, we write:
asm (".intel_syntax noprefix");
asm ("...");
asm (".att_syntax noprefix");
and compiler flag:
-masm=intel
In MS/intel compiler:
__asm
{
...
}
and no compiler flag needed.
-
i use gcc on linux
my editor is geany.
i would like to write the code for a subtraktion function with inline assambler but i dont know how to do this.
-
#include<stdio.h>
int main()
{
int a=10,b=20,c;
asm ("
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;
}
this dont work why?Can sombody help me.
-
Take care about asm inline, you risk to modify some register that are already use before your asm.
You need to put quote for each instruction and add brackets around variables.
Here's the code for do subtraction function:
int a = 2, b = 1, c;
int main(void)
{
int tmp;
asm (".intel_syntax noprefix");
asm ("mov eax, [a]");
asm ("sub eax, [b]");
asm ("mov [c], eax");
asm (".att_syntax noprefix");
printf("%d - %d = %d\n", a, b, c);
return (0);
}
And like you see, you need global variable (global label) for use variable written in ascii (a, b, c), else you need to use stack param.
(off-topic)
Unlike Nasm who give the ability to use 3 way to create variable: global label + param stack + local label (like structure in C code), variables range is just the way how see/manage their addresses.
We can expand those way with give the possibility to manage data's address directly while writing code maybe.
-
Hi
i compiled it and it's ok but i dont get an exucutible.
Thanks
-
No problem
You don't arrived to get an executable file ?
-
the terminal output
./geany_run_script.sh: 5: ./geany_run_script.sh: ./testnasm: not found
------------------
(program exited with code: 127)
Press return to continue
Yes its running uuuuuuuuuuuuuuuuuuuuuuuhhhhuuuuuuuuuuuuuuuuuuu
1000 Thanks to SHAYNOX
-
Congratulation ^^