Reverse byte order of EAX register

蹲街弑〆低调 提交于 2019-11-29 12:08:45
johnfound

Why not simply:

 mov  eax, 0AABBCCDDh
 bswap eax

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):

This one:

XOR BYTE PTR [eax], al 

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.

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

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

How 'bout...

    mov eax, 0AABBCCDDh
    xchg al, ah ; 0AABBDDCCh
    rol eax, 16 ; 0DDCCAABBh
    xchg al, ah ; 0DDCCBBAAh

Would that not do what is wanted in one register? I see X.J has already posted that (rotate left, rotate right - same result) Gotta be quick to beat you guys! :)

An alternative solution, using the rol instruction only:

mov eax,0xAABBCCDDh
rol ax,8            ; 0AABBDDCCh
rol eax,16          ; 0DDCCAABBh
rol ax,8            ; 0DDCCBBAAh

I believe, in most cases, this will be ever so slightly faster than using the xchg instruction, although I see no reason not to simply use bswap, which is cleaner and likely faster.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!