从Masm中的Dump读取字节

时间:2015-06-08 19:46:28

标签: assembly masm32

在我的应用程序的一部分中,我需要读取一个字节

invoke GlobalAlloc,GMEM_MOVEABLE or GMEM_ZEROINIT, eax; eax = size   
mov  hMemory,eax
invoke GlobalLock,hMemory
mov  pMemory,eax   

"如何移动" eM的前4个字节的pMemory?然后将接下来的2个字节移动到ebx ??

示例:

1A5288 .... 12 34 56 78 12 34

所以

eax = 12345678
ebx = 00001234

1 个答案:

答案 0 :(得分:2)

根据您的示例,您正在使用“小端”字节顺序进行操作。字节序列12 34 56 78作为78563412加载到32位寄存器中。您可以使用BSWAP反转32/64位寄存器中的字节顺序。在第二种情况下,您需要反转32位寄存器的低两个字节中的字节顺序。以下是XCHGROR 8 | ROL 8相应的说明。

示例:

include \masm32\include\masm32rt.inc

.data
    hMemory DWORD ?
    pMemory DWORD ?
    myMemory DB 12h, 34h, 56h, 78h, 12h, 34h

.code
main PROC
    mov eax, 32
    invoke GlobalAlloc,GMEM_MOVEABLE or GMEM_ZEROINIT, eax; eax = size
    mov  hMemory,eax
    invoke GlobalLock,hMemory
    mov  pMemory,eax

    mov ecx, LENGTHOF myMemory
    lea esi, myMemory
    mov edi, pMemory
    rep movsb                       ; [pMemory] = 12 34 56 78 12 34

    mov esi, pMemory
    mov eax, [esi]                  ; eax = 78563412
    bswap eax                       ; eax = 12345678

    movzx ebx, word ptr [esi+4]     ; ebx = 00003412
    xchg bh, bl                     ; ebx = 00001234
  ; ror bx, 8                       ; equivalent to `xchg bh, bl`

    printf ("eax = %08X\nebx = %08X\n", eax, ebx)

    xor eax, eax                    ; Return 0
    ret
main ENDP

END main