RosettaCodeData/Task/Fork/X86-Assembly/fork.x86

72 lines
1.2 KiB
Text
Raw Permalink Normal View History

2017-09-23 10:01:46 +02:00
; x86_64 linux nasm
%include "/home/james/Desktop/ASM_LIB/Print.asm"
%include "/home/james/Desktop/ASM_LIB/Sleep.asm"
2013-04-10 22:43:41 -07:00
section .data
2017-09-23 10:01:46 +02:00
parent: db "Parent: "
child: db "Child: "
newLine: db 10
section .text
global _start
_start:
mov rax, 57 ; fork syscall
syscall
cmp rax, 0 ; if the return value is 0, we're in the child process
je printChild
printParent: ; else it's the child's PID, we're in the parent
mov rax, 1
mov rdi, 1
mov rsi, parent
mov rdx, 8
syscall
mov rax, 39 ; sys_getpid
syscall
mov rdi, rax
call Print_Unsigned
mov rax, 1
mov rdi, 1
mov rsi, newLine
mov rdx, 1
syscall
mov rdi, 1 ; sleep so the child process can print befor the parent exits
call Sleep ; you might not see the child output if you don't do this
jmp exit
printChild:
mov rdi, 1
call Sleep ; sleep and wait for parent to print to screen first
mov rax, 1
mov rdi, 1
mov rsi, child
mov rdx, 7
syscall
mov rax, 39 ; sys_getpid
syscall
mov rdi, rax
call Print_Unsigned
mov rax, 1
mov rdi, 1
mov rsi, newLine
mov rdx, 1
syscall
exit:
mov rax, 60
mov rdi, 0
syscall