|
CS456 - Systems Programming
Spring 2026
|
Displaying ./code/nasm/forkexec/fork.asm
; Fork (new version)
; Compile with: nasm -f elf fork.asm
; Link with (64 bit systems require elf_i386 option): ld -m elf_i386 fork.o -o fork
; or since we have a makefile, we can just run (make fork)
; Run with: ./fork
%include '../functions.asm' ; maake sure to get the path correct here
; make sure to rename old version fork.asm.old, and then this one fork.asm before compiling
SECTION .data
childMsg db 'This is the child process', 0h ; a message string
parentMsg db 'This is the parent process', 0h ; a message string
SECTION .bss
pid resb 8 ; reserve 8 bytes to store pid
status resb 8 ;
SECTION .text
global _start
_start:
mov eax, 2 ; invoke SYS_FORK (kernel opcode 2)
int 80h
mov [pid], eax ; moving eax to pid
cmp eax, 0 ; if eax is zero we are in the child process
jz child ; jump if eax is zero to child label
parent:
; since we already moved the return value of fork to pid, we can safely call the
; function that prints the parent message
mov eax, parentMsg ; inside our parent process move parentMsg into eax
call sprintLF ; call our string printing with linefeed function
; invoking the waitpid system call
mov eax, 7 ; waitpid is opcode 7
mov ebx, [pid] ; move our pid value to ebx
mov ecx, status ; move the status value we declared to ecx
mov edx, 0 ; move options (currently set to 0) to edx
int 80h
call quit ; quit the parent process
child:
mov eax, childMsg ; inside our child process move childMsg into eax
call sprintLF ; call our string printing with linefeed function
call quit ; quit the child process
|