Wednesday 23 October 2013

IA64 Assembly code of Write Sys call

Assembly code  of  a  Simple C  program...?

                     Here  i  am  going  to  write  a  simple  Intel ArCh 64 Assembly code  for  the  write  system  call.  Generally  when  we  compiling  a  code, Assembler makes  assembly  code  in to the executable  code. For  writing  a   assembly  code  we  have  good  knowledge  of  opcode of  IA64 .  This  is  just a  sample  program  taken from  the Book  of  "Writing  Security  Tools  and Exploits". Here first  i  find the  assembly  of  the  c  code by  debugging the  executable code or  using  the  objdump  command. The  make  a  simple    assembly  code file  of that. After  that  i use  assembler  to  compile that  in  to  executable  code.  Finally  i  linked  this  code  to an  executable  one.  If  you  dig deeper  in this ,  you can  make  a  better  code of  your own.

C  Program 

 int main()
 {
         while(1)
           write(1,"Hello, world !\n",15);
  exit(0);
 }

64  bit -Intel  Assembly  code

#hello.asm
BITS 64        ;64bit Intel Arch
section  .data  ;Data  Segment
    msg  db  "Hello  World", 0x0a   ;  The  String  and  newline  char
section .text      ;Text  Segment

    global _start    ;Default  entry  point   for  ELF  linking

_start:
;SYSCALL:write(1,msg,14)
                xor ecx,ecx
                mov  eax, 4     ;       Put  4  into  eax  ,  since  write  is syscall  #4
                mov  ebx,1    ;         Put  1  into  ebx,  since   stdout  is  1
                mov  ecx, msg    ;    Put  the  address  fo  the  string  into ecx
                mov  edx,14    ;        Put  14  into  edx,  since  our  string  is  14  bytes
                int  0x80       ;          Call  the  kernel  to  make  the  system  call  happen

loop _start        ; Start  a loop
;SYSCALL:exit(0)
               mov  eax,1  ;      Put  into  eax  since  exit  is  syscall#!
               mov  ebx,0  ;      Exit  with  success
               int 0x80      ;      Do  the  syscall




Assembler  make  this code in  to  object  code

          nasm  -f  elf64  hello.asm 

Link  this  file  using ld  command  in  linux 

            ld  -s  -o hello  hello.o

Execute  the  code
             ./hello


Output

Hello, world !
Hello, world !
Hello, world !
Hello, world !
Hello, world !
Hello, world !
Hello, world !
Hello, world !
Hello, world !
Hello, world !
Hello, world !
Hello, world !
Hello, world !
Hello, world !



No comments:

Post a Comment