File: echo-linux-amd64.asm 1 ; The MIT License (MIT) 2 ; 3 ; Copyright (c) 2026 pacman64 4 ; 5 ; Permission is hereby granted, free of charge, to any person obtaining a copy 6 ; of this software and associated documentation files (the "Software"), to deal 7 ; in the Software without restriction, including without limitation the rights 8 ; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 ; copies of the Software, and to permit persons to whom the Software is 10 ; furnished to do so, subject to the following conditions: 11 ; 12 ; The above copyright notice and this permission notice shall be included in 13 ; all copies or substantial portions of the Software. 14 ; 15 ; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 ; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 ; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 ; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 ; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 ; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 ; SOFTWARE. 22 23 24 ; To compile this app for linux/amd64, use the commands 25 ; 26 ; nasm -f elf64 -o echo-linux-amd64.o echo-linux-amd64.asm 27 ; ld -n -s echo-linux-amd64.o -o echo 28 29 30 ; echo [words...] 31 ; 32 ; Emit a line with all the words given as command-line arguments. 33 34 35 stdout equ 1 36 sys_write equ 1 37 sys_exit equ 60 38 39 section .rodata 40 empty_line: db 10 41 42 section .text 43 44 global _start 45 46 _start: 47 48 ; rcx = argc 49 pop rcx 50 51 ; skip argv[0] 52 pop rbx 53 ; get argv[1] 54 pop rbx 55 56 ; find length of byte-slice using the arg-count, replacing each null byte 57 ; with a space along the way 58 59 ; for (rdx = 0, rcx = rcx - 1; rcx != 0; rcx--, rbx++, rdx++) 60 mov rdx, 0 61 dec rcx 62 args_loop: 63 ; quit loop when rcx == 0 64 cmp rcx, 0 65 je args_loop_done 66 67 ; check current byte 68 mov al, [rbx] 69 cmp al, 0 70 jne not_null 71 ; if (byte @ rbx == 0) { byte @ rbx = ' '; rcx--; } 72 mov al, ' ' 73 mov [rbx], al 74 dec rcx 75 not_null: 76 77 inc rbx 78 inc rdx 79 jmp args_loop 80 81 args_loop_done: 82 83 ; if (rdx == 0) { rbx = &empty_line; rdx = 1; } else byte @ rbx - 1 = '\n'; 84 cmp rdx, 0 85 je got_no_words 86 mov al, 10 87 mov [rbx-1], al 88 jmp got_words 89 got_no_words: 90 mov rbx, empty_line 91 inc rbx 92 mov rdx, 1 93 got_words: 94 95 ; make rbx point to the start of the multi-line byte-slice, which had all 96 ; nulls replaced by spaces, except for the final null, which was replaced 97 ; by a line-feed 98 sub rbx, rdx 99 100 ; write(stdout, message = rbx, message_length = rdx) 101 mov rax, sys_write 102 mov rdi, stdout 103 mov rsi, rbx 104 syscall 105 ; exit(0) 106 mov rax, sys_exit 107 mov rdi, 0 108 syscall