flat assembler
Message board for the users of flat assembler.
Index
> Windows > Console Programming with Win32 API |
Author |
|
Madis731 17 May 2004, 08:43
Could you give us some code, perhaps somebody (like me :$) didn't get it working following all your hints.
Thanks |
|||
17 May 2004, 08:43 |
|
inskipp 17 May 2004, 14:30
I wrote this small piece of code some time ago, it may be helpful.
Code: format PE console 4.0 entry start include '%fasminc%/win32a.inc' section '.code' code readable executable start: invoke CreateConsoleScreenBuffer,GENERIC_WRITE,0,0,1,0 mov [hCSB],eax inc eax jz exit ;if eax=-1 error invoke SetConsoleActiveScreenBuffer,[hCSB] invoke FillConsoleOutputCharacter,\ [hCSB],'Q',\ 80*25,0,Temp invoke Sleep,4000 invoke CloseHandle,[hCSB] exit: invoke ExitProcess,0 section '.data' data readable writeable hCSB dd ? Temp dd ? ;include 'csb.imp' section '.idata' import data readable writeable library kernel32,'kernel32.dll' import kernel32,\ CloseHandle,'CloseHandle',\ CreateConsoleScreenBuffer,'CreateConsoleScreenBuffer',\ ExitProcess,'ExitProcess',\ FillConsoleOutputCharacter,'FillConsoleOutputCharacterA',\ SetConsoleActiveScreenBuffer,'SetConsoleActiveScreenBuffer',\ Sleep,'Sleep' |
|||
17 May 2004, 14:30 |
|
MattBro 23 Sep 2004, 18:06
OK sorry it took so long to get back to you. Here is an example of a console running from within a normal windows programming context. ie you don't need to use format PE console directives.
Code: ; An example of running a console without compiling in console mode include '%fasminc%\win32ax.inc' entry start ; This entry is missing from the windows includes CONSOLE_TEXTMODE_BUFFER = 1 section '.data' data readable writeable inHandle dd ? ; DWORD: handle to standard input device outHandle dd ? ; DWORD handle to standard output device bytesWritten dd ? ; DWORD: number of bytes written cons_buflen dd 0 ; length of string in input buffer cons_inbuff db 0 ; start of buffer times 1023 db 0 hello db 'Greetings Earthling ! Do not be alarmed;' db 'your death will be painless',10,13 db 'Enter <cntrl>q to quit',10,13,0 newline db 10,13,0 _t777: times 100 db 0 _gencap db 'MacroFlow',0 section '.code' code readable executable start: ; ; allocate a console, get its handle and write to it invoke AllocConsole invoke GetStdHandle, dword STD_OUTPUT_HANDLE ; invoke CreateConsoleScreenBuffer, GENERIC_WRITE, dword 0, NULL, CONSOLE_TEXTMODE_BUFFER, NULL ; Put the address of the handle in ; our variable ; call apifail mov [outHandle],eax invoke GetStdHandle, dword STD_INPUT_HANDLE mov [inHandle], eax ; make the screen buffer 'active' invoke SetConsoleActiveScreenBuffer, eax mov eax, hello call Write2Cons ; debug step for processing commands, load stack with stuff ; dpush 7 MFCons.top: call ReadCons xor edx, edx mov dl, byte [eax] cmp dl, 11H ; control Q check je MFCons.done call Write2Cons jmp MFCons.top MFCons.done: invoke FreeConsole jmp goodbye ; ( -- strlen) proc StrLength, StringBuff ; get length of string StringBuff ; StringBuff ; pointer to string ; ; StringBuff must be null terminated ; incrementing eax. The value in eax is the string length ; ; returns: eax = length of string StringBuff enter mov edi, [StringBuff] ; dpush 0 ; character count mov eax,0 ; character count ; Aprintf 'finding strlen of %s ', dword [ptrString] StrLength.top: ; top of loop cmp [edi], byte 0 ; check for string null termination je StrLength.done ; if null, jump out of loop and return inc edi ; pointer to next byte inc eax ; increment counterc jmp StrLength.top ; back to top of loop ; jump here to return StrLength.done: return endp ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Writes a null terminated ASCII string to the console ; String is in (eax) Write2Cons: ; ; Writes a null-terminated string in bstring to the console window mov edx, eax ; () stdcall StrLength,edx ; return length of string in eax cld ; clear the direction flag ; must do this before WriteConsole ; consHandle ; console output handle ; edx: points to string ; eax string length ; bytesWritten: returns number of bytes written (ptr) ; Aprintf 'consHandle: %x strlen: %d hello: %s',dword [consHandle], eax, edx invoke WriteConsole, dword [outHandle], edx, eax, bytesWritten, 0 ; Aprintf 'bytes written: %d', dword [bytesWritten] ret ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Read text from the console (up to 256 bytes) ; The output is a MacroFlow style string ; in eax ReadCons: mov eax, 0 invoke ReadConsole, dword [inHandle], cons_inbuff, dword 1023,cons_buflen, 0 ; add an extra null termination mov eax, dword [cons_buflen] ; (buflen) mov [cons_inbuff + eax], byte 0 ; add overhead size to length parameter ; should be equal to tmpa.hsiz defined in mf4.inc add eax, 6 mov [cons_buflen], eax mov eax, cons_inbuff ret goodbye: invoke ExitProcess, 0 section '.imp' import data readable writeable library kernel32,'KERNEL32.DLL',\ user,'USER32.DLL' import kernel32,\ AllocConsole,'AllocConsole',\ CreateConsoleScreenBuffer,'CreateConsoleScreenBuffer',\ FreeConsole, 'FreeConsole',\ GetStdHandle,'GetStdHandle',\ WriteConsole, 'WriteConsoleA',\ ReadConsole, 'ReadConsoleA',\ ExitProcess, 'ExitProcess',\ SetConsoleActiveScreenBuffer, 'SetConsoleActiveScreenBuffer' import user,\ MessageBox,'MessageBoxA',\ wsprintf,'wsprintfA' The program reads what you typed and then echos it back to the console. A good starting point for generating console interactive io from a regular windows program. Regards, -Matt |
|||
23 Sep 2004, 18:06 |
|
Vortex 23 Sep 2004, 19:57
Hutch's masm32 package provides very usefull functions for console applications. The simple example below prints 10 times a string.
Code: format PE CONSOLE 4.0 entry start Include '%fasminc%\win32a.inc' Include '%fasminc%\macro\if.inc' Include 'Struct.inc' section '.data' data readable writeable msg db 'Console application',0 section '.code' code readable executable start: call ClearScreen push esi xor esi,esi @@: inc esi stdcall locate,esi,esi stdcall StdOut,msg cmp esi,10 jne @b pop esi invoke ExitProcess,0 include 'Locate.asm' include 'Clearscr.asm' include 'Strlen.asm' include 'Stdout.asm' section '.idata' import data readable writeable library kernel32,'kernel32.dll' import kernel32,\ ExitProcess,'ExitProcess',\ FillConsoleOutputCharacter,'FillConsoleOutputCharacterA',\ GetConsoleScreenBufferInfo,'GetConsoleScreenBufferInfo',\ GetStdHandle,'GetStdHandle',\ SetConsoleCursorPosition,'SetConsoleCursorPosition',\ WriteFile,'WriteFile'
_________________ Code it... That's all... |
|||||||||||
23 Sep 2004, 19:57 |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.