将奇数小数位计算为整数中总位数的分数
Count odd decimal digits as a fraction of total digits in an integer
Write an assembly MACRO that takes an integer number as parameter.
Keep dividing the number by 10 until it reaches zero. Then compute the
percentage of odd digits in that number. ( Example if num=73458 then
the percentage is = 3/5=0.6)
我的代码
.model small
.code
.data
x dd 73458
MOV AX,@DATA
MOV DS,AX
mov si,offset x
mov ax,[si]
mov dx,[si+2]
mov cx,0
start:
cmp ax,0
je l2
mov bx,10
div bx
mov dl,al
mov dh,0
mov al,0
mov bl,2
div bl
cmp ah,0
jne l3
je l4
l4:
mov ax,dx
jmp start
l3:
inc cx
mov ax,dx
jmp start
l2:
mov dl,'0'
mov ah,2
int 21h
mov dl,','
mov ah,2
int 21h
mov ax,5
mov bh,0
mov bx,cx
div bl
mov dl,ah
mov ah,2
int 21h
end
但是上面有错误你能帮我避免错误吗
您已将 73458 加载到 DX:AX。第一次执行DIV BX后,DX就是余数(8),AX就是结果(7345)。您可以测试余数是否为奇数,如下所示:
mov bx,10
start:
div bx
test dl, 1
jz l1 ; Bit zero is zero so the digit is even
inc cx ; Count up odd digit
l1:
test ax, ax ; If ax is zero, we are done
jz l2
xor dx, dx ; Set dx to zero for next divide
jmp start
Write an assembly MACRO that takes an integer number as parameter. Keep dividing the number by 10 until it reaches zero. Then compute the percentage of odd digits in that number. ( Example if num=73458 then the percentage is = 3/5=0.6)
我的代码
.model small
.code
.data
x dd 73458
MOV AX,@DATA
MOV DS,AX
mov si,offset x
mov ax,[si]
mov dx,[si+2]
mov cx,0
start:
cmp ax,0
je l2
mov bx,10
div bx
mov dl,al
mov dh,0
mov al,0
mov bl,2
div bl
cmp ah,0
jne l3
je l4
l4:
mov ax,dx
jmp start
l3:
inc cx
mov ax,dx
jmp start
l2:
mov dl,'0'
mov ah,2
int 21h
mov dl,','
mov ah,2
int 21h
mov ax,5
mov bh,0
mov bx,cx
div bl
mov dl,ah
mov ah,2
int 21h
end
但是上面有错误你能帮我避免错误吗
您已将 73458 加载到 DX:AX。第一次执行DIV BX后,DX就是余数(8),AX就是结果(7345)。您可以测试余数是否为奇数,如下所示:
mov bx,10
start:
div bx
test dl, 1
jz l1 ; Bit zero is zero so the digit is even
inc cx ; Count up odd digit
l1:
test ax, ax ; If ax is zero, we are done
jz l2
xor dx, dx ; Set dx to zero for next divide
jmp start