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