EAX寄存器的反向字节顺序

idi*_*ish 3 x86 assembly masm endianness masm32

示例:0xAABBCCDD将变为0xDDCCBBAA

由于第一次XOR操作中的访问冲突异常,我的程序崩溃了.

似乎有一个更好的天真解决方案,使用移位或旋转,但无论如何,这里是代码:

  ;; #########################################################################

      .486
      .model flat, stdcall
      option casemap :none   ; case sensitive

;; #########################################################################

      include \masm32\include\masm32.inc
      include \masm32\include\kernel32.inc

      includelib \masm32\lib\kernel32.lib
    includelib \masm32\lib\masm32.lib


.code
;; The following program will flip the sequence of the bytes in the eax
;; example : 0xAABBCCDD will turn into 0xDDCCBBAA
start:
MOV eax, 0AABBCCDDh 
XOR BYTE PTR [eax], al ;; Swap first byte and last byte
XOR al, BYTE PTR [eax]
XOR BYTE PTR [eax], al 
XOR BYTE PTR [eax+1], ah ;; Swap 2nd byte of eax and 3rd byte
XOR ah, BYTE PTR [eax+1]
XOR BYTE PTR [eax+1], ah
end_prog:
    ;;Exit the program, eax is the exit code
    push eax
    call ExitProcess
END start
Run Code Online (Sandbox Code Playgroud)

我在这做错了什么?有没有更好的解决方案呢?

joh*_*und 15

为什么不简单:

 mov  eax, 0AABBCCDDh
 bswap eax
Run Code Online (Sandbox Code Playgroud)

我不确定你在程序中尝试做什么,但可以说出CPU实际上尝试做什么(但不能,这就是为什么崩溃):

这个:

XOR BYTE PTR [eax], al 
Run Code Online (Sandbox Code Playgroud)

尝试计算寄存器AL(字节大小)中的值的xor运算和地址0AABBCCDDh(EAX寄存器的内容)的存储器中的字节值.只要在该地址上没有OS分配任何内存,程序就会崩溃.

不使用bswap进行正确的字节交换如下(感谢XJ):

    xchg  ah, al
    ror   eax, 16
    xchg  ah, al.
Run Code Online (Sandbox Code Playgroud)