我是一个新的汇编程序员,我无法成功找到一个数字有多少位。我的目的是找到阶乘。我在程序集 8086 的模拟器中编程。
问问题
1945 次
1 回答
2
执行此操作的最有效方法是使用bsr
指令(请参阅此幻灯片,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 指令可能会有所帮助。
于 2013-04-22T07:46:20.407 回答