360 likes | 1.6k Views
ASCII table. Displaying Numbers as Text. Problem: display numerical values as text Consider the numerical value 0x5A held in a single 8-bit register The string “0x5A” is to be displayed on the LCD. How to do it?. Each hex digit represents 4 bits
E N D
ASCII table CS-280 Dr. Mark L. Hornick
Displaying Numbers as Text • Problem: display numerical values as text • Consider the numerical value 0x5A held in a single 8-bit register • The string “0x5A” is to be displayed on the LCD
How to do it? • Each hex digit represents 4 bits • Ex: Numeric value 0x5A in binary is 01011010. • We need to output characters ‘0’, ‘x’, ‘5’, ‘A’ • ‘0’ is ASCII character code 0x30 • ‘x’ is ASCII 0x78 • ‘5’ is ASCII 0x35 • ‘A’ is ASCII 0x41 • Is there any pattern here? CS-280 Dr. Mark L. Hornick
Pattern? • ASCII characters ‘0’-’9’: • The ASCII code for ‘0’ is 0x30 = 0x0 + 0x30 • The ASCII code for ‘1’ is 0x31 = 0x1 + 0x30 • The ASCII code for ‘8’ is 0x38 = 0x8 + 0x30 • The ASCII code for ‘9’ is 0x39 = 0x9 + 0x30 • ASCII characters ‘A’-’F’ • The ASCII code for A is 0x41 = 0xA + 0x37 • The ASCII code for B is 0x42 = 0xB + 0x37 • The ASCII code for C is 0x43 = 0xC + 0x37 • The ASCII code for D is 0x44 = 0xD + 0x37 • The ASCII code for E is 0x45 = 0xE + 0x37 • The ASCII code for F is 0x46 = 0xF + 0x37 CS-280 Dr. Mark L. Hornick
Solution • For hex values 0x0 – 0x9 • Add 0x30 to convert to ASCII code of corresponding character ‘0’ – ‘9’ • For hex values 0xA – 0xF • Add 0x37 to convert to ASCII code of corresponding character ‘A’ – ‘F’ • Add 0x57 to convert to ASCII code of corresponding character ‘a’ – ‘f’ CS-280 Dr. Mark L. Hornick
SWAP Instruction LDI r20, 0x5A ; init SWAP r20 ; swap nibbles After SWAP, r20 contains 0xA5 CS-280 Dr. Mark L. Hornick
Converting numeric to ASCII LDI r20, 0x5A ; init r20 to a value MOV r21, r20 ; copy r20 ANDI r21, 0x0F ; r21=0x0A after mask MOV r22, r20 ; copy r20 SWAP r22 ; r22=0xA5 after swap ANDI r22, 0x0F ; r22=0x05 after mask LDI r20, 0x37 ; ASCII offset for A-F ADD r21, r20 ; r21 is now 0x41=‘A’ LDI r20, 0x30 ; ASCII offset for 0-9 ADD r22, r20 ; r22 is now 0x35=‘5’ CS-280 Dr. Mark L. Hornick