Inline Assembly On GCC using Intel Syntax

Saturday, December 03 2005 @ 07:49 PM CET

Contributed by: stingduk

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

  ....... ! ;********************************************************                             
  ....... ! main:                           ;xref o80482c7                                        
  ....... !   push        ebp                                                                     
  804836d !   mov         ebp, esp                                                                
  804836f !   sub         esp, 8                                                                  
  8048372 !   and         esp, 0fffffff0h                                                         
  8048375 !   mov         eax, 0                                                                  
  804837a !   sub         esp, eax                                                                
  804837c !   sub         esp, 0ch                                                                
  804837f !   push        strz_this_is_a_sample_intel_syntax_8048520                              
  8048384 !   call        printf@@GLIBC_2.0                                                       
  8048389 !   add         esp, 10h                                                                
  804838c !   sub         esp, 0ch                                                                
  804838f !   push        inlined                                                                 
  8048394 !   call        strlen@@GLIBC_2.0                                                       
  8048399 !   add         esp, 10h                                                                
  804839c !   mov         [len], eax                                                              
  80483a1 !   sub         esp, 8                                                                  
  80483a4 !   push        dword ptr [len]                                                         
  80483aa !   push        data_8048557                                                            
  80483af !   call        printf@@GLIBC_2.0                                                       
  80483b4 !   add         esp, 10h                                                                
  80483b7 !   mov         edx, [len]                                                              
  80483bd !   mov         ecx, inlined                                                            
  80483c2 !   mov         ebx, 1                                                                  
  80483c7 !   mov         eax, 4                                                                  
  80483cc !   int         80h                                                                     
  80483ce !   sub         esp, 0ch                                                                
  80483d1 !   push        strz_probably_it_is_a_success_look_8048560                              
  80483d6 !   call        printf@@GLIBC_2.0                                                       
  80483db !   add         esp, 10h                                                                
  80483de !   mov         eax, 1                                                                  
  80483e3 !   leave                                                                               
  80483e4 !   ret                            

looks nice

Find the sourcecode here

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

stingduk

3 comments



http://www.reversing.be/article.php?story=20051203194931893