linux nasm assembly clear screen in terminal

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-18 05:27:09

问题


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.)



来源:https://stackoverflow.com/questions/13206629/linux-nasm-assembly-clear-screen-in-terminal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!