X86 build - how to use the Windows API _WriteConsole @ 4 - masm32 syntax

As a result of my message Can I use int21h for windows xp for printing? . I saw an article about using the Windows API and in this article alluded 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:

.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 use this syntax to compile 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 got it for compilation and reference, but when I run the created .exe, it does nothing, even the error is returned. The console is just black. Why is this?

. stackoverflow.com , .

,

Progrmr

+3
3

-, , , NASM, , , MASM , . MASM, NASM. , Assembly, , . , , . - , , . , , masm32rt.inc, , MASM32. IDE , , MASM32.

masm32\include masm32rt.inc. , masm32rt.inc , - ​​.

You start your source file with:
.586
.option casemap:NONE
.model flat, stdcall

include yourincludeshere and it could be a bunch
includelib yourlibshere same here a bunch

masm32rt.inc include includeselib libs , . , .

\masm32\include, protos API, invoke , API, WriteConsoleA WriteConsole hell, YoConsole equ, YoConsole WriteConsole.

, - extern _WriteConsoleA @20: NASM MASM, :

.code
yourentrypointname:

end yourentrypointname

libs , includeelib .

, , , . !! Windows DEFINES . (, ) , -11 API, , , . , , , WriteConsole 40 ? equate, equate . , MASM- Iczelion, , , .

+2

:

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

- _go, , - go -/entry: , , ! , ... ?

+2

You can try MASM32 , here is a welcome example for a console application:

; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««« *

    .486
    .model flat, stdcall
    option casemap :none   ; case sensitive

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    include \masm32\include\windows.inc
    include \masm32\include\masm32.inc
    include \masm32\include\kernel32.inc
    include \masm32\macros\macros.asm

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

    .code

start:

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    print "Hello world"

    exit

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

end start

But if you want to stick with your current assembler, we can look under the macro. Mac has a print macro:

print MACRO arg1:REQ,varname:VARARG      ;; display zero terminated string
    invoke StdOut,reparg(arg1)
  IFNB <varname>
    invoke StdOut,chr$(varname)
  ENDIF
ENDM

So you want StdOut, in MASM32 it looks like this:

StdOut proc lpszText:DWORD

    LOCAL hOutPut  :DWORD
    LOCAL bWritten :DWORD
    LOCAL sl       :DWORD

    invoke GetStdHandle,STD_OUTPUT_HANDLE
    mov hOutPut, eax

    invoke StrLen,lpszText
    mov sl, eax

    invoke WriteFile,hOutPut,lpszText,sl,ADDR bWritten,NULL

    mov eax, bWritten
    ret

StdOut endp

So, at the end of this journey, you need to use WriteFile not WriteConsole :)

0
source

All Articles