Anonymous user
Fork: Difference between revisions
no edit summary
(Add BaCon) |
No edit summary |
||
Line 1,277:
=={{header|X86 Assembly}}==
I've written a subroutine that prints out any positive value. It lives on my desktop and you can't find it on rosetta code.
I've also written a sleep subroutine and you can find that in the Sleep task on this site.
<lang x86asm>
; x86_64 linux nasm
%include "/home/james/Desktop/ASM_LIB/Print.asm"
%include "/home/james/Desktop/ASM_LIB/Sleep.asm"
section .data
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
</lang>
|