Skip to content

Instantly share code, notes, and snippets.

@Scherso
Last active April 21, 2023 15:33
Show Gist options
  • Save Scherso/c78d1d8f42c9118ba05507c3b7c15499 to your computer and use it in GitHub Desktop.
Save Scherso/c78d1d8f42c9118ba05507c3b7c15499 to your computer and use it in GitHub Desktop.
bits 64
section .text
global _start
_start:
push NUM_FORKS
.loop:
mov rax, SYS_FORK ; Moving '57' into 'rax', syscall for 'stub_fork()'.
syscall ; Calling the value in 'rax'.
mov rax, SYS_WRITE ; Moving '1' into 'rax', syscall for 'write()'.
mov rdi, STDOUT ; Moving '1' into 'rdi', the second argument in a syscall.
mov rsi, message ; Moving our message into 'rsi', the third argument in a syscall.
mov rdx, messagel ; Moving the message length into the fourth argument of the 'write()' call.
syscall ; Calling the value in 'rax'.
lea rax, [time] ; Loading the effective address of 'time' in the '.data' section.
mov DWORD [rax], 2 ; Moving '2', our seconds argument to the upper portion of 'rax'.
mov BYTE [eax + 4], 0 ; Moving '0', our nano-seconds argument to the lower portion of the 'ax' regsiter.
mov rax, SYS_SLEEP ; Moving our sleep syscall to 'rax' to be called with a syscall instruction.
mov rdi, time ; Moving our time structure to 'rdi' to be passed as the second argument in our syscall.
xor esi, esi ; Setting the lower portion of the 'si' register to '0' to pass '0' as the third argument.
syscall ; Calling the value in 'rax'.
pop rcx
dec rcx ; Decrementing our loop counter.
push rcx
jnz .loop ; If the counter isn't zero, jump to '.loop'.
call _exit ; Exiting the program once our counter holds a value of '0'.
_exit:
mov rax, SYS_EXIT ; Moving '60' into 'rax', syscall for 'exit()'
xor rdi, rdi ; Setting the return value to '0'.
syscall ; Calling the value in 'rax'.
section .data
SYS_EXIT equ 0x3C ; 0x3C = 60
SYS_FORK equ 0x39 ; 0x39 = 57
SYS_WRITE equ 0x1 ; 0x1 = 1
SYS_SLEEP equ 0x23 ; 0x23 = 35
STDOUT equ 0x1 ; 0x1 = 1
NUM_FORKS equ 10
time dq 0, 0
message db "23 instructions executed.", 0xA, 0xD
messagel equ $ - message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment