linux nasm assembly clear screen in terminal

前端 未结 2 389
耶瑟儿~
耶瑟儿~ 2020-12-18 17:02

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.

2条回答
  •  感动是毒
    2020-12-18 17:34

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

提交回复
热议问题