flat assembler
Message board for the users of flat assembler.
Index
> Windows > clear screen - win32 console |
Author |
|
int0x50 16 Jun 2023, 07:54
i am writing a PE64 console application... i am struggling to clear the screen ..
the escape characters through printf is not working out ... |
|||
16 Jun 2023, 07:54 |
|
int0x50 16 Jun 2023, 09:12
thanks Flier-Mate
|
|||
16 Jun 2023, 09:12 |
|
Picnic 16 Jun 2023, 11:05
|
|||
16 Jun 2023, 11:05 |
|
int0x50 16 Jun 2023, 11:47
edit: it worked after including API\KERNEL32.INC into import table. the FillConsoleOutputCharacter should be FillConsoleOutputCharacterA.
I have tried the following program .. the compilation is happening ... but it's not working ... when I tried debugging it I get STATUS_ENTRYPOINT_NOT_FOUND... section '.data' data readable writeable handle_getstd dw 1 csbi rb 18 count dw 0 dwLen dd 0 dwCoord dd 0 lpWritten dd 0 section '.text' code readable executable main: push rax push rbx push rcx push rdx xor eax, eax mov eax, -11 invoke GetStdHandle, -11 mov dword [handle_getstd], eax xor rdx, rdx xor rcx, rcx invoke GetConsoleScreenBufferInfo, dword [handle_getstd], addr csbi xor ecx, ecx xor edx, edx mov byte cl, byte [csbi] mov byte dl, byte [csbi + 2] imul ecx, edx mov dword [dwLen], ecx invoke FillConsoleOutputCharacter, dword [handle_getstd], 0x20, dword [dwLen], dword [dwCoord], addr lpWritten pop rdx pop rcx pop rbx pop rax ret section '.idata' import data readable library msvcrt, 'msvcrt.dll',\ kernel32, 'kernel32.dll' import msvcrt,\ printf, 'printf' import kernel32,\ GetStdHandle, 'GetStdHandle',\ GetConsoleScreenBufferInfo, 'GetConsoleScreenBufferInfo', \ FillConsoleOutputCharacter, 'FillConsoleOutputCharacter' |
|||
16 Jun 2023, 11:47 |
|
bitRAKE 16 Jun 2023, 15:59
I'd recommend using fasmg.
Assuming you are using a terminal that supports ANSI codes, this should work: Code: format PE64 CONSOLE 6.2 at 0x1_4000_0000 include 'format\format.inc' include 'macro\import64.inc' include '..\..\utility\align.inc' section '.data' data readable writeable _clear_screen db 27,'[H',27,'[J' _clear_screen.characters := $ - _clear_screen section '.text' code readable executable main: entry $ virtual at rsp dq ? ; shadow space dq ? dq ? dq ? .P5 dq ? ; fifth parameter ; some local space on the stack for temporary data: .hStdOut dq ? .dwTemp dd ? align.assume rsp, 16 align 16 | 8 .stack_offset := $ - $$ end virtual sub rsp, .stack_offset mov rcx, -11 ; STD_OUTPUT_HANDLE call [GetStdHandle] mov [.hStdOut], rax mov [.P5], 0 lea r9, [.dwTemp] mov r8d, _clear_screen.characters lea rdx, [_clear_screen] mov rcx, [.hStdOut] call [WriteConsoleA] mov ecx, 0 call [ExitProcess] int3 section '.idata' import data readable library kernel32, 'kernel32.dll' import kernel32,\ ExitProcess, 'ExitProcess',\ GetStdHandle, 'GetStdHandle',\ WriteConsoleA, 'WriteConsoleA' FYI: Please, use code tags. _________________ ¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup |
|||
16 Jun 2023, 15:59 |
|
Flier-Mate 16 Jun 2023, 17:05
If I remember correctly, in command prompt, the program has to set ENABLE_VIRTUAL_TERMINAL_PROCESSING flag through SetConsoleMode function to enable ANSI escape sequence.
Further reading: https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences Thanks for sharing, @Picnic and @bitRAKE, although both approaches are different. |
|||
16 Jun 2023, 17:05 |
|
Flier-Mate 16 Jun 2023, 17:08
Example in C provided by Microsoft to enable VT mode:
Code: HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); if (hOut == INVALID_HANDLE_VALUE) { return false; } DWORD dwMode = 0; if (!GetConsoleMode(hOut, &dwMode)) { return false; } dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; if (!SetConsoleMode(hOut, dwMode)) { return false; } |
|||
16 Jun 2023, 17:08 |
|
Flier-Mate 16 Jun 2023, 17:11
int0x50 wrote: thanks Flier-Mate You're welcomed. Glad that you solved it, but you may want to try ANSI escape code to clear screen as well. |
|||
16 Jun 2023, 17:11 |
|
int0x50 20 Jun 2023, 06:26
@bitRAKE, what do you mean by " I'd recommend using fasmg." ... how should I read about fasm vs fasmg ?
|
|||
20 Jun 2023, 06:26 |
|
int0x50 20 Jun 2023, 06:32
@bitRAKE, isn't going about high-level with fasmg? I would like to see something at low-level and would like to still have control on many things... in that case is fasmg still useful?
|
|||
20 Jun 2023, 06:32 |
|
revolution 20 Jun 2023, 11:06
fasmg is a macro language. To use it to assemble x86 you also need to use the x86 macros to parse the source.
fasm is an assembler proper. Use it stand-alone to assemble x86 code directly. |
|||
20 Jun 2023, 11:06 |
|
bitRAKE 20 Jun 2023, 20:49
int0x50 wrote: @bitRAKE, what do you mean by " I'd recommend using fasmg." ... how should I read about fasm vs fasmg ? I don't know if there is a fasm vs fasmg? fasmg is more general, supporting big signed integers, combined stages, and a secondary macro (CALM). fasm uses {} to block macros. fasmg starts blocks with the <keyword> and terminates blocks with END <keyword>. In many respects they are very similar, but x86 is not built in to fasmg - it can work generally on text in unexpected ways. int0x50 wrote: @bitRAKE, isn't going about high-level with fasmg? I would like to see something at low-level and would like to still have control on many things... in that case is fasmg still useful? My aim is creating something featureful - not compatible syntax. Like the advanced align macro, which has very necessary features that make coding easier, imho. Featureful doesn't mean restrictive either - should always be able to manually control code/abstraction generation, imho. The downside is that I'm not in a hurry to complete anything. The present broken work is like a dozen iterations in. Create something, use it for a while, hate it, try again, ... Both fasm and fasmg work at several (compatible) abstraction levels with the present Windows macros/includes. I'd try them first until you desire something else. _________________ ¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup Last edited by bitRAKE on 21 Jun 2023, 10:04; edited 1 time in total |
|||
20 Jun 2023, 20:49 |
|
int0x50 21 Jun 2023, 05:43
thank you @bitRAKE ... thanks @revolution ...
|
|||
21 Jun 2023, 05:43 |
|
int0x50 22 Jun 2023, 09:23
"You're welcomed. Glad that you solved it, but you may want to try ANSI escape code to clear screen as well. "
@Flier-Mate, how would you do that? |
|||
22 Jun 2023, 09:23 |
|
Flier-Mate 22 Jun 2023, 09:58
As I said above, use SetConsoleMode to enable VT mode, then you can start sending escape character to console.
Code: format PE console entry main include 'win32a.inc' section '.data' data readable writeable _handle dd ? _cls db 27,'[H',27,'[J' _len = $ - _cls _mode dd ? _dummy dd ? section '.text' code readable executable main: push -11 call [GetStdHandle] mov dword [_handle], eax push _mode push [_handle] call [GetConsoleMode] or [_mode], 4 ; ENABLE_VIRTUAL_TERMINAL_PROCESSING push [_mode] push [_handle] call [SetConsoleMode] push 0 push _dummy push _len push _cls push [_handle] call [WriteConsoleA] push 0 call [ExitProcess] section '.idata' import data readable library kernel32, 'kernel32.dll' import kernel32,\ GetStdHandle, 'GetStdHandle',\ GetConsoleMode, 'GetConsoleMode',\ SetConsoleMode, 'SetConsoleMode',\ WriteConsoleA, 'WriteConsoleA',\ ExitProcess, 'ExitProcess' |
|||
22 Jun 2023, 09:58 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.