首页 > 解决方案 > 如何在 Python 中模拟 XMM 寄存器行为

问题描述

如何实现与使用 128 位无符号整数汇编寄存器相同的行为。

例如右移时,最高有效位将被清零,或者当您左移时,总大小不会扩大,将继续保持 128 位。Numpy 只上升到 64 位整数。

这是一个实际的例子:

int128bits = 0x00000001000000010000000100000001
int128bits = int128bits << 0xff

期待:

int128bits = 0x00000000000000000000000000000000

现实:

int128bits = 0x8000000080000000800000008000000000000000000000000000000000000000000000000000000000000000

标签: python

解决方案


我用 PeachPy 偶然发现的一个解决方案

from peachpy import *
from peachpy.x86_64 import *

def decrypt(dword1):
        #Assembler instructions to Transform
        x = Argument(uint64_t)
        with Function("DotProduct", (x,), uint64_t) as asm_function:
            #Load var and Shift right
            LOAD.ARGUMENT(rax, x)
            MOVQ(xmm2, rax)
            MOVLHPS(xmm2, xmm2)
            PSRLDQ(xmm2, 0xf)
            
            MOVQ(rax, xmm1)
            RETURN(rax)

        #Read Assembler Return
        python_function = asm_function.finalize(abi.detect()).encode().load()
        return python_function(dword1)
        
print(decrypt(0x0000000100000001))

推荐阅读