windowswinapimasmdereference

Dereference buffer after ReadFile (MASM)


I have a crude program to read a 4-byte file (0x00000060 or 96). The file is 4-byte long for the sake of simplicity, I don't know its length at the start. I can't figure out how to dereference the buffer and operate its value.

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

If the 4 bytes contained displayable characters (e.g. 0x31323334 == "1234"), passing buffer to StdOut or outputting it in any other way would print them fine, so I know that the program read the file correctly. How do I get the read value as a 4-byte integer into ebx or a DWORD variable?


Solution

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

    did what I wanted and printing str$(eax) or sdword$(eax) would output 1610612736, which in hex is 0x60000000, so all I need is to swap endian.