File: cls-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 cls-linux-amd64.o cls-linux-amd64.asm
  27 ; ld -n -s cls-linux-amd64.o -o cls
  28 ; rm cls-linux-amd64.o
  29 
  30 
  31 ; cls
  32 ;
  33 ; CLear Screen
  34 ;
  35 ; Options, also with optional leading single or double dash
  36 ;
  37 ;    h, help    show this help message
  38 
  39 
  40 stdout equ 1
  41 sys_write equ 1
  42 sys_exit equ 60
  43 
  44 lf equ 10 ; line feed
  45 esc equ 0x1b ; escape
  46 
  47 section .rodata
  48 
  49 ;clear: db esc, '[H', esc, '[2J', esc, '[3J'
  50 ;clear_length equ $ - clear
  51 clear:
  52     db esc, 'c', esc, '[!p', esc, '[?3;4l', esc, '[4l', esc, '>', esc, '[?69l'
  53 clear_length equ $ - clear
  54 
  55 info:
  56     db 'cls [options...]', lf
  57     db lf
  58     db 'CLear Screen', lf
  59     db lf
  60     db 'Options, also with optional leading single or double dash', lf
  61     db lf
  62     db '    h, help    show this help message', lf
  63 info_length equ $ - info
  64 
  65 section .text
  66 
  67 global _start
  68 
  69 _start:
  70 
  71 ; rcx = argc
  72 pop rcx
  73 
  74 cmp rcx, 1
  75 je no_args
  76 
  77 ; skip argv[0]
  78 pop rbx
  79 ; get argv[1]
  80 pop rbx
  81 
  82 ; check/skip (optional) leading dash
  83     mov al, [rbx + 0]
  84     cmp al, '-'
  85     jne no_dashes
  86     inc rbx
  87 ; check/skip (optional) second leading dash
  88     mov al, [rbx + 0]
  89     cmp al, '-'
  90     jne single_dash
  91     inc rbx
  92 single_dash:
  93 no_dashes:
  94 ; check letter 'h'
  95     mov al, [rbx + 0]
  96     cmp al, 'h'
  97     jne no_args
  98 ; check null byte: `h` is a valid help-option
  99     mov al, [rbx + 1]
 100     cmp al, 0
 101     je help
 102 ; check letter 'e'
 103     mov al, [rbx + 1]
 104     cmp al, 'e'
 105     jne no_args
 106 ; check letter 'l'
 107     mov al, [rbx + 2]
 108     cmp al, 'l'
 109     jne no_args
 110 ; check letter 'p'
 111     mov al, [rbx + 3]
 112     cmp al, 'p'
 113     jne no_args
 114 ; check null byte: `help` is also a valid help-option
 115     mov al, [rbx + 4]
 116     cmp al, 0
 117     je help
 118 
 119 no_args:
 120     ; write(stdout, clear, clear_length)
 121     mov rax, sys_write
 122     mov rdi, stdout
 123     mov rsi, clear
 124     mov rdx, clear_length
 125     syscall
 126 
 127 quit:
 128     ; exit(0)
 129     mov rax, sys_exit
 130     mov rdi, 0
 131     syscall
 132 
 133 help:
 134     ; write(stdout, info, info_length)
 135     mov rax, sys_write
 136     mov rdi, stdout
 137     mov rsi, info
 138     mov rdx, info_length
 139     syscall
 140 
 141     jmp quit