linux nasm assembly clear screen in terminal

2020-02-11 18:17发布

问题:

Is there a way to clear the screen in a terminal window with nasm? By clear the screen, I mean emulate the Ctrl-L hotkey. Remove all text from the window.

Is this possible to do in nasm assembly?

Thanks in advance,

Rileyh

回答1:

In Bash:

echo -ne "\033[H\033[2J"

In C:

printf("\033[H\033[2J");

How do I find the string:

$ strace -e trace=write clear >/dev/null 
write(1, "\33[H\33[2J", 7)              = 7
Process 7983 detached


回答2:

Have a look at this NASM program:

http://www.muppetlabs.com/~breadbox/software/tiny/snake.asm.txt

There's an interesting part showing how to write escape sequences to the stdout:

%define SC_write        4   ; eax = write(ebx, ecx, edx)
%define ESC         033q

; (...)

refresh:
        mov eax, ESC | ('[' << 8) | (BOTTOMROW << 16)
        stosd
        mov eax, ';0H' | (SI << 24)
        stosd
        mov edx, edi
        mov edi, outbuf
        mov ecx, edi
        sub edx, ecx
        xor ebx, ebx
        lea eax, [byte ebx + SC_write]
        inc ebx
        int 0x80

The code doesn't probably do exactly what you want to, but it'd be easy to modify it to output \033[H\033[2J. Also have a look here:

http://ascii-table.com/ansi-escape-sequences-vt-100.php

Plus, if you want your code to be portable, think of using some library that's compatible among different terminals, like ncurses.

(EDIT: That was for Linux. If you're on Windows, I'd try this.)