x86 汇编:WriteConsole 工作时出现问题,返回 0

问题描述 投票:0回答:1

我是 x86 汇编的初学者,我刚刚拿到我的代码来第一次实际汇编。

我正在制作一个计算器程序来学习,我需要打印到控制台以提示用户输入。我不是问如何请求输入,只是问为什么我的代码不写入控制台。

下面是我的代码。

我尝试从堆栈中内联传递参数,改变引用常量的方式。我一定是缺少一些信息。

对我可能错过或掩盖的相关文档也非常开放。

非常感谢:)

;------------------------------------------------------------------------------------------------
;  Calculator in x86
;------------------------------------------------------------------------------------------------

; Assembler Directives

.386                                            ; Full 80386 instruction set and mode
.model flat, stdcall                            ; All 32-bit and later apps are flat. Used ot include "tiny, etc"
option casemap:none                             ; Preserve the case of system identifiers but not our own, more or less

; Include files - headers and libs that we need for calling the system dlls like user32, gdi32, kernel32, etc

include D:\masm32\include\windows.inc           ; Main windows header file
include D:\masm32\include\user32.inc            ; Windows, control, etc
include D:\masm32\include\kernel32.inc          ; Handles, modules, paths, etc
include D:\masm32\include\gdi32.inc             ; Drawing into a device context (i.e. painting)

; Libs 0information needed to link our bainary to the system DLL calls

includelib D:\masm32\lib\kernel32.lib           ; Kernel32.dll
includelib D:\masm32\lib\user32.lib             ; User32.dll
includelib D:\masm32\lib\gdi32.lib              ; GDI32.dll

; Forward declarations - Our main entry point will call forward to WinMain


; Constants Data Aliases Prototypes

WriteWrapper    PROTO,                              ; PRototype declaration of WriteWrapper
            :PTR BYTE, 
            :DWORD

.DATA
    
    bytesWBuf   DWORD   ?                           ; Buffer for lpNumberOfCharsWritten

    promptWelc  DB      "Welcome to the worst assembly calculator ever!", 13, 10
    promptWelcL EQU     $-promptWelc

    exitCode    DWORD   0                          ;Init to 0. Program jumps to ExitMain if not 0 and return exitCode

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.CODE                                           ; Here is where the pgoram itself lives
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; PROTO functions [
WriteWrapper PROC, msg:PTR BYTE, msgLen:DWORD       ; WriteConsole helper. rebuilding the wheel ik

    push        STD_OUTPUT_HANDLE                   ; Getting handle and pushing it to eax
    call        GetStdHandle

    cmp         eax, -1                             ; Checking if handle is invalid
    je          b_InvalidHandle

    push        0               ;lpReserved
    push        offset bytesWBuf    ;lpNumberOfCharsWritten
    push        msgLen          ;nNumberOfCharsToWrite
    push        msg             ;*lpBuffer
    push        eax             ;hConsoleOutput
    call        WriteConsole    ;Invocation

    cmp         eax, 0                              ; Checking WriteConsole res, has been returning 0
    je          b_InvalidWriteConsoleResult

    jmp         ExitWriteWrapper

b_InvalidHandle:
    mov         exitCode, 2
    jmp         ExitWriteWrapper

b_InvalidWriteConsoleResult:
    mov         exitCode, 3
    jmp         ExitWriteWrapper
    
    
ExitWriteWrapper:
    ret

WriteWrapper ENDP


main PROC
; Initialize Out Handle

    INVOKE      WriteWrapper, offset promptWelc, promptWelcL   ;Call to WriteWrapper
    cmp         exitCode, 0                                    ;Checking exitcode for exceptions
    jne         ExitMain                                       ;Has been returning 3, which is b_InvalidConsoleResult
    jmp         ExitMain

; Exit 
ExitMain:
    push exitCode
    call ExitProcess

main ENDP

END main






assembly x86 console masm masm32
1个回答
0
投票

我是个傻瓜。我正在将我的子系统设置为窗口而不是控制台进行组装

如果您希望它起作用,请确保使用类似的模板进行编译

...\ml.exe /link /subsystem:console source.asm ...

© www.soinside.com 2019 - 2024. All rights reserved.