Я новый программист сборки, и мне не удалось найти количество цифр, число которых имеет. Моя цель - найти факториалы. Я программирую в эмуляторе сборки 8086.Как найти количество цифр в сборке 8086?
4
A
ответ
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 может помочь.
есть инструкция, которая может сделать логарифм base10 номера? –
Взгляните на это для теории: http://stackoverflow.com/q/6655754/1353098. На практике я не думаю, что есть инструкция для log10. У вас есть доступ к любым библиотекам, которые могут открыть эту функцию (например, библиотеке c или библиотеке математики). – Will
Здесь x87 может пригодиться. Журналы - это вещи, которые, скорее всего, имеют процессоры с плавающей запятой. Я считаю, что 'FYL2X' может быть инструкцией для вас. – Will