Saturday, December 03 2005 @ 07:49 PM CET Contributed by: stingduk Views: 27517
Level : newbie
OS : linux Language : C and inline ASM (GCC)
INTRODUCTION
those who started out coding assemly programs in windows and masm and then started using msvc or bcc would find it almost a head ache to code
an inline assembly in gcc or even use the native as (gnu assembler)
because gcc natively uses AT&T syntax while windowers are accustomed to
Intel Syntax
i now show you how to inline in gcc with intel_syntax directly embedded
this is a small Hello World Program that is coded in Assembly Using int 0x80
the code in c is as follows
#include <stdio>
#include <string.h>
char inlined[] = "this is a message from intel syntaxed inlined assembly code\n";
int len;
int main (void)
{
printf("this is a sample intel syntaxed and inlined c program\n");
len = strlen(inlined);
printf("%d\n",len);
asm(".intel_syntax noprefix\n");
asm("mov edx,len\n");
asm("mov ecx,offset inlined\n");
asm("mov ebx,1\n");
asm("mov eax,4\n");
asm("int 0x80\n");
printf("probably it is a success look in gdb\n");
return 1;
notice the first line :
asm(".intel_syntax noprefix\n");
This denotes that the folowing assembly snippet uses intel syntax to the
native assembler gas
now compile this with the following command line gcc -o intelinlined -masm=intel intelinlined.c
notice the switch -masm=intel that is all you need to do to inline with intel syntax in gcc
hope this was usefull enough
now lets try running the above code and see if it works
someone@server:~/myfirst/myfirstasm> ./intelinlined
this is a sample intel syntaxed and inlined c program
60
this is a message from intel syntaxed inlined assembly code
probably it is a success look in gdb
someone@server:~/myfirst/myfirstasm>
ok it works beautifully lets confirm the disassembly
here i use hte a pretty good hexeditor that even does xrefs etc
thanks to detten for his server usage
also thanks to Zach Dwiel who posted a snippet for this in game dev
http://www.gamedev.net/reference/articles/article1987.asp
hope you find it use full
Authored by:
stingduk on
Sunday, December 04 2005 @ 04:28 PM CET
the n is not just n i had used an escape sequence for
new line which was backslash n
a forced line feed is neceesery at the end of each line to avoid misinterpretation
but this board software has stripped that back slash
i dont know if detten could fix that problem
ill try mailing detten the source and precompiled binary so that it would be easier to follow the
article
thanks for the comment
now lets see if i can use extended instructions :)