ASCII table CS280 Dr Mark L Hornick 1
ASCII table CS-280 Dr. Mark L. Hornick 1
Displaying Numbers as Text l Problem: display numerical values as text l Consider the numerical value 0 x 5 A held in a single 8 -bit register l The string “ 0 x 5 A” is to be displayed on the LCD 2
How to do it? l Each hex digit represents 4 bits l l Ex: Numeric value 0 x 5 A in binary is 0101 1010. We need to output characters ‘ 0’, ‘x’, ‘ 5’, ‘A’ l l l ‘ 0’ is ASCII character code 0 x 30 ‘x’ is ASCII 0 x 78 ‘ 5’ is ASCII 0 x 35 ‘A’ is ASCII 0 x 41 Is there any pattern here? CS-280 Dr. Mark L. Hornick 3
Pattern? l ASCII characters ‘ 0’-’ 9’: l The ASCII code for ‘ 0’ is 0 x 30 = 0 x 0 + 0 x 30 l The ASCII code for ‘ 1’ is 0 x 31 = 0 x 1 + 0 x 30 l The ASCII code for ‘ 8’ is 0 x 38 = 0 x 8 + 0 x 30 l The ASCII code for ‘ 9’ is 0 x 39 = 0 x 9 + 0 x 30 l ASCII characters ‘A’-’F’ l The ASCII code for A is 0 x 41 = 0 x. A + 0 x 37 l The ASCII code for B is 0 x 42 = 0 x. B + 0 x 37 l The ASCII code for C is 0 x 43 = 0 x. C + 0 x 37 l The ASCII code for D is 0 x 44 = 0 x. D + 0 x 37 l The ASCII code for E is 0 x 45 = 0 x. E + 0 x 37 l The ASCII code for F is 0 x 46 = 0 x. F + 0 x 37 CS-280 Dr. Mark L. Hornick 4
Solution l For hex values 0 x 0 – 0 x 9 l l Add 0 x 30 to convert to ASCII code of corresponding character ‘ 0’ – ‘ 9’ For hex values 0 x. A – 0 x. F l l Add 0 x 37 to convert to ASCII code of corresponding character ‘A’ – ‘F’ Add 0 x 57 to convert to ASCII code of corresponding character ‘a’ – ‘f’ CS-280 Dr. Mark L. Hornick 5
SWAP Instruction LDI SWAP r 20, 0 x 5 A r 20 ; init ; swap nibbles After SWAP, r 20 contains 0 x. A 5 CS-280 Dr. Mark L. Hornick 6
Converting numeric to ASCII LDI MOV ANDI MOV SWAP ANDI LDI ADD r 20, r 21, r 22, r 20, r 21, r 20, r 22, 0 x 5 A r 20 0 x 0 F 0 x 37 r 20 0 x 30 r 20 ; ; ; ; ; init r 20 to a value copy r 20 r 21=0 x 0 A after mask copy r 20 r 22=0 x. A 5 after swap r 22=0 x 05 after mask ASCII offset for A-F r 21 is now 0 x 41=‘A’ ASCII offset for 0 -9 r 22 is now 0 x 35=‘ 5’ CS-280 Dr. Mark L. Hornick 7
- Slides: 7