ascii table

7
CS-280 Dr. Mark L. Hornick 1 ASCII table

Upload: inara

Post on 07-Jan-2016

23 views

Category:

Documents


0 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: ASCII table

CS-280Dr. Mark L. Hornick

1

ASCII table

Page 2: ASCII table

2

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

Page 3: ASCII table

CS-280Dr. Mark L. Hornick

3

How to do it?

Each hex digit represents 4 bits Ex: Numeric value 0x5A in binary is 0101 1010. 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?

Page 4: ASCII table

CS-280Dr. Mark L. Hornick

4

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

Page 5: ASCII table

CS-280Dr. Mark L. Hornick

5

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’

Page 6: ASCII table

CS-280Dr. Mark L. Hornick

6

SWAP Instruction

LDI r20, 0x5A ; init

SWAP r20 ; swap nibbles

After SWAP, r20 contains 0xA5

Page 7: ASCII table

CS-280Dr. Mark L. Hornick

7

Converting numeric to ASCII

LDI r20, 0x5A ; init r20 to a valueMOV r21, r20 ; copy r20ANDI r21, 0x0F ; r21=0x0A after maskMOV r22, r20 ; copy r20 SWAP r22 ; r22=0xA5 after swapANDI r22, 0x0F ; r22=0x05 after maskLDI r20, 0x37 ; ASCII offset for A-FADD r21, r20 ; r21 is now 0x41=‘A’LDI r20, 0x30 ; ASCII offset for 0-9ADD r22, r20 ; r22 is now 0x35=‘5’