我正在使用Z3
来处理一些汇编程序分析任务。我被困在模拟x86
操作码bsf
的语义。在Z3中模拟x86操作码'bsf'的语义
bsf operand1 operand2
的语义定义为在源操作数(操作数1)中搜索最低有效集位(1位)。
其语义可以C
模拟为:
if(operand1 == 0) {
ZF = 0;
operand2 = Undefined;
}
else {
ZF = 0;
Temporary = 0;
while(Bit(operand1, Temporary) == 0) {
Temporary = Temporary + 1;
operand2 = Temporary;
}
}
现在,假设每个操作数(例如,注册)保持symbolic expression
,我试图模拟上述语义Z3Py
。我写的代码是这样的(简化):
def aux_bsf(x): # x is operand1
if simplify(x == 0):
raise Exception("undefined in aux_bsf")
else:
n = x.size()
for i in range(n):
b = Extract(i, i, x)
if simplify(b == 1):
return BitVecVal(i, 32)
raise Exception("undefined in bsf")
不过,我发现的simplify(x==0)
评价(例如,x
等于BitVecVal(13, 32) + BitVec("symbol1", 32)
)总是等于True
。换句话说,我总是被困在第一个例外!
我在这里做错了什么..?
============================================== ====== OK,所以我想我需要的是这样的:
def aux_bsf(x):
def aux(x, i):
if i == 31:
return 31
else:
return If(Extract(i, i, x) == 1, i, aux(x, i+1))
return aux(x, 0)
我在这个问题的一些新的代码更新。 – computereasy