Title: 8086 Addressing Modes
1ADVANCED MICROPROCESSORS
Session XXI Data Conversion Routines 15.11.2005
Dr. K.Rajanikanth M.S.Ramaiah Institute of
Technology, Bangalore
2Learning Objectives
- Learn to develop Data Conversion Routines
- Using Algorithmic Techniques and
- Through Look Up Tables.
3Introduction - 1
- Often Data available in one format needs to be
converted in to some other format. - Examples
- ASCII to Binary
- Binary to ASCII
- BCD to 7-Segment Code
4Introduction - 2
- Data Conversion may be based on
- Algorithm
- Look Up Table
5Converting from Binary to ASCII - 1
- Example Binary number 0100 0011 67 D
- To display this on the screen, we need to convert
- this binary number in to Two ASCII characters,
- 6 and 7.
- ASCII code for character 6 is 36H and
- ASCII code for character 7 is 37H.
6Binary to ASCII - 2
- Another Example
- Binary number 0000 0010 0100 0011 579 D
- To display this on the screen, we need Three
ASCII characters, 5, 7 and 9. - ASCII code for character 5 is 35H,
- ASCII code for character 7 is 37H, and
- ASCII code for character 9 is 39H
7Binary to ASCII Algorithm - 1
- Binary number 0000 0010 0100 0011 579 D
- Divide 579 by 10
Quotient 57 Remainder 9 , Save 9 - Divide 57 by 10
Quotient 5 Remainder 7 , Save 7 - Divide 5 by 10
Quotient 0 Remainder 5 , Save 5
8Binary to ASCII Algorithm - 2
- Quotient 0 ? Conversion Complete.
- Remainders saved in the order of 9, 7, and 5.
- Retrieve remainders in the order of 5, 7, and 9.
- (Save Retrieve using Stack)
- While retrieving, add 30H to convert the digit
to ASCII code and then display it (or print it,
or save it)
9Binary to ASCII Program - 1
Input 16-Bit Binary Number in AX Output
Equivalent ASCII displayed on screen .MODEL
TINY .CODE .STARTUP MOV AX,2A5H Test
value CALL B2A Binary to ASCII and
Display .EXIT
10Binary to ASCII Program - 2
B2A PROC NEAR PUSH DX PUSH
CX PUSH BX MOV CX,0 Count of ASCII
digits, Initialized to 0 MOV
BX,10 Divisor is 10
11Binary to ASCII Program - 3
B2A1 MOV DX,0 Dividend in DX, AX. DX
0 DIV BX Divide by 10
PUSH DX Save remainder digit INC CX
Increment digit count OR AX,AX
Conversion completed ? (Quotient,
i.e AX 0 ?) JNZ B2A1 No, continue
division
12Binary to ASCII Program - 4
Conversion is complete as quotient in AX 0
Count of remainder digits is in CX
B2A2 POP DX Retrieve remainder in
DL ADD DL,30H Convert to ASCII
MOV AH,06H Console Display Function INT
21H DOS Service, display digit LOOP
B2A2 Repeat for all digits
13Binary to ASCII Program - 5
Clean up Return. AX is destroyed POP
BX POP CX POP DX RET B2A ENDP END
14Binary to ASCII Another Method -1
- Number lt 100 ? Alternative, simpler method
exists. - AAM (ASCII Adjust AX After Multiplication)
- ? Converts value in AX in to 2-Digit Unpacked
- BCD in AX.
- Example AX 0027H (39 Decimal)
- Execute AAM Now, AX 0309H
15Binary to ASCII Another Method -2
- AX 0309H This is Unpacked BCD.
- Now, add 3030H to AX to get 3339H Packed
- ASCII representation.
- Separate the two bytes (unpack) to get the two
- ASCII characters representing the given number.
- Works only when the number is less than 100.
16Another Program - 1
Input Binary Number in AL, Assumed lt100
Output Equivalent ASCII displayed on
screen .MODEL TINY .CODE .STARTUP MOV AL
, 2AH Test value CALL B2A
Binary to ASCII and Display .EXIT
17Program - 2
B2A PROC NEAR PUSH DX MOV
AH,0 AX Number AAM AX
Unpacked BCD ADD AX, 3030H Convert to
ASCII PUSH AX Now, unpack and display
18Program - 3
MOV DL , AH First Digit MOV AH ,
06H Display Function INT 21H
Display first digit POP AX
Retrieve value MOV DL , AL
Second Digit MOV AH , 06H Display
Function INT 21H Display
second digit
19Program - 4
Clean up Return. AX is destroyed POP
DX RET B2A ENDP END
20Refinements - 1
- Leading 0 is displayed.
- Example AL 7H. What is displayed 07
- Can we replace leading 0 with a blank so that
the display - looks better?
- Yes. Check if the first digit is 0.
- If so, display 20H (blank)
- else, display the digit.
21Refinements - 2
Revised Code for displaying first digit ADD AH
, 20H CMP AH , 20H First Digit 0? JZ
B2A1 Display blank ADD AH , 10H ASCII
Code B2A1 MOV DL , AH First Digit MOV AH
, 06H Display Function INT 21H Display
first digit
Old Code for displaying first digit MOV DL ,
AH First Digit MOV AH , 06H Display
Function INT 21H Display first digit
22ASCII to Binary Algorithm - 1
- Example ASCII 156
- 3 characters, 1 , 5, and 6
- With codes as 31H, 35H, and 36H.
- Converted Binary Value must be
- 0000 0000 1001 1100
23ASCII to Binary Algorithm - 2
- Conversion Procedure
- Start with (Binary) Result 0
- First ASCII digit 31H
- Subtract 30H to get corresponding
- BCD digit 01H.
- Result Result 10 Next BCD Digit
- Result 0 10 01 0000 0000 0000 0001
24ASCII to Binary Algorithm - 3
- Conversion Procedure (continued)
- Next ASCII digit 35H
- Subtract 30H to get corresponding
- BCD digit 05H.
- Result Result 10 Next BCD Digit
- Result 01 10 05
- 0000 0000 0000 1111
25ASCII to Binary Algorithm - 4
- Conversion Procedure (continued)
- Next ASCII digit 36H
- Subtract 30H to get corresponding
- BCD digit 06H.
- Result Result 10 Next BCD Digit
- Result 15 10 06
- 0000 0000 1001 1100
26ASCII to Binary Algorithm - 5
- Conversion Procedure (continued)
- ASCII digits exhausted.
- Conversion completed.
- Result 0000 0000 1001 1100
27ASCII to Binary Program - 1
- ASCII characters representing a number are
- read from key board.
- The first non-digit character (any character
- other than 0 through 9) typed signals the
- end of the number entry
28ASCII to Binary Program - 2
- Result returned in AX, which is then stored in
- memory
location TEMP. - Result assumed not to exceed 16 bits!
- Program can be modified to accept larger
- numbers by implementing 32- bit addition.
29ASCII to Binary Program - 3
- .MODEL SMALL
- .DATA
- TEMP DW ?
- .CODE
- .STARTUP
- CALL RDNUM
- MOV TEMP, AX
- .EXIT
30ASCII to Binary Program - 4
- RDNUM PROC NEAR
- PUSH BX
- PUSH CX
- MOV CX,10 Multiplier is 10
- MOV BX,0 Result initialized to 0
- RDN1 MOV AH,1 Read Key with Echo
- INT 21H
31ASCII to Binary Program - 5
- Check if digit. If less than 0 or greater
than 9 - Number entry is over
- CMP AL,0
- JB RDN2
- CMP AL,9
- JA RDN2
32ASCII to Binary Program - 7
- Is digit. Update Result
- SUB AL, 30H BCD Digit
- PUSH AX
- MOV AX,BX
- MUL CX
- MOV BX,AX Result Result 10
- POP AX
- MOV AH,0 AX Current Digit
- ADD BX,AX Update Result
- JMP RDN1 Repeat
33ASCII to Binary Program - 6
- Non- digit. Clean Up and Return
- RDN2 MOV AX,BX Result in AX
- POP CX
- POP BX
- RET
- RDNUM ENDP
- END
34Using Look Up Tables for Data Conversion
- Often, a look-up table simplifies data
conversion. - XLAT can be used if table has up to 256
byte-entries - Value to be converted is used to index in to the
table containing conversion values.
35BCD to 7-Segment Code - 1
- 7 Segment display with active high (Logic 1)
input - to light a segment.
Control Byte
a
b
f
a
b
c
d
e
f
g
dp
g
c
e
Bit 1 ? Segment is on 0 ? Segment is off
d
dp
36BCD to 7-Segment Code - 2
- Example Display code for 3 No decimal point
Control Byte a 1 b 1 c 1 d 1 e 0
f 0 g 1 dp 0.
a
b
f
1
1
1
1
0
0
1
0
g
c
e
Display Code for 3 4F H Similarly, for other
digits.
d
dp
37BCD to 7-Segment Code Program - 1
- Input AL BCD Digit
- Output AL 7-Segment code.
- BT7SEG PROC FAR
- PUSH BX
- MOV BX, OFFSET TABLE
- XLAT CSTABLE
- POP BX
- RET
38BCD to 7-Segment Code Program - 2
- TABLE DB 3FH 0
- DB 06H 1
- DB 5BH 2
- DB 4FH 3
- DB 66H 4
- DB 6DH 5
- DB 7DH 6
- DB 07H 7
- DB 7FH 8
- DB 6FH 9
- BT7SEG ENDP
39BCD to 7-Segment Code Program Notes
- XLAT instruction does not normally contain an
operand. - Here we are using the operand (TABLE). It is
dummy! Used only to specify segment override. - XLAT uses DS by default. Here the table is in CS.
So segment override is being specified.
40More Programming Examples
- More examples are discussed in the Text Book.
- Reading Displaying Hexadecimal Data
- Using a Look-Up table to access ASCII strings
- Program to get Time Date using Function
- Codes 2CH and 2AH respectively and then to
- display them.
Thank You