; The MIT License (MIT) ; ; Copyright (c) 2026 pacman64 ; ; Permission is hereby granted, free of charge, to any person obtaining a copy ; of this software and associated documentation files (the "Software"), to deal ; in the Software without restriction, including without limitation the rights ; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell ; copies of the Software, and to permit persons to whom the Software is ; furnished to do so, subject to the following conditions: ; ; The above copyright notice and this permission notice shall be included in ; all copies or substantial portions of the Software. ; ; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR ; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, ; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE ; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER ; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, ; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE ; SOFTWARE. ; To compile this app for linux/amd64, use the commands ; ; nasm -f elf64 -o echo-linux-amd64.o echo-linux-amd64.asm ; ld -n -s echo-linux-amd64.o -o echo ; echo [words...] ; ; Emit a line with all the words given as command-line arguments. stdout equ 1 sys_write equ 1 sys_exit equ 60 section .rodata empty_line: db 10 section .text global _start _start: ; rcx = argc pop rcx ; skip argv[0] pop rbx ; get argv[1] pop rbx ; find length of byte-slice using the arg-count, replacing each null byte ; with a space along the way ; for (rdx = 0, rcx = rcx - 1; rcx != 0; rcx--, rbx++, rdx++) mov rdx, 0 dec rcx args_loop: ; quit loop when rcx == 0 cmp rcx, 0 je args_loop_done ; check current byte mov al, [rbx] cmp al, 0 jne not_null ; if (byte @ rbx == 0) { byte @ rbx = ' '; rcx--; } mov al, ' ' mov [rbx], al dec rcx not_null: inc rbx inc rdx jmp args_loop args_loop_done: ; if (rdx == 0) { rbx = &empty_line; rdx = 1; } else byte @ rbx - 1 = '\n'; cmp rdx, 0 je got_no_words mov al, 10 mov [rbx-1], al jmp got_words got_no_words: mov rbx, empty_line inc rbx mov rdx, 1 got_words: ; make rbx point to the start of the multi-line byte-slice, which had all ; nulls replaced by spaces, except for the final null, which was replaced ; by a line-feed sub rbx, rdx ; write(stdout, message = rbx, message_length = rdx) mov rax, sys_write mov rdi, stdout mov rsi, rbx syscall ; exit(0) mov rax, sys_exit mov rdi, 0 syscall