File: hecho-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 hecho-linux-amd64.o hecho-linux-amd64.asm
  27 ; ld -n -s hecho-linux-amd64.o -o hecho
  28 
  29 
  30 ; hecho [words...]
  31 ;
  32 ; Highlighted ECHO emits a highlighted banner-like line with the words given
  33 ; as arguments, as with the `echo` command.
  34 
  35 
  36 stdout equ 1
  37 sys_write equ 1
  38 sys_exit equ 60
  39 
  40 section .rodata
  41     ansi_invert: db 0x1b, '[7m'
  42     ansi_reset: db 0x1b, '[0m'
  43     empty_line: db 10
  44 
  45 section .text
  46 
  47 global _start
  48 
  49 _start:
  50 
  51 ; rcx = argc
  52 pop rcx
  53 
  54 ; skip argv[0]
  55 pop rbx
  56 ; get argv[1]
  57 pop rbx
  58 
  59 ; find length of byte-slice using the arg-count, replacing each null byte
  60 ; with a space along the way
  61 
  62 ; for (rdx = 0, rcx = rcx - 1; rcx != 0; rcx--, rbx++, rdx++)
  63 mov rdx, 0
  64 dec rcx
  65 args_loop:
  66     ; quit loop when rcx == 0
  67     cmp rcx, 0
  68     je args_loop_done
  69 
  70     ; check current byte
  71     mov al, [rbx]
  72     cmp al, 0
  73     jne not_null
  74         ; if (byte @ rbx == 0) { byte @ rbx = ' '; rcx--; }
  75         mov al, ' '
  76         mov [rbx], al
  77         dec rcx
  78     not_null:
  79 
  80     inc rbx
  81     inc rdx
  82     jmp args_loop
  83 
  84 args_loop_done:
  85 
  86 ; if (rdx == 0) { rbx = &empty_line; rdx = 1; } else ...
  87 cmp rdx, 0
  88 je got_no_words
  89     mov al, 10
  90     mov [rbx-1], al
  91     jmp got_words
  92 got_no_words:
  93     ; write(stdout, empty_line, 1)
  94         mov rax, sys_write
  95         mov rdi, stdout
  96         mov rsi, empty_line
  97         mov rdx, 1
  98         syscall
  99 
 100     ; exit(0)
 101         mov rax, sys_exit
 102         mov rdi, 0
 103         syscall
 104 got_words:
 105 
 106 ; make rbx point to the start of the multi-line byte-slice, which had all
 107 ; nulls replaced by spaces, except for the final null, which was replaced
 108 ; by a line-feed
 109 sub rbx, rdx
 110 
 111 push rbx
 112 push rdx
 113 
 114 ; write(stdout, ansi_invert, 4)
 115     mov rax, sys_write
 116     mov rdi, stdout
 117     mov rsi, ansi_invert
 118     mov rdx, 4
 119     syscall
 120 
 121 pop rdx
 122 pop rbx
 123 
 124 ; write(stdout, message = rbx, message_length = rdx)
 125     mov rax, sys_write
 126     mov rdi, stdout
 127     mov rsi, rbx
 128     syscall
 129 ; write(stdout, ansi_reset, 4)
 130     mov rax, sys_write
 131     mov rdi, stdout
 132     mov rsi, ansi_reset
 133     mov rdx, 4
 134     syscall
 135 ; exit(0)
 136     mov rax, sys_exit
 137     mov rdi, 0
 138     syscall