Author Topic: Command 'Clear' (Clear the gnu/linux console)  (Read 24278 times)

Offline Ferran

  • Jr. Member
  • *
  • Posts: 23
  • Country: ad
Command 'Clear' (Clear the gnu/linux console)
« on: August 20, 2020, 08:27:07 AM »
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: [Select]
; 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

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

Offline fredericopissarra

  • Full Member
  • **
  • Posts: 373
  • Country: br
Re: Command 'Clear' (Clear the gnu/linux console)
« Reply #1 on: August 20, 2020, 08:51:58 AM »
Simplier, no process exec:

Code: [Select]
; 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

Offline Ferran

  • Jr. Member
  • *
  • Posts: 23
  • Country: ad
Re: Command 'Clear' (Clear the gnu/linux console)
« Reply #2 on: September 06, 2020, 09:04:39 AM »
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: [Select]
; -------------------------------------------------------------------------------
; 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
 
 

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.

Offline fredericopissarra

  • Full Member
  • **
  • Posts: 373
  • Country: br
Re: Command 'Clear' (Clear the gnu/linux console)
« Reply #3 on: September 06, 2020, 10:14:19 AM »
Same one as before. Change 'msg' to:
Code: [Select]
msg:
  db      `\033[2J\033[H`
  db      `Hello, NASM members!\n`
msglen    equ $ - msg

               

Offline Ferran

  • Jr. Member
  • *
  • Posts: 23
  • Country: ad
Re: Command 'Clear' (Clear the gnu/linux console)
« Reply #4 on: September 06, 2020, 11:47:18 AM »
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

Offline fredericopissarra

  • Full Member
  • **
  • Posts: 373
  • Country: br
Re: Command 'Clear' (Clear the gnu/linux console)
« Reply #5 on: September 06, 2020, 12:05:03 PM »
P.D: Today we've grandmother's lasagna
Ahhhhhh... please, don't tempt me!  ;D

Offline Ferran

  • Jr. Member
  • *
  • Posts: 23
  • Country: ad
Re: Command 'Clear' (Clear the gnu/linux console)
« Reply #6 on: September 06, 2020, 03:36:03 PM »
All right, here we go again after the feast.

Finally i clean the screen without using the printf C instruction. Pure Assembly with NASM only. I was impressed with how easy it was to adapt federicopissarra's code and how well everything worked.

well, if you wants clear screen with printf C instruction see it above, but if you don't like this option, you could to use the next code:

Code: [Select]

; -----------------------------------------------------------------------
; cls.asm (ver.2.0)
; Clear the screen & display a sympa text
; (pure GNU/linux assembly x80_64 code with NASM)
;
; Compile: nasm -f elf64 cls.asm
; link: ld -s -o cls cls.o
; run: ./cls
;
; Auhors: Ferran & federicopisarra (NASM forum members)
; -----------------------------------------------------------------------

bits 64

segment .rodata
msg:
        db `\033[2J\033[H`, 0           ; ANSI code for clear screen
        db `Hello, NASM members!`,10,0  ; Sympa message
        msglen equ $ - msg              ; Length of msg

segment .text
global _start

_start:

; ------- clear screen ------------------

        mov rax,1                       ; 1 = sys_write number
        mov rdi,1                       ; 1 = STDOUT file descriptor
        mov rsi,msg                     ; input the ANSI code from msg
        mov rdx,msglen                  ; input the length of msg
        syscall                         ; Kernel calls

; ------- print message -----------------

        mov rax,1                       ; 1 = sys_write number
        mov rdi,1                       ; 1 = STDOUT file descriptor
        mov rsi,msg                     ; message's buffer
        mov rdx,msglen                  ; input the length
        syscall                         ; Kernel calls

; ------- end program ------------------
        mov eax, 60
        xor edi, edi
        syscall

Enjoy it !
« Last Edit: September 06, 2020, 03:59:10 PM by Ferran »