Created
July 9, 2023 16:57
-
-
Save phoemur/053361e4a54f148a9af8e93b5c402f7c to your computer and use it in GitHub Desktop.
asm to print command line arguments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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