Home > Enterprise >  Count odd decimal digits as a fraction of total digits in an integer
Count odd decimal digits as a fraction of total digits in an integer

Time:11-07

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)

my code

.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

but there are errors on it can you help me to avoid errors

CodePudding user response:

You have loaded 73458 into DX:AX. After the first time DIV BX is executed, DX will be the remainder (8) and AX will be the result (7345). You could test if the remainder is odd as follows:

  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
  • Related