The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: realcr on October 20, 2006, 03:13:09 PM

Title: Console mode in win32
Post by: realcr on October 20, 2006, 03:13:09 PM
Hi everyone!

I always write window based assembly code , but today I figured out I have no idea how to use printf on console using masm.
I tried importing the printf function and it didn't work. I know for sure someone probably asked this question before but I couldn't find it myself in this forum.
Can anyone point me to some simple "hello world" program using console mode 32 bit ?

thanks,
bar.

Title: Re: Console mode in win32
Post by: redskull on October 20, 2006, 04:32:43 PM
.486   
.model flat, stdcall
option casemap:none
         
includelib \masm32\lib\kernel32.lib   
include \masm32\include\kernel32.inc

STD_OUTPUT_HANDLE  equ -11
STD_INPUT_HANDLE equ -10

.data?               
hConsoleOutput    DWORD ?
hConsoleInput   DWORD ?      
lpcchWritten   DWORD ?
InputString   DWORD ?
lpcchRead      DWORD ?

.data                  
DisplayString db "Press Enter to exit"

.code                  
start:               

invoke AllocConsole
invoke GetStdHandle, STD_OUTPUT_HANDLE
mov hConsoleOutput, eax
invoke GetStdHandle, STD_INPUT_HANDLE
mov hConsoleInput, eax


invoke WriteConsole, hConsoleOutput, ADDR DisplayString, \
                      SIZEOF DisplayString, ADDR lpcchWritten, \
                      0


invoke ReadConsole, hConsoleInput, ADDR InputString, 1, \
                     ADDR lpcchWritten, 0

invoke ExitProcess, 0
end start
Title: Re: Console mode in win32
Post by: Vortex on October 20, 2006, 05:01:30 PM
realcr,

Here is an example with printf :

.386
.model flat, stdcall
option casemap :none

include     \masm32\include\kernel32.inc
include     msvcrt.inc

includelib  \masm32\lib\kernel32.lib
includelib  \masm32\lib\msvcrt.lib

.data
message     db 'Hello world!',0

.code

start:

    invoke  printf,ADDR message
    invoke  ExitProcess,0

END start

[attachment deleted by admin]
Title: Re: Console mode in win32
Post by: realcr on October 20, 2006, 08:17:13 PM
Great thanks vortex and redskull!

bar.