I'm trying to write an x86 version of the 'cat' program as a training of syscall calls in assembly.
I'm struggling a lot with command line arguments. I use the main symbol as an entry point, so I thought I would find the argc parameter in %rdi and the argv parameter in %rsi. Actually argc is in %rdi as expected, but I keep segfaulting when trying to pass argv[1] to the open syscall.
Not sure of what I'm doing wrong, here is my assembly code:
main:
cmp $2, %rdi // If argc != 2 return 1
jne .err1
lea 8(%rsi), %rdi // Move argv[1] -> %rdi
xor %rsi, %rsi // 0 to %rsi -> O_RDONLY
xor %rdx, %rdx
mov $2, %rax // Open = syscall 2
syscall
cmp 0, %rax // If open returns <0 -> exit status 2
jl .err2
mov %rax, %rdi // Move fd to %rdi
call cat
ret
.err1:
mov $1, %rax
ret
.err2:
mov $2, %rax
ret