Skip to content

Instantly share code, notes, and snippets.

@phoemur
Created July 9, 2023 16:57
Show Gist options
  • Save phoemur/053361e4a54f148a9af8e93b5c402f7c to your computer and use it in GitHub Desktop.
Save phoemur/053361e4a54f148a9af8e93b5c402f7c to your computer and use it in GitHub Desktop.
asm to print command line arguments
section .data
EXIT_SUCCESS equ 0 ; successful operation
SYS_exit equ 60 ; call code for terminate
linefeed db 10
stringRes db "Number of arguments: "
section .text
global _start
%macro LF_macro 0
mov rax, 1
mov rdi, 1
mov rsi, linefeed
mov rdx, 1
syscall
%endmacro
; print to stout a string located at rdi as pointer
_printstring:
mov rsi, rdi
call _strlen
mov rdx, rax
mov rax, 1
mov rdi, 1
syscall
ret
_start:
mov r8, [rsp]
add r8, 48
mov [stringRes + 20], r8
mov rdi, stringRes
call _printstring
LF_macro
pop rcx ; argc
_printloop:
pop rdi ; argv[0...argc]
push rcx
call _printstring
LF_macro
pop rcx
loop _printloop
; Exit
mov rax, SYS_exit
mov rdi, EXIT_SUCCESS
syscall
; input rdi as pointer to \0 terminated string
; return rax as length of string
_strlen:
xor rax, rax
_strlenLoop:
cmp byte [rdi], 0
je _strlenRet
inc rax
inc rdi
jmp _strlenLoop
_strlenRet:
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment