EAX寄存器的反向字节顺序 [英] Reverse byte order of EAX register

查看:108
本文介绍了EAX寄存器的反向字节顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例:0xAABBCCDD将变成0xDDCCBBAA

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

My program crashes, due to Access Violation exception right in the first XOR operation.

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

It seems like there's a better naive solution, using shifting or rotating, but anyways, here's the code:

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

      .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

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

What am I doing wrong here? Is there any better solution for this?

推荐答案

为什么不简单:

 mov  eax, 0AABBCCDDh
 bswap eax

我不确定您要在程序中尝试做什么,但是可以说出CPU实际试图做什么(但是不能,这就是崩溃的原因):

I am not sure what you are trying to do in your program, but can say what the CPU actually tries to do (but can't and that is why crashes):

这个:

XOR BYTE PTR [eax], al 

尝试计算寄存器AL(字节大小)中的值与存储器中地址0AABBCCDDh(EAX寄存器的内容)中字节的值的异或运算.只要在该地址上没有操作系统分配的任何内存,程序就会因GPF而崩溃.

Tries to compute an xor operation of the value in the register AL (byte sized) and a value of the byte in memory at address 0AABBCCDDh (the content of EAX register). As long as on this address there is no any memory allocated by the OS, the program crashes with GPF.

以下不使用bswap的正确字节交换(感谢 X.J ):

The proper byte swapping without using bswap is the following (Thanks to X.J):

    xchg  ah, al
    ror   eax, 16
    xchg  ah, al.

这篇关于EAX寄存器的反向字节顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆