如何将 "or" 转换为 SPARC 程序集?
How to convert "or" to SPARC assembly?
我正在尝试将以下行转换为 SPARC 程序集(使用负逻辑):
if (i < a || i > b) {...}
但我一直在研究如何将 "or" 转换为按位或在 SPARC 中。而且我找不到有用的文档来转换它。有人可以帮忙吗?谢谢。
(假设i
在$l0
,a
在$l1
,b
在$l2
)
编辑:
这是我尝试过的:
cmp %l0, %l1 ! compare i with a
bge end_if ! negative logic test to end if statement
nop
cmp %l0, %l2 ! compare i with b
ble end_if ! negative logic test to end if statement
nop
if:
/*do something*/
end_if:
/*statements*/
更具体的问题是如何在两个cmp
指令之间使用"or"?使用负逻辑时,这让我感到非常困惑。
您的代码:
cmp %l0, %l1 ! compare i with a
bge end_if ! negative logic test to end if statement
nop
cmp %l0, %l2 ! compare i with b
ble end_if ! negative logic test to end if statement
nop
if:
/*do something*/
end_if:
/*statements*/
是一个与。两个 sub-conditions 都必须满足,才能达到 "do something" 代码。
(一些布尔逻辑可能有助于解释:!(a ^ b) = !a v !b
注意:组合操作发生变化!)
对于 OR,您必须使用不同的逻辑:
test condition1
jump_if_true doit
test condition2
jmp_if_true doit ; *1) this branch and
jmp dont_doit ; this jmp can be optimized
do_it:
/* do if-part */
jmp endif
dont_do_it
/* do else-part */
endif:
现在您可以将最后 2 次跳转优化为一次
test condition1
jump_if_true doit
test condition2
jmp_if_not_true dont_doit ; inverted to remove the 2nd jump
doit:
/* do if-part */
jmp endif
dont_doit
/* do else-part */
endif:
您可以轻松检查 if (cond1 || cond2) { ... }
是否等同于
if (cond1) goto if_code
if (cond2) goto if_code
goto other_code
if_code:
...
other_code:
并且 if (cond1 && cond2) { ... }
等同于
if ( ! cond1) goto other_code
if ( ! cond2) goto other_code
if_code:
...
other_code:
您的实现将第二个模式与 cond1 := i < a
和 cond2 := i > b
相匹配(注意否定)。
所以它相当于if (i < a && i > b){ ... }
。
重点是在析取 cond1 || cond2
中,如果 cond1
为假,并不意味着析取为假,其他条件可以为真。
析取和合取运算符的absorption law体现在跳转方面的实现上。
在我的代码中,我未指定评估的 short-circuit 方面。可以提前评估条件(无 short-circuit)或仅在需要时评估(short-circuit)。
我正在尝试将以下行转换为 SPARC 程序集(使用负逻辑):
if (i < a || i > b) {...}
但我一直在研究如何将 "or" 转换为按位或在 SPARC 中。而且我找不到有用的文档来转换它。有人可以帮忙吗?谢谢。
(假设i
在$l0
,a
在$l1
,b
在$l2
)
编辑:
这是我尝试过的:
cmp %l0, %l1 ! compare i with a
bge end_if ! negative logic test to end if statement
nop
cmp %l0, %l2 ! compare i with b
ble end_if ! negative logic test to end if statement
nop
if:
/*do something*/
end_if:
/*statements*/
更具体的问题是如何在两个cmp
指令之间使用"or"?使用负逻辑时,这让我感到非常困惑。
您的代码:
cmp %l0, %l1 ! compare i with a
bge end_if ! negative logic test to end if statement
nop
cmp %l0, %l2 ! compare i with b
ble end_if ! negative logic test to end if statement
nop
if:
/*do something*/
end_if:
/*statements*/
是一个与。两个 sub-conditions 都必须满足,才能达到 "do something" 代码。
(一些布尔逻辑可能有助于解释:!(a ^ b) = !a v !b
注意:组合操作发生变化!)
对于 OR,您必须使用不同的逻辑:
test condition1
jump_if_true doit
test condition2
jmp_if_true doit ; *1) this branch and
jmp dont_doit ; this jmp can be optimized
do_it:
/* do if-part */
jmp endif
dont_do_it
/* do else-part */
endif:
现在您可以将最后 2 次跳转优化为一次
test condition1
jump_if_true doit
test condition2
jmp_if_not_true dont_doit ; inverted to remove the 2nd jump
doit:
/* do if-part */
jmp endif
dont_doit
/* do else-part */
endif:
您可以轻松检查 if (cond1 || cond2) { ... }
是否等同于
if (cond1) goto if_code
if (cond2) goto if_code
goto other_code
if_code:
...
other_code:
并且 if (cond1 && cond2) { ... }
等同于
if ( ! cond1) goto other_code
if ( ! cond2) goto other_code
if_code:
...
other_code:
您的实现将第二个模式与 cond1 := i < a
和 cond2 := i > b
相匹配(注意否定)。
所以它相当于if (i < a && i > b){ ... }
。
重点是在析取 cond1 || cond2
中,如果 cond1
为假,并不意味着析取为假,其他条件可以为真。
析取和合取运算符的absorption law体现在跳转方面的实现上。
在我的代码中,我未指定评估的 short-circuit 方面。可以提前评估条件(无 short-circuit)或仅在需要时评估(short-circuit)。