Skip to content

Instantly share code, notes, and snippets.

@OzTamir
Created March 26, 2014 11:37
Show Gist options
  • Save OzTamir/9781353 to your computer and use it in GitHub Desktop.
Save OzTamir/9781353 to your computer and use it in GitHub Desktop.
Fork Process in x86 NASM. Each process will print a different message and then exit.
section .text
global _start
_start:
mov eax, 2 ; SYS_FORK Op Code
int 0x80
cmp eax, 0 ;If the return value is 0, we are in the child process
jz child
parent:
mov edx, len ;Move msg length to edx
mov ecx, msg ;Move msg to ecx
call print ;Print
call exit ;Exit
child:
mov edx,cLen ;Same...
mov ecx,cMsg
call print
call exit
print:
mov ebx,1 ;first argument: file handle (stdout)
mov eax,4 ;system call number (SYS_WRITE)
int 0x80
ret
exit:
mov ebx,0 ; Exit code
mov eax,1 ; SYS_EXIT
int 0x80
section .data
msg db "I AM YOUR FATHER",0xa ;String to be printed by father process
len equ $ - msg ; And it's length
cMsg db "Im the good child",0xa ;String to be printed by child process
cLen equ $ - cMsg ;And it's length...
@faissaloo
Copy link

SYS_FORK isn't an opcode, it's a system call. Other than that, really useful, thanks.

@juangod1
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment