Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

x86 assembly - how to use Windows API _WriteConsole@4 - masm32 syntax

In result to my post Can I use int21h on windows xp to print things?, I have seen an article about using Windows API's and in this article was a reference to using the _WriteConsole@4 API to print a message to the console. The article is at http://cs.lmu.edu/~ray/notes/x86assembly/.

Here is my code so far:

.386P
.model  flat
extern  _ExitProcess@4:near
extern  _GetStdHandle@4:near
extern  _WriteConsoleA@20:near
public  _go

.data
      msg     byte    'if you get this, it worked.', 10
      handle  dword   ?
      written dword   ?
.code 
start:
_go:    
      push    -11
      call    _GetStdHandle@4
      mov     handle, eax
      push    0
      push    offset written
      push    13
      push    offset msg
      push    handle
      call    _WriteConsoleA@20
      push    0
      call    _ExitProcess@4
end start

I am using this syntax to compile the code: ML:

ml (the file is called test.asm) test.asm /c

Link:

link test.obj C:\masm32\lib\kernel32.lib /SUBSYSTEM:CONSOLE /entry:go

I have gotten it to compile and link, but when I run the .exe that is produced, it does absolutely nothing, not even an error return. The console is just black. Why is this?

Any help would be greatly appreciated. And to the users of this forum, I apologize for bombarding stackoverflow.com every day, it is just that I have very few resources to learn with.

Thanks in advance,

Progrmr

like image 210
Progrmr Avatar asked Sep 10 '25 19:09

Progrmr


1 Answers

This works without problem:

include masm32rt.inc

.data
szMsg       db  "I am in the console!", 0
MSG_LEN     equ $ - szMsg

.data?
BytesWriten dd  ?

.code
start:
    push    STD_OUTPUT_HANDLE
    call    GetStdHandle

    push    NULL
    push    offset BytesWriten
    push    MSG_LEN
    push    offset szMsg
    push    eax
    call    WriteConsole

    push    0
    call    ExitProcess
end start

your entry label is _go yet you tell the linker is is go - /entry:go so it creates the console but does not execute any code! You don't need to tell the linker the entry point in this case, your entry point is start... How does the linker know? The end start

like image 116
Gunner Avatar answered Sep 13 '25 10:09

Gunner