【问题标题】:Nasm x86 Check for UNDERflowNasm x86 检查下溢
【发布时间】:2021-02-15 05:12:37
【问题描述】:

考虑 16 位架构:

在下面的代码中,

测试.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流。 joadcsetc(进位标志正常)都没有用。

如果不可能,那么只有当 ecx 为负时才将其转换为 0 的汇编代码

【问题讨论】:

  • dec ecx 如果产生否定结果,则设置 SF; (从减少 ECX 中的旧垃圾并合并新的低字节......)您可以将其与 cmovsjs 一起使用。 (或cmovl / jl)。
  • 非常感谢彼得,js 成功了。你可能想回答
  • 正如我评论您的代码高尔夫球链接时,dec ecx 根据整个 ECX 寄存器设置 SF,但mov cl, al 仅将一个新的低字节合并到任何现有字节中。此外,“7 的倍数”,而不是“幂”,余数 = 0。
  • 我将在本地重新编译文件以确保字节码并查看您的建议并进行相应的编辑,非常感谢!

标签: assembly nasm x86-16


【解决方案1】:

感谢@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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-24
    • 1970-01-01
    • 2016-08-15
    • 2011-07-14
    • 1970-01-01
    • 2011-12-29
    • 1970-01-01
    • 2018-02-04
    相关资源
    最近更新 更多