How to resolve the algorithm Fork step by step in the X86-64 Assembly programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Fork step by step in the X86-64 Assembly programming language

Table of Contents

Problem Statement

Spawn a new process which can run simultaneously with, and independently of, the original parent process.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Fork step by step in the X86-64 Assembly programming language

Source code in the x86-64 programming language

option casemap:none

windows64     equ 1
linux64       equ 3

ifndef __THREAD_CLASS__
__THREAD_CLASS__      equ 1

  if @Platform eq windows64
    option dllimport:
      CreateThread    proto :qword, :qword, :qword, :qword, :dword, :qword
      HeapAlloc       proto :qword, :dword, :qword
      HeapFree        proto :qword, :dword, :qword
      ExitProcess     proto :dword
      GetProcessHeap  proto 
    option dllimport:
      exit            equ ExitProcess
  elseif @Platform eq linux64
    pthread_create    proto :qword, :qword, :qword, :qword
    malloc            proto :qword
    free              proto :qword
    exit              proto :dword
  endif

  printf              proto :qword, :vararg

  CLASS thread
    CMETHOD createthread
  ENDMETHODS
    tid     dq ?
    hThread dq ?
  ENDCLASS

  METHOD thread, Init, , <>
    mov rax, thisPtr
    ret
  ENDMETHOD

  METHOD thread, createthread, , <>, lpCode:qword, arg:qword
    local z:qword,x:qword

    mov rbx, thisPtr
    assume rbx:ptr thread
    mov z, lpCode
    mov x, 0
    .if arg != 0
      mov x, arg
    .endif
    if @Platform eq windows64
      invoke CreateThread, 0, 0, z, x, 0, addr [rbx].tid
      .if rax == 0
        mov rax, -1
        ret
      .endif
    elseif @Platform eq linux64
      invoke pthread_create, addr [rbx].tid, 0, z, x
      .if rax != 0
        mov rax, -1
        ret
      .endif
    endif
    mov [rbx].hThread, rax
    assume rbx:nothing
    ret
  ENDMETHOD

  METHOD thread, Destroy, , <>
    ;; We should close all thread handles here..
    ;; But I don't care. In this example, exit does it for me. :]
    ret
  ENDMETHOD

endif               ;;__THREAD_CLASS__

thChild             proto

.data

.code
main proc
  local pThread:ptr thread

  mov pThread, _NEW(thread)
  invoke printf, CSTR("--> Main thread spwaning child thread...",10)
  lea rax, thChild
  pThread->createthread(rax, 0)
  _DELETE(pThread)
  ;; Just a loop so Exit doesn't foobar the program.
  ;; No reason to include and call Sleep just for this.. -.-
  mov rcx, 20000
  @@:
    add rax, 1
  loop @B
  invoke exit, 0
  ret
main endp

thChild proc
  invoke printf, CSTR("--> Goodbye, World! from a child.... thread.",10)
  mov rax, 0
  ret
thChild endp
end


; x86_64 linux nasm

%include "/home/james/Desktop/ASM_LIB/Print.asm"
%include "/home/james/Desktop/ASM_LIB/Sleep.asm"

section .data

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

  

You may also check:How to resolve the algorithm Copy a string step by step in the Pop11 programming language
You may also check:How to resolve the algorithm User input/Text step by step in the PHP programming language
You may also check:How to resolve the algorithm Set of real numbers step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Sorting algorithms/Gnome sort step by step in the Clojure programming language
You may also check:How to resolve the algorithm Extreme floating point values step by step in the Forth programming language