RosettaCodeData/Task/Command-line-arguments/X86-64-Assembly/command-line-arguments.x86-64
2024-10-16 18:07:41 -07:00

58 lines
1.2 KiB
Text

format ELF64 executable 3
entry start
segment executable readable
start:
pop rax ; pop argc off stack. It should be at least 1
.loop0:
pop rdi ; pop the pointer to argument string off stack
call prints ; call a function to print a string
mov rdi, 0x0a ; parameter to next function. 0x0a = line feed = 10
call printc ; call a function to print a single character
dec rax ; decrement RAX.
jnz .loop0 ; get next argument if RAX is not yet 0
.exit:
mov rax, 60
mov rdi, 0
syscall
prints: ; function that prints a zero terminated string
push rax
push rdi
push rsi
push rdx
xor rdx, rdx
cmp byte [rdi + rdx], 0
je .return
.stloop:
cmp byte [rdi + rdx], 0
je .printit
inc rdx
jmp .stloop
.printit:
mov rax, 1
mov rsi, rdi
mov rdi, 1
syscall
.return:
pop rdx
pop rsi
pop rdi
pop rax
ret
printc: ;function that prints a single character in rdi (dil).
push rax
push rsi
push rdx
push rdi
mov rax, 1
mov rdi, 1
mov rsi, rsp
mov rdx, 1
syscall
pop rdi
pop rdx
pop rsi
pop rax
ret