windows ReadFile后取消引用缓冲区(MASM)

jxct1oxe  于 2023-06-24  发布在  Windows
关注(0)|答案(1)|浏览(186)

我有一个原始的程序来读取一个4字节的文件(0x00000060或96)。为了简单起见,这个文件是4字节长的,我一开始不知道它的长度。我不知道如何解引用缓冲区和操作它的值。

include \masm32\include\masm32rt.inc
include \masm32\macros\macros.asm

.CONST
FilePath DB "file", 0

.DATA?
hFile DWORD ?
dwFileSize DWORD ?
dwBytesRead DWORD ?
dwHighSize DWORD ?
mem DWORD ?
buffer DWORD ?
bufferData DWORD ?

.CODE
start:
    invoke CreateFile, ADDR FilePath, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, 0
    mov hFile, eax
    
    invoke GetFileSize, hFile, ADDR dwHighSize
    mov dwFileSize, eax
    invoke GlobalAlloc, GMEM_MOVEABLE or GMEM_ZEROINIT, dwFileSize
    mov mem, eax
    invoke GlobalLock, mem
    mov buffer, eax
    
    invoke ReadFile, hFile, buffer, dwFileSize, ADDR dwBytesRead, 0
    invoke CloseHandle, hFile
    
    mov ebx, DWORD PTR buffer   ; Don't know what to do here
    ; Printing str$(ebx) would just result in a random value (same as mov ebx, buffer, so, the address),
    ; mov ebx, OFFSET buffer would print a fixed value
    
    invoke GlobalUnlock, buffer
    invoke GlobalFree, mem
end start

如果4个字节包含可显示字符(例如0x31323334 ==“1234”),将 buffer 传递给 StdOut 或以任何其他方式输出都可以很好地打印它们,所以我知道程序正确地读取了文件。如何将读取的值作为4字节整数放入ebx或DWORD变量中?

t3psigkw

t3psigkw1#

mov ebx, buffer
mov eax, DWORD PTR [ebx]

做了我想要的,打印str$(eax)sdword$(eax)将输出1610612736,十六进制是0x60000000,所以我需要的只是交换字节序。

相关问题