Nasm x86 检查下溢
Nasm x86 Check for UNDERflow
考虑 16 位架构:
在下面的代码中,
test.s
bits 16
section .text
test:
aam 7 ; divide n by 7
mov cl, al ; cl = n % 7
dec ecx ; <--- here if n was power of 7 we get the underflow
; ... more things where I make use of ah = n / 7
add eax, ecx ; return that number
ret
我想知道如何检查 under 流程。 jo
、adc
和 setc
(进位标志正常)都没有用。
如果不可能,则仅当 ecx 为负时才将其转换为 0 的汇编代码
感谢@PeterCordes 解决了这个问题,帮助我为 code golf 创造了答案:
0: d4 07 aam 0x7 ; eax contains n, with aam 0x7 -> al = n % 7 and ah = n / 7
2: 88 c1 mov cl,al ; keep result of n%7 in ecx
4: 66 c1 e8 08 shr ax,0x8 ; by shifting 8 times ax, ax=al=n/7;
8: 66 6b c0 05 imul ax,ax,0x5 ; we multiply by 5
c: 49 dec ecx ; ecx = ecx - 1 -> give 65535 if n was power of 7
d: 78 02 js 11 <end> ; if it happened we don't care about the remainder so we can just return n/7*5, the Signed Flag will be set so we can jump 2 bytes forward.
f: 01 c8 add eax,ecx ; if not then we add (n%7)-1 to n/7*5
00000011 <end>:
11: c3 ret
考虑 16 位架构:
在下面的代码中,
test.s
bits 16
section .text
test:
aam 7 ; divide n by 7
mov cl, al ; cl = n % 7
dec ecx ; <--- here if n was power of 7 we get the underflow
; ... more things where I make use of ah = n / 7
add eax, ecx ; return that number
ret
我想知道如何检查 under 流程。 jo
、adc
和 setc
(进位标志正常)都没有用。
如果不可能,则仅当 ecx 为负时才将其转换为 0 的汇编代码
感谢@PeterCordes 解决了这个问题,帮助我为 code golf 创造了答案:
0: d4 07 aam 0x7 ; eax contains n, with aam 0x7 -> al = n % 7 and ah = n / 7
2: 88 c1 mov cl,al ; keep result of n%7 in ecx
4: 66 c1 e8 08 shr ax,0x8 ; by shifting 8 times ax, ax=al=n/7;
8: 66 6b c0 05 imul ax,ax,0x5 ; we multiply by 5
c: 49 dec ecx ; ecx = ecx - 1 -> give 65535 if n was power of 7
d: 78 02 js 11 <end> ; if it happened we don't care about the remainder so we can just return n/7*5, the Signed Flag will be set so we can jump 2 bytes forward.
f: 01 c8 add eax,ecx ; if not then we add (n%7)-1 to n/7*5
00000011 <end>:
11: c3 ret