簡體   English   中英

x86程序集 - 如何使用Windows API _WriteConsole @ 4 - masm32語法

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

結果我的帖子我可以在windows xp上使用int21h來打印東西嗎? ,我看過一篇關於使用Windows API的文章,在本文中引用了使用_WriteConsole @ 4 API將消息打印到控制台。 該文章位於http://cs.lmu.edu/~ray/notes/x86assembly/

到目前為止,這是我的代碼:

.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

我使用這種語法來編譯代碼:ML:

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

鏈接:

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

我已經得到它來編譯和鏈接,但是當我運行生成的.exe時,它什么都沒做,甚至沒有錯誤返回。 控制台只是黑色。 為什么是這樣?

任何幫助將不勝感激。 對於這個論壇的用戶,我為每天轟炸stackoverflow.com道歉,這只是我很少有資源可以學習。

提前致謝,

Progrmr

這沒有問題:

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:go所以它創建控制台但不執行任何代碼! 在這種情況下,您不需要告訴鏈接器入口點,您的入口點就是開始...鏈接器如何知道? 結束了

首先,您獲取示例代碼的鏈接來自NASM用戶,他可能從未使用MASM,就像他說的那樣。 他還像NASM格式一樣寫了他的MASM樣本。 您想要使用Assembly的事實意味着您必須是高級計算機用戶。 您需要知道如何使用批處理文件,如何設置系統路徑和其他事項。 當出現問題時,你會通過研究來學習。 所以你得到一個錯誤,說它找不到masm32rt.inc,但你說你正在使用MASM32。 我使用批處理文件和IDE進行匯編,我的系統路徑指向MASM32中的各種目錄。

在masm32rt.inc之前添加masm32 \\ include目錄的絕對路徑。 當你在它時,在文本編輯器中打開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已經包含了它,它包含libs的include和includelib以及廣泛使用的包含。 我們用它來節省一堆打字。

現在打開\\ masm32 \\ include中的任何包含文件include只是為API調用設置原型,因此你可以使用invoke進行參數檢查,它也可以對API調用進行別名,所以我們不必輸入WriteConsoleA而只是寫WriteConsole地獄,你甚至可以做YoConsole equ,你可以在你的代碼中為WriteConsole寫YoConsole。

話雖這么說,我們不使用 - extern _WriteConsoleA @ 20:附近因為NASM也不需要將我們的條目標簽設置為公共MASM知道你的入口點:

.code
yourentrypointname:

end yourentrypointname

我們也不必為鏈接器指定庫,因為我們在源代碼中使用了includelib。

另外,不要,我再說一遍,不要養成在參數中使用硬編碼數字的習慣。 現在擺脫那種習慣!! Windows頭文件使用DEFINES是有原因的 - 代碼可讀性,我們(幫助你的人)不需要查找-11對於該API調用的含義,我使用了define並且您知道該參數的含義。 如果你有4次WriteConsole調用怎么辦? 如果使用equate,則需要僅更改equate而不是search and replace。 如果你想要體面的MASM教程搜索Iczelion它們已經很老了並且包含一些錯誤但是它們會讓你開始,我們很多人在早期都使用過那些教程。

您可以嘗試使用MASM32 ,這是控制台應用程序的hello world示例:

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

    .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

但是如果你想堅持使用當前的匯編程序,我們可以在macros.asm下看一下打印宏:

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

所以你想要StdOut,在MASM32中它看起來像這樣:

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

所以在這個旅程結束時,你必須使用WriteFile而不是WriteConsole :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM