Extrn Bin1:Word
Extrn Bin2:Word
Extrn Len1:Byte
Extrn Len2:Byte
Extrn Num1:Byte
Extrn Num2:Byte
Extrn Dividend:Byte
Extrn Divisor:Byte
Extrn Sign:Byte
Extrn Quotient:Byte
Extrn Remainder:Byte
Extrn AnsMsg:Byte
Public divFloat
.Model Small
.Code
divFloat PROC Far
; Input: Num1 is Dividend (in ASCII characters)
; Num2 is Divisor (in ASCII characters)
; Input: Bin1 is Dividend (in binary)
; Bin2 is Divisor (in binary)
xor cx,cx ; move Num1 to Dividend
mov cl,Len1 ; get length of input
lea si,Num1 ; point to start of number
lea di,Dividend ;
rep movsb ; move memory (@ in si) to memory
; (@ in di )
; byte by byte until cx = 0 (rep will
; repeat and check. Direction flag will
; affect whether si and di are
; incremented or decremented
xor cx,cx ; move Num2 to Divisor
mov cl,Len2
lea si,Num2
lea di,Divisor
rep movsb
lea si,Quotient
lea di,Remainder
mov ax,Bin1 ; load registers with binary values
mov bx,Bin2
div bl ; ah,al(remainder,quotient) = ax / bl
push bx ; preserve bx (divisor) on stack
push ax ; preserve ax (dividend)
cmp al,99 ; if quotient > 99d (3 digit)
ja QMore99 ; then handle
cmp al,9 ; else if quotient > 9d (2 digit)
ja QMore9 ; then handle remainder
or al,30h ; else
mov [si+2],al ; store in quotient print buffer
; (right justified).
jmp CompRem ; now process remainder
QMore9: xor ah,ah ; clear high byte of ax
mov bl,10 ; put 10d in bl
div bl ; divide by 10
or al,30h ; convert quotient to ascii
mov [si+1],al ; store in buffer
or ah,30h ; convert remainder to ascii
mov [si+2],ah ; store remainder in buffer
jmp CompRem ; then handle remainder
QMore99: xor ah,ah ; e.g., al=123
mov bl,100 ; bl=100
div bl ; Then, al=1 & ah=23 (100's)
or al,30h ; find and convert high digit of quotient
mov [si],al ; to binary and put in print buffer
mov al,ah ; shift remainder to quotient
xor ah,ah ; zero high byte of ax
mov bl,10 ; set divisor to 10
div bl ; ax = ax/bl
or al,30h ; adjust quotient to be printable
mov [si+1],al ; move to message buffer
or ah,30h ; remainder (1's) - convert to printable
mov [si+2],ah ; put in buffer
CompRem: pop ax ; get remainder from original divide
mov al,ah ; move remainder in ah to al
xor ah,ah ; clear ah
mov bl,10 ; set divisor to 10
mul bl ; left shift by decimal 10 .25 = 2.5
pop bx ; get original divisor copy from stack
push bx ; put a copy back on the stack for late
div bl ; divide by divisor
or al,30h ; convert to printable
mov [di],al ; put in print buffer
mov al,ah ; move remainder to al
xor ah,ah ; clear ah
mov bl,10 ; multiply by 10 - shift decimal point
mul bl
pop bx ; get original divisor
div bl ; divide
or al,30h ; convert quotient to printable
mov [di+1],al ; and store (note further precision
; discarded)
lea dx, AnsMsg ; get @ of answer message
mov ah, 09h ; print
int 21h
ret
divFloat ENDP
END