2013-04-21 64 views
4

我是一個新的程序集編程人員,我無法找到一個數字有多少個數字。我的目的是查找階乘。 我在組件的仿真器8086如何查找程序集8086中的位數?

+1

有沒有可以做一個base10對數的指令? – 2013-04-21 20:09:50

+0

看看這個理論:http://stackoverflow.com/q/6655754/1353098。在實踐中,我不認爲有log10指令。你有權訪問任何可能暴露此函數的庫(如c庫或數學庫) – Will 2013-04-21 20:14:45

+0

儘管x87可能派上用場。日誌是浮點處理器更可能擁有的東西。我認爲'FYL2X'可能是你的指令。 – Will 2013-04-21 20:19:12

回答

1

執行此操作的最有效方式是使用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指令可能會有所幫助。