NASM - The Netwide Assembler
NASM Forum => Example Code => Topic started by: Ferran 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.
; 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 (https://en.wikipedia.org/wiki/ANSI_escape_code)
-
Simplier, no process exec:
; 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
-
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.
; -------------------------------------------------------------------------------
; 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.
-
Same one as before. Change 'msg' to:
msg:
db `\033[2J\033[H`
db `Hello, NASM members!\n`
msglen equ $ - msg
-
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
-
P.D: Today we've grandmother's lasagna
Ahhhhhh... please, don't tempt me! ;D
-
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:
; -----------------------------------------------------------------------
; 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 !