I'm exceptionally new to assembly. I only picked it up yesterday and I've looked through many examples and still can't figure out for myself how to write to the console. I always get an error when I seem to replicate it in my own way.
Asked
Active
Viewed 1.1k times
2 Answers
15
The easiest way is to use the C functions. In its simplest use, printf()
takes a string as a parameter and writes it on the standard output.
This code should work:
format PE console
entry start
include 'win32a.inc'
section '.text' code executable
start:
push hello
call [printf]
pop ecx
push 0
call [ExitProcess]
section '.rdata' data readable
hello db 'Hello world!', 10, 0
section '.idata' data readable import
library kernel32, 'kernel32.dll', \
msvcrt, 'msvcrt.dll'
import kernel32, ExitProcess, 'ExitProcess'
import msvcrt, printf, 'printf'

Bastien Léonard
- 60,478
- 20
- 78
- 95
-
this closes the window immediately :( – jwzumwalt Feb 21 '19 at 05:52
-
2@jwzumwalt The simplest solution is to keep the command line program open. An alternative is to call getchar() or similar at the end of the program, or create a batch file which calls your program, then uses the pause command. Let me know if you need more information. – Bastien Léonard Feb 21 '19 at 14:31
-
how to do it without the include – B''H Bi'ezras -- Boruch Hashem Aug 13 '20 at 05:21
-
The only code on the internet that actually works, for this purpose. – MathCrackExchange Aug 25 '21 at 10:48
13
Use WriteConsole.
include 'win32wxp.inc'
.code
start:
invoke AllocConsole
invoke WriteConsole,<invoke GetStdHandle,STD_OUTPUT_HANDLE>,tex,12,dummy,0
invoke Sleep,-1
.end start
.data
tex TCHAR 'Hello World!'
dummy rd 1

Jens Björnhager
- 5,632
- 3
- 27
- 47
-
If you want to be able to use pipe redirection of your output, you can use `WriteFile` in a similar manner. – Jens Björnhager Sep 21 '12 at 15:41
-