6b. 16 bit binary to BCD conversion

vtu mp 6b
Q) Convert a 16-bit binary value (assumed to be an unsigned integer) to BCD and display it from left to right and right to left for specified number of times on a 7-segment display Interface.

This program will convert a binary number to its seven segment equivalent and then display the same from left to right and right to left in the seven segment display interface.

Algorithm:
Seven Segment Display Connections: Connect the data bus to the Multiplex Seven Segment display interface. No power supply is required.
Illustration:
Step 1: The data bus from the CPU is connected the seven segment display interface.
Step 2: Initialize the control word 80H, and store it in the control register.
Step 3: To convert 16 bit binary into BCD
    for(i=0;i<4i++)
    {
    Rem=data%10 (i.e., reminder)
    DI[i]=table[rem];
    /* table contains the seven segment equivalent code for the rem*/
     Data=data/10
Step 4: while (n>0) /*n is the number of times to display from left to right and vice-versa*/
    {
    P=address of ‘msg’
    {
    q=p
    Display(q)
    Delay()       
    P++            /*  increment the pointer to the next character in the message */
    }
    /* pointer is moved to last character of the message to display from left to right */
    P=address of msg + 15
/* total 8 combinations of display (in reverse order i.e., from bottom to top) to display from left to right */
For(i=0;i<N11;i++)
{
    Q=p    /*save address of the msg */
    Display(q)    /* display procedure */
    Delay()
}
    n--;
}
Display Procedure:
i) Initialize the counter to 4 to 4 digit seven segment display.
ii) Move the contents of pointer to AL(which points to first 4 character).
iii) Initialize Port A address and load the string byte to display on the seven segment display and the pointer automatically increments to point to the next character (in the 12 character message).
iv) Introduce some delay between each display and enable next digit D2 and so on until all the digits from D1 to D4 is displayed.
Step 5: Stop

CODE:


Explanation:
.MODEL SMALL
.DATA
PA EQU 0e800H
PB EQU 0e801H
PC EQU 0e802H
CR EQU 0e803H
CW DB 80H
M1 DB 10,13,"PRESS ANY KEY TO EXIT$"
MSG DB 0FFH,0FFH,0FFH,0FFH,?,?,?,?,?,0FFH,0FFH,0FFH,0FFH
TABLE DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H
BCD DB 5 DUP(?)
NUM DW 0ffh

Programming mode:small, one data one code segment
Data segment contains port addresses, control word and messages to be displayed.
MSG contains the BCD to be displayed on the seven segment display. 5 ‘?’s because we will fill it up once we have calculated the values.
TABLE contains the seven segment values of each digit from 0-9.
(see how to convert any character to seven segment equivalent value by clicking here)
BCD is a 5 byte length location used to store the BCD of the input we give.
NUM is the input. (0FFH=00255 decimal)

.CODE
    MOV AX,@DATA
    MOV DS,AX
    MOV AL,CW
    MOV DX,CR
    OUT DX,AL
    LEA DX,M1
    MOV AH,09H
    INT 21H
    CALL BIN_SSC

Code segment:
Initialization of data segment. And activating the hardware interface as usual. (first 5 lines)
Next is displaying the message M1 using LEA and 09H service on Interrupt 21H.
Call the BIN_SSC to convert the input from Binary to Seven segment equivalent.

BIN_SSC will convert the number into the 7 segment equivalent. Lets see about that in the later part. For now, we assume that the conversion is over. And look at the displaying from left to right part.


START:MOV BH,10
    LEA DI,MSG
BACK:MOV SI,DI
    PUSH BX
    CALL DISP_MSG
    CALL DELAY
    CALL KBHIT
    POP BX
    INC DI
    DEC BH
    JNZ BACK

START is a label. Move 10 to BH, this will be the counter. Since we have 13 values in the ‘msg’ array. And we want to display 4 at a time from left to right (since seven segment display in the lab has only 4 character capacity). Load the offset of msg into SI register.
BACK is again a label. Move the DI to SI, (we are going to display 4 characters from SI on the display)
Push the value of BX into stack for back up before calling DISP_CHAR.
Call the DISP_MSG function to display 4 characters from SI.
After displaying, call for some delay.
KBHIT is for detecting any button press by the user during execution and terminate if there was a press. You may neglect the KBHIT function and all related stuffs to KBHIT.
After displaying 4 characters POP BX back and increment the ‘msg’ index DI to point to next location.
Decrement counter BH and if not zero, jump to label back to print the next four characters. When BH becomes 0, we will have all the characters displayed on the display. Now its time to display in reverse order.

MOV BH,10    LEA DI,MSG+9
BACK1:MOV SI,DI
    PUSH BX
    CALL DISP_MSG
    CALL DELAY
    CALL KBHIT
    POP BX
    DEC DI
    DEC BH
    JNZ BACK1
JMP START

So, again move the counter value 10 to BH.
Load MSG+9 to DI. (The offset of last but 3 rd character. Since we are displaying from right to left, the right most 4 characters’ offset will be MSG+9. Check out)
BACK1 is the label. Move DI to SI and print 4 characters from SI using DISP_CHAR.
Call some delay, Call KBHIT (you can neglect) and pop back BX.
Decrement DI to print the previous 4 characters (right to left, remember). Decrement the counter and if not 0, go to BACK1 to display the next 4 characters in reverse order.


Now we displayed left to right and right to left. Then jump to START to repeat this process again and again.


Now the conversion part,
BIN_SSC function.

BIN_SSC PROC
    LEA SI,BCD
    MOV AX,NUM
    MOV BX,10000
    CALL CONV
        MOV BX,1000
    CALL CONV
    MOV BX,100
    CALL CONV
        MOV BX,10
    CALL CONV
    MOV [SI],DL

BIN_SSC is the procedure header.
Load the BCD’s offset to SI, NUM (the input) to AX..
We need to extract each values and store it in BCD array. Extracting may be done using simple division.
Consider our input. 00255.
Move 10000 to BX and call CONV to divide 00255 by 10000. We will get the quotient 0. Store this in BCD array. Reminder 0255 will be stored in AX for the next division.
Next, move 1000 to BX and divide on AX. Quotient will be 0 , store in BCD+1. Reminder 255 will be stored in AX for next division.
Perform this procedure to extract all the digits.
Store each digits in BCD.


LEA SI,BCD
    LEA DI,MSG+8
    LEA BX,TABLE
    MOV CX,5
NEXT:MOV AL,[SI]
    XLAT
    MOV [DI],AL
    DEC DI
    INC SI
    LOOP NEXT
    RET
BIN_SSC ENDP

After getting each digits, Load BCD’s offset to SI again, MSG+8 to DI(we will store each BCD digit’s Seven segment equivalent in MSG array in reverse order that is MSG+8, MSG+7, MSG+6 and so on).
Move offset of Table to BX (XLAT will convert the AL value to its lookup table equivalent. Here, the Look up table is 7 segment table. After conversion, it will store back the 7 segment value to AL)
Move the counter to 5. (since there are 5 digits).
NEXT is a label.
Move [SI] to AL.(first BCD digit).
Call XLAT. This will fetch the 7 segment equivalent of first BCD digit and store in AL.
Move the resulting AL value to [DI].
Then decrement DI and increment SI.
LOOP to NEXT. (This will run 5 times, each time converting each BCD digit and storing in MSG array).
RET at last. Conversion is over.


CONV PROC
    MOV DX,0
    DIV BX
    MOV [SI],AL
    MOV AX,DX
    INC SI
    RET
CONV ENDP

Conversion using division has been explained above.


KBHIT PROC
    MOV AH,1
    INT 16H
    JNZ EXIT
    RET
EXIT:MOV AH,4CH
    INT 21H
KBHIT ENDP

KBHIT senses the key pressed by user(if any) using 16H interrupt and 01H service number and terminates the program if any key was pressed. may be actually neglected. (if neglecting, dont forget to put MOV AH,4CH and INT 21H wherever necessary, in the program.)


DISP_MSG PROC
    MOV CX,4
NXT_CHAR:MOV BL,8
    MOV AL,[SI]
NXTBIT:ROL AL,1
    MOV DX,PB
    OUT DX,AL
    PUSH AX
    MOV AL,00H
    MOV DX,PC
    OUT DX,AL
    MOV AL,11H
    MOV DX,PC
    OUT DX,AL
    POP AX
    DEC BL
    JNZ NXTBIT
    INC SI
    LOOP NXT_CHAR
    RET
DISP_MSG ENDP


DISP_MSG is procedure to display 4 characters.
(see "how to display a message in seven segment display" by clicking here)
DELAY PROC
    PUSH CX
    PUSH BX
    MOV CX,0FFFFH
N3:MOV BX,0FFFFH
N4:DEC BX
    JNZ N4
    LOOP N3
    POP BX
    POP CX
    RET
DELAY ENDP
END

DELAY procedure is the same. You may end the program now.


OUTPUT:
Seen in 7 segment display.

He is a simple passionate tech freak who himself is an engineering student at Canara Engineering college. He likes App Development, Web designing, Blogging, Youtubing, Debugging and also is a CodeGeek!

Sharing is sexy!

Related Articles

Share your views about this article!