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