61
Other Discussion / Re: NASM or YASM in the modern era (or something else)
« Last post by debs3759 on February 03, 2024, 10:43:57 PM »As far as I know, this is it. There are mailing lists, but they are dead.
; fork,asn
;
; nasm -f elf64 -o fork.o fork.asm
; ld -s fork.o -o fork
; ./fork
bits 64
default rel ; All offset only effective addresses are rip-relative from now on...
; Since no data will be writen, they can be at .rodata section.
section .rodata
; We don't need the nul char!
childMsg db `This is the child process\n` ; a message string
clength equ $-childMsg
parentMsg db `This is the parent process\n` ; a message string
plength equ $-parentMsg
tspec:
dq 3 ; 3 seconds delay.
dq 0
section .text
global _start
_start:
; Notice: Using RAX here will encode a 10 bytes instruction (rex prefix, 8 bytes for the immediate and the opcode).
; In x86-64 mode, changing E?? will AUTOMATICALLY zero the upper 32 bits of R?? registers.
mov eax,57 ; sys_fork
syscall
; fork() will return a file descriptor (int type).
test eax,eax
js .fail ; sys_fork can fail (returs a descriptor < 0).
jz .child
; Parent process.
mov eax,1 ; sys_write
mov edi,eax ; stdout
lea rsi,[parentMsg] ; Must be rip-relative addressing.
mov edx,plength
syscall
call workalot
.exit:
mov eax,60
xor edi,edi
syscall
.fail:
mov eax,60
mov edi,1
syscall
align 4
.child:
mov eax,1 ; sys_write
mov edi,eax ; stdout
lea rsi,[childMsg] ; message address (rip-relative)
mov edx,clength ; message string length
syscall
call workalot
jmp .exit
; -------- unten nur proceduren ---------------------
align 4
workalot:
mov eax,35 ; sys_nanosleep
lea rdi,[tspec]
xor esi,esi
syscall
ret
; Fork
; nasm -f elf64
; ld fork.o -o fork
; Run with: ./fork
[bits 64]
SECTION .data
childMsg db 'This is the child process', 0h ; a message string
clength equ $-childMsg
parentMsg db 'This is the parent process', 0h ; a message string
plength equ $-parentMsg
crlf db 0xA,0xD ; newline, length is 2
SECTION .text
global _start
_start:
mov rax, 57 ; SYS_FORK
syscall
cmp rax, 0 ; if eax is zero we are in the child process
jz child ; jump if eax is zero to child label
parent:
mov rax, 1 ; sys_write
mov rdi, 1 ; stdout
mov rsi, parentMsg ; message address
mov rdx, plength ; message string length
syscall
mov rax, 1 ; sys_write
mov rdi, 1 ; stdout
mov rsi, crlf ; message address
mov rdx, 2 ; message string length
syscall
call workalot
jmp exit
child:
mov rax, 1 ; sys_write
mov rdi, 1 ; stdout
mov rsi, childMsg ; message address
mov rdx, clength ; message string length
syscall
mov rax, 1 ; sys_write
mov rdi, 1 ; stdout
mov rsi, crlf ; message address
mov rdx, 2 ; message string length
syscall
call workalot
jmp exit
exit:
mov rax, 60 ; sys_exit
mov rdi, 0 ; return 0 (success)
syscall
; -------- unten nur proceduren ---------------------
workalot:
mov rcx, 65000
loop1:
push rcx
mov rcx, 65000
loop2:
push rcx
nop
pop rcx
loop loop2
pop rcx
loop loop1
ret