How do you clear the screen and move the cursor in a console app?
I did a search and I got a bunch of hits that were windows related.
Thanks for all your patience with me.
There are library modules in MASM32 to do this.
Try to study this C code
#include <windows.h>
void gotoxy(int x, int y){
HANDLE hnd = NULL;
COORD coord;
coord.X = x;
coord.Y = y;
if (!hnd) hnd = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hnd, coord);
}
void clrscr(void){
HANDLE hnd = NULL;
CONSOLE_SCREEN_BUFFER_INFO csbi;
COORD coord = {0,0};
DWORD dummy;
if (!hnd){
hnd = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hnd, &csbi);
}
FillConsoleOutputCharacter(hnd,' ', csbi.dwSize.X * csbi.dwSize.Y, coord, &dummy);
gotoxy(0,0);
}
The very rough translation of gotoxy in MASM32 will be something like this (http://masm32.com/board/index.php?topic=3078.5):
include \masm32\include\masm32rt.inc
.data?
hStdOut HANDLE ?
.code
start:
invoke GetStdHandle,STD_OUTPUT_HANDLE
mov hStdOut, eax
invoke SetConsoleCursorPosition, eax, 0A0027h
print chr$('X')
invoke SetConsoleCursorPosition, hStdOut, 0C0000h
inkey
exit
end start
Not sure how to translate the clrscr part though :redface:
Put an int 3 before the Print At and launch Olly 8)
include \masm32\MasmBasic\MasmBasic.inc ; download (http://masm32.com/board/index.php?topic=94.0)
Init
.While 1
For_ ct=0 To 15
Print At(ct+1, Locate(y)+1) CColor(cBlue, cYellow) Str$("Line %i", ct)
Next
Inkey CrLf$, "hit any key to continue, c for CLS, Escape to exit"
.Break .if eax==VK_ESCAPE
.if eax=="c"
Cls
PrintLine "Cleared" ; documentation (http://www.webalice.it/jj2006/MasmBasicQuickReference.htm#Mb1120)
Delay 500
.endif
.Endw
EndOfCode
Have a look at the cls & loc macros in the masm32 library
Full translate (?) of the c code
;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
gotoxy PROC x:DWORD,y:DWORD
local hnd:HANDLE,coord:COORD
mov hnd,0
mov eax,x
mov coord.X,ax
mov eax,y
mov coord.Y,ax
invoke GetStdHandle,STD_OUTPUT_HANDLE
mov hnd,eax
.if eax != 0
invoke SetConsoleCursorPosition,hnd, coord
.endif
ret
gotoxy ENDP
clrscr PROC
local hnd:HANDLE,NumberOfChar:DWORD
local csbi:CONSOLE_SCREEN_BUFFER_INFO
local coord:COORD
local dummy:DWORD
mov hnd,0
mov dword ptr [coord],0
invoke GetStdHandle,STD_OUTPUT_HANDLE
mov hnd,eax
.if eax
invoke GetConsoleScreenBufferInfo,hnd, addr csbi
mov edx,0
movzx ecx,csbi.dwSize.Y
movzx eax,csbi.dwSize.X
mul ecx
mov NumberOfChar,eax
invoke FillConsoleOutputCharacter,hnd,32,NumberOfChar , coord,addr dummy
invoke gotoxy,0,0
.endif
ret
clrscr endp
main PROC C argc:DWORD,pargv:DWORD
invoke InitInstance,1
;---- code here --------
invoke printf_s,TXT(13,10,13,10,13,10,"hello,This string must be erased if you press a key",13,10)
invoke _getch
invoke clrscr
invoke printf_s,TXT(13,10,"Press a key to quit",13,10)
invoke _getch
invoke InitInstance,0
mov eax,0
ret
main endp
It's a code who use the Windows 10 crt.If :
"Vcruntime140.dll not found"
You need the "redistributables packages Visual C++ 2015",search MSDN for them.It's a free download
clear screen is not as simple as it should be - lol
moving the cursor is simple - SetConsoleCursorPosition
Quote from: dedndave on October 29, 2015, 12:22:04 AM
clear screen is not as simple as it should be - lol
I agree :bgrin:
But a n-100 MB download to get Vcruntime140.dll is perhaps a little overkill ::)
It can be done with a 53 bytes macro (push/pop ecx included):
include \masm32\include\masm32rt.inc ; plain Masm32 for the fans of pure assembler
include Cls.asm
.code
start:
xor ebx, ebx
.Repeat
print "line "
inc ebx
print str$(ebx), " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", 13, 10
.Until ebx>20
inkey "hit any key"
Cls ; <<<<<<<<<<<<<<<<<<< 53 bytes <<<<<<<<<<<<<<
print "screen cleared!"
MsgBox 0, "Cute", "Hi", MB_OK or MB_SETFOREGROUND
exit
end start
P.S.: For fans of special effects, Cls takes an optional parameter, e.g.
Cls "."
this one allows you to clear individual screen buffers
it can easily be modified to clear the main buffer
OPTION PROLOGUE:None
OPTION EPILOGUE:None
ConCls PROC hConsole:HANDLE
;Clear Console Screen Buffer, DednDave 12-2011
;
; This function allows the use of multiple screen buffers, clearing
;only the buffer specified by hConsole. Space characters and the current
;foreground/background attributes for the specified buffer are used.
;
;Call With: hConsole = handle of console screen buffer to clear
;
; Returns: EAX = screen position (0,0)
; ECX = total number of characters in screen buffer
; EDX = buffer character attributes used to clear
; all other registers preserved
push ebx
push edi
mov ebx,[esp+12] ;EBX = hConsole
xor edi,edi
sub esp,(sizeof CONSOLE_SCREEN_BUFFER_INFO+3) and -4
INVOKE GetConsoleScreenBufferInfo,ebx,esp
movzx eax,word ptr [esp].CONSOLE_SCREEN_BUFFER_INFO.dwSize.x
movzx edx,word ptr [esp].CONSOLE_SCREEN_BUFFER_INFO.dwSize.y
mul edx
push edx
INVOKE FillConsoleOutputCharacter,ebx,32,eax,edi,esp
pop eax
movzx edx,word ptr [esp].CONSOLE_SCREEN_BUFFER_INFO.wAttributes
push edx
push eax
INVOKE FillConsoleOutputAttribute,ebx,edx,eax,edi,esp
INVOKE SetConsoleCursorPosition,ebx,edi
pop ecx
pop edx
xchg eax,edi
add esp,(sizeof CONSOLE_SCREEN_BUFFER_INFO+3) and -4
pop edi
pop ebx
ret 4
ConCls ENDP
OPTION PROLOGUE:PrologueDef
OPTION EPILOGUE:EpilogueDef
geez .. you guys are awesome!
thanks again for all your patience with me