Contribute  :  Web Resources  :  Past Polls  :  Site Statistics  :  Downloads  :  Forum  
    BiW ReversingThe challenge is yours    
 Welcome to BiW Reversing
 Thursday, April 17 2014 @ 02:36 PM CEST

Inline Assembly On GCC using Intel Syntax


CodingLevel : newbie

OS : linux
Language : C and inline ASM (GCC)


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);
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
this is a message from intel syntaxed inlined assembly code
probably it is a success look in gdb
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
hope you find it use full


What's Related

Story Options

Inline Assembly On GCC using Intel Syntax | 3 comments | Create New Account
The following comments are owned by whomever posted them. This site is not responsible for what they say.
Inline Assembly On GCC using Intel Syntax
Authored by: moniker on Sunday, December 04 2005 @ 12:09 PM CET
great help tnx, never got around reading that manpage..

now lets see if i can use extended instructions :)
Inline Assembly On GCC using Intel Syntax
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
thanks for the comment
 Copyright © 2014 BiW Reversing
 All trademarks and copyrights on this page are owned by their respective owners.
Powered By Geeklog 
Created this page in 0.08 seconds