有人能向我解释一下是否有办法获得一个十进制值(0-9(吗?我正在做的一个项目需要它。我想做MOV SI, [CX:DX]
,但后来我有一种行不通的感觉。所以我决定在网上搜索,但我想要的主题不会出现。那么有人能告诉我是否有办法从0到9得到1个十进制值吗?
我尝试过:MOV BL, 1000
MOV AH, 00h
DIV DX
我做错什么了吗?
使用带有16位代码的编译器NASM。
从评论来看,你想要的似乎是:
;Input
; cx Clock ticks since midnight
;
;Output
; ax Most significant non-zero decimal digit of clocks since midnight (unless
; clocks since midnight is zero, where zero will be returned)
;
;Trashed
; none (contents of all registers not used for outputs are preserved)
get_most_significant_decimal_digit:
mov ax,cx ;ax = numerator = clocks since midnight
cmp ax,10 ;Is it already small enough?
jb .done ; yes
push bx
push dx
mov bx,10 ;bx = divisor (constant value 10)
.continue:
xor dx.dx ;dx:ax = numerator zero extended to 32 bits
div bx ;ax = numerator / 10
cmp ax,10 ;Is it small enough now?
jae .continue ; no, keep going
pop dx
pop bx
.done:
ret
注意:为了获得最大的性能,最好为每个除法使用不同的除数,选择除数可以最大限度地减少除法的数量。例如";如果不小于10000,则除以10000";那么";如果不小于100,则除以100";那么";如果不小于10,则除以10";。很难说性能改进是否值得所需的额外复杂性(以及更差的代码可读性(。
转念一想;它并没有那么混乱(未经测试,NASM语法(:
section .data
const10000: dw 10000
const100: dw 100
const10: dw 10
section .text
;Input
; cx Clock ticks since midnight
;
;Output
; ax Most significant non-zero decimal digit of clocks since midnight (unless
; clocks since midnight is zero, where zero will be returned)
;
;Trashed
; none (contents of all registers not used for outputs are preserved)
get_most_significant_decimal_digit:
mov ax,cx ;ax = numerator = clocks since midnight
push dx
cmp ax,10000
jb .l1
xor dx,dx
div word [const10000]
.l1:
cmp ax,100
jb .l2
xor dx,dx
div word [const100]
.l2:
cmp ax,10
jb .l3
xor dx,dx
div word [const10]
.l3:
pop dx
ret
对于该替代方案;更坏的情况";是3个分部和3个分支(而不是以前方法的4个分部和5个分支(。