【问题标题】:WriteConsole returns false in MASMWriteConsole 在 MASM 中返回 false
【发布时间】:2020-09-19 06:56:04
【问题描述】:

我正在尝试创建一个数组,并访问它的值并将它们打印出来

调用 WriteConsole 子例程后,它返回 false,但是,提供了所有值。在这里我们可以看到 - https://imgur.com/a/vUfwOo6 调用 WriteConsole 后 Eax 寄存器为 0。在这里,您可以看到被推送到堆栈的寄存器值。 https://imgur.com/a/gv6s4uG 考虑到 WriteConsole 是 WINAPI 子例程,这意味着它是 stdcall。所以,我从右到左传递值。

lpReserved -> 0
lpNumberOfCharsWritten -> offset to 00403028 (CharsWritten variable)
nNumberOfCharsToWrite -> Just 2, because in array only ints are present of length 2
*lpBuffer -> ebx register, which contains array lvalue
hConsoleOutput -> Output from GetStdHandle (In this case -> edx register -> A0)

我的 MASM 代码:

.386
.model flat,stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib
include \masm32\include\user32.inc
include C:\masm32\include\masm32.inc
includelib C:\masm32\lib\masm32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\msvcrt.lib ; Some default includes :P

.data

dArray              dd  10 dup (?)      ; Main array
CharsWritten        dd ?
LoopCounter         dd 0
StdHandle           dd ?
.code


PrintArrayToScreen proc

mov eax, STD_OUTPUT_HANDLE
push eax
call GetStdHandle

mov StdHandle,eax
mov eax,[LoopCounter]
innerPrintLoop:
mov ecx,offset dArray
mov eax, [LoopCounter]
mov ebx,[ecx + eax * 4]
mov esi,offset CharsWritten

push 0
push esi
push 2
push ebx
mov edx,StdHandle
push edx

call WriteConsole

mov eax,[LoopCounter]
inc eax
mov LoopCounter,eax ; Storing the Loop Counter in the variable
cmp eax,11  ; +1 because of loop counter increment
jnz innerPrintLoop
ret
PrintArrayToScreen endp

arrayLoop proc          ; Subroutine for the array filling
mov eax,offset dArray
mov bx,10
mov ecx,0
innerLoop:
mov [eax + ecx * 4],bx  ; ecx * 4 => counter * 4 bytes
inc bx
add ecx,1
cmp ecx,10
jne innerLoop
mov eax,offset dArray
ret
arrayLoop endp

start:
call arrayLoop
call PrintArrayToScreen
mov eax,0
push eax
call ExitProcess
end start

【问题讨论】:

    标签: windows assembly masm


    【解决方案1】:

    来自the documentationWriteConsole

    lpBuffer [in]
    指向包含要写入控制台屏幕缓冲区的字符的缓冲区的指针。

    所以你应该传递要写入的数据的地址,但实际上你传递的是数据本身。

    您可以通过将行 mov ebx,[ecx + eax * 4] 更改为 lea ebx,[ecx + eax * 4] 来“修复”该问题。但请注意,WriteConsole 不会为您进行任何整数到字符串的转换,因此您可能仍然无法获得预期的结果。如果你想要那种功能,请使用printf

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-27
      • 2013-10-12
      • 2012-12-31
      • 2015-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多