math - 如何在程序集8086中找到位数?

标签 math assembly x86 x86-16 digits

我是一个新的汇编程序员,我无法成功找到一个数字有多少位数字。我的目的是找到阶乘。
我在程序集 8086 的模拟器中编程。

最佳答案

执行此操作的最有效方法是使用 bsr指令(请参阅此 slides ,20 到 25)。

这应该使代码如下:

    .text
    .globl  main
    .type   main, @function
main:
    movl    $1024, %eax ;; pushing the integer (1024) to analyze
    bsrl    %eax, %eax  ;; bit scan reverse (give the smallest non zero index)
    inc     %eax        ;; taking the 0th index into account

但是,我想您需要基数为 10 的日志而不是基数为 2 的...所以,代码如下:
    .text
    .globl  main
    .type   main, @function
main:
    movl    $1024, %eax ;; pushing the integer (1024) to analyze
    bsrl    %eax, %eax  ;; bit scan reverse (give the smallest non zero index)
    inc     %eax        ;; taking the 0th index into account

    pushl   %eax        ;; saving the previous result on the stack

    fildl   (%esp)      ;; loading the previous result to the FPU stack (st(0))
    fldlg2              ;; loading log10(2) on the FPU stack
    fmulp   %st, %st(1) ;; multiplying %st(0) and %st(1) and storing result in %st(0)

    ;; We need to set the FPU control word to 'round-up' (and not 'round-down')
    fstcw  -2(%esp)      ;; saving the old FPU control word
    movw   -2(%esp), %ax ;; storing the FPU control word in %ax
    andw   $0xf3ff, %ax  ;; removing everything else
    orw    $0x0800, %ax  ;; setting the proper bit to '1'
    movw   %ax, -4(%esp) ;; getting the value back to memory
    fldcw  -4(%esp)      ;; setting the FPU control word to the proper value

    frndint              ;; rounding-up

    fldcw  -2(%esp)      ;; restoring the old FPU control word

    fistpl (%esp)        ;; loading the final result to the stack
    popl   %eax          ;; setting the return value to be our result

    leave
    ret

我很想知道是否有人能找到比这更好的!事实上,使用 SSE 指令可能会有所帮助。

关于math - 如何在程序集8086中找到位数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16135651/

相关文章:

assembly - 不知道为什么我们在汇编代码使用 %eax 和 %edx 时添加寄存器 %rdx 和 %rax

assembly - x64 fastcall 调用者堆栈管理

assembly - 编译汇编代码

android - 使用 houdini(Android 模拟器)在基于 x86 的 AVD 上运行 ARM 库

x86 - SSE 比较打包的无符号字节

python - 使用 Lambdas 从字符串表达式构建可执行函数

c - 是否有 CIEDE2000 或 CIE94 Delta-E 色差计算算法的已知实现?

algorithm - 在等距六边形平铺引擎中转换屏幕到 map 坐标?

c++ - 反转 __m512i 寄存器中的值

c++ - C++ 中的二分法