NASM Forum > Example Code

Command 'Clear' (Clear the gnu/linux console)

(1/2) > >>

Ferran:
Hello again everybody:

I believe that useful programs are those that solve a need. In my case I was a little tired of printing messages on the console within the rest of commands, directory listings or other instructions that had nothing to do with the currently execution of a program. For me it was necessary to invoke the 'Clear' function to clear my console window of anything that disturbed my view.

Thus, this is a program able to invoke the gnu/linux command 'Clear' to clear the console. It's not necessary to run it as if, you could use the section data (to setup the structures) and section text (to invoke the clear function) separately and add it in your own programs.

I hope it will be useful as to me.


--- Code: ---; sysClear.asm
; Invokes command 'clear' to clear the gnu/linux console,
; (really it uses the bash command: echo -en "\033c")
; Compile: nasm -f elf sysClear.asm
; Link: ld -m elf_i386 sysClear.o -o sysClear
; Run: ./sysClear
;
; Author: Ferran Trapé
; Date: August 20, 2020

SECTION .data

; Structures

command        db '//bin/echo', 0h ; command to execute as string
        arg1   db '-en', 0h                    ; option -en in echo
        arg2   db  '\033c', 0h                ; ANSI escape code to clear
 
arguments      dd command
        dd arg1                          ; argument1 to pass to commandline
        dd  arg2                                   ; argument2
    dd 0h                                    ; end of structure

environment dd 0h          ; No one arguments to pass as environment variables

SECTION .text
 global _start

_start:

; Function clear

 mov eax, 11              ; invoke SYS_EXECVE
 mov ebx, command            ; address of the file to execute
 mov ecx, arguments          ; address of the arguments to pass to the commandline
 mov edx, environment            ; address of the environment variables
 int    80h

exit:

 mov        eax, 1                              ;sys_exit
 int           80h                                  ;system

--- End code ---

Tip:

Also we could to use it to change the color of the text and backgroud. Only you need to explore the list of ANSI escape codes here

fredericopissarra:
Simplier, no process exec:


--- Code: ---; For amd64:
;
; nasm -felf64 -o cls.o cls.asm
; ld -s -o cls cls.o

  bits    64
  default rel

  section .text

  global  _start
_start:
  ; sys_write()
  mov     eax,1
  mov     edi,eax
  lea     rsi,[msg]
  mov     edx,msglen
  syscall

  ; sys_exit()
  mov     eax,60
  xor     edi,edi
  syscall

  section .rodata

  ; Notice the backquotes...
msg:
  db      `\033[2J\033[H`
msglen    equ $ - msg
--- End code ---

Ferran:
Hello again everybody:

Today I bring you a variation of this program that is a mix between my previous code (see above) and the specifications of @federicopissarra that are as accurate as ever.


--- Code: ---; -------------------------------------------------------------------------------
; cls.asm (v.1.1)
; Clear the screen & display a sympa text
; (supported with extern printf instruction)

; Compile: nasm -f elf64 cls.asm
; link: gcc -no-pie cls.o -o cls
; run: ./cls
;
; Auhors: Ferran & federicopisarra (NASM forum members)
; -------------------------------------------------------------------------------

bits 64
default rel

segment .rodata
cls: db `\033[2J\033[H`, 0
fmt: db "%s",10, 0
fmc: db "%s", 0

segment .data
msg1: db "Hello, NASM members!", 10, 0

segment .text
global main
 
extern printf

main:
; -------- clear screen ----------
.cls:
push rbp
lea rdi, [fmc]
lea rsi, [cls]
call printf ; wrt ..plt
mov rax, 0
pop rbp

; -------- print message ---------
.print:
push rbp
lea rdi, [fmt]
lea rsi, [msg1]
call printf ; wrt ..plt
mov rax, 0
pop rbp

; ------- end program ------------
.exit:
mov eax, 60
xor edi, edi
syscall
 
 
--- End code ---

Note: I wanted to do it without "printf", but I couldn't get the desired effect. By the moment this works fine and is coded quickly.

You will like it.

fredericopissarra:
Same one as before. Change 'msg' to:

--- Code: ---msg:
  db      `\033[2J\033[H`
  db      `Hello, NASM members!\n`
msglen    equ $ - msg
--- End code ---

               

Ferran:
OK Fred, let me to test this retrieving your first code and soon i will come back with it.

P.D: Today we've grandmother's lasagna

Navigation

[0] Message Index

[#] Next page

Go to full version