Thursday 31 January 2013

PROCEDURE TO DISPLAY A MULTIPLE DIGIT NUMBER IN 8086 ASSEMBLY LANGUAGE


Good day, Today we will improve on our program from previous post which calculates the average of two numbers in 8086 assembly language to calculate for n numbers, also, you would have noticed that our previous program collects, calculates and displays the average of numbers with a single digit and that doesn't qualify a good program.
Therefore, We will want our program to accept, calculate and display average for numbers with multiple digits.
First we modify our display procedure, making it able to display multiple digits numbers like 10, 80, 345 etc. instead of single characters.
Well, What our new procedure basically does is to split the provided value(in AX) to single digits(i.e by continually dividing it by 10 till it turns 0), change them to their ASCII code equivalence and display them using a loop.

Here is the sample code.
;ASSEMBLER: MASM611
display proc ;Beginning of procedure
MOV BX, 10 ;Initializes divisor
MOV DX, 0000H ;Clears DX
MOV CX, 0000H ;Clears CX

;Splitting process starts here
.Dloop1: MOV DX, 0000H ;Clears DX during jump
div BX ;Divides AX by BX
PUSH DX ;Pushes DX(remainder) to stack
INC CX ;Increments counter to track the number of digits
CMP AX, 0 ;Checks if there is still something in AX to divide
JNE .Dloop1 ;Jumps if AX is not zero

.Dloop2: POP DX ;Pops from stack to DX
ADD DX, 30H ;Converts to it's ASCII equivalent
MOV AH, 02H
INT 21H ;calls DOS to display character
LOOP .Dloop2 ;Loops till CX equals zero
RET ;returns control
display ENDP

No comments: