ascii

source

/* This program gives information about an ascii code. ** Its arguments are taken as octal, hex, or decimal numbers ** and it prints information about the character. ** With an argument of -, all values are printed. ** With no argument, ascii expects characters from the keyboard. ** To exit, push the same key three times in a row. */ #include <stdlib.h> #include <stdio.h> #include <termios.h> #include <unistd.h> #include <ctype.h> char *value[] = { "nul ^@", "soh ^A", "stx ^B", "etx ^C", "eot ^D", "enq ^E", "ack ^F", "bel ^G", "bs ^H", "ht ^I", "nl ^J", "vt ^K", "np ^L", "cr ^M", "so ^N", "si ^O", "dle ^P", "dc1 ^Q", "dc2 ^R", "dc3 ^S", "dc4 ^T", "nak ^U", "syn ^V", "etb ^W", "can ^X", "em ^Y", "sub ^Z", "esc ^[", "fs ^\\ ^shL", "gs ^] ^shM", "rs ^^ ^shN", "us ^_ ^shO", "sp", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=", ">", "?", "@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "[", "\\", "]", "^", "_", "`", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~", "del ^?" }; static void show_char(int c) { printf(" %#4x %#4o %3d %s\n", c, c, c, value[c]); } int main(int argc, char **argv) { struct termios orig, new; int base, current, last, lastlast; current = last = lastlast = -1; if (argc > 1) { if (*argv[1] == '-') { for (current = 0; current <= 127; current++) show_char(current); } else { for (int i = 1; i < argc; i++) { char *cp = argv[i]; current = 0; switch (*cp) { case '0': base = 8; cp++; if (*cp != 'x') break; /* else fall through */ case 'x': base = 16; cp++; break; default: base = 10; } for (;; ++cp) { int d; if (!isxdigit (*cp)) break; if (isdigit (*cp)) d = *cp - '0'; else if (isupper (*cp)) d = *cp - 'A' + 10; else d = *cp - 'a' + 10; if (d > base) break; current = base * current + d; } if (*cp != '\0') { printf("%s???\n", cp); exit(1); } if (current <= 127) { show_char(current); } else printf("value %s out of range\n", argv[i]); } } } else { (void) tcgetattr(2, &orig); new = orig; (void) cfmakeraw(&new); (void) tcsetattr(2, TCSANOW, &new); while (((current = (0177 & getchar())) != last) || (current != lastlast)) { lastlast = last; last = current; show_char(current); putchar('\r'); // device is raw } (void) tcsetattr(2, TCSANOW, &orig); } }

assembly

0000000000401196 <show_char>: 48 83 ec 08 sub $0x8,%rsp 89 fe mov %edi,%esi 48 63 c7 movslq%edi,%rax 4c 8b 04 c5 80 40 40 mov 0x404080(,%rax,8),%r8 00 89 f9 mov %edi,%ecx 89 fa mov %edi,%edx bf 10 20 40 00 mov $0x402010,%edi b8 00 00 00 00 mov $0x0,%eax e8 76 fe ff ff call 401030 <printf@plt> 48 83 c4 08 add $0x8,%rsp c3 ret 00000000004011bf <main>: 41 55 push %r13 41 54 push %r12 55 push %rbp 53 push %rbx 48 81 ec 88 00 00 00 sub $0x88,%rsp 83 ff 01 cmp $0x1,%edi 0f 8e 1a 01 00 00 jle 4012ef <main+0x130> 41 89 fd mov %edi,%r13d 49 89 f4 mov %rsi,%r12 48 8b 46 08 mov 0x8(%rsi),%rax 80 38 2d cmpb $0x2d,(%rax) 74 21 je 401205 <main+0x46> e8 b7 fe ff ff call 4010a0 <__ctype_b_loc@plt> 48 89 c5 mov %rax,%rbp 49 8d 5c 24 08 lea 0x8(%r12),%rbx 41 8d 45 fe lea -0x2(%r13),%eax 4d 8d 64 c4 10 lea 0x10(%r12,%rax,8),%r12 41 bd 10 00 00 00 mov $0x10,%r13d e9 97 00 00 00 jmp 40129c <main+0xdd> bb 00 00 00 00 mov $0x0,%ebx 89 df mov %ebx,%edi e8 85 ff ff ff call 401196 <show_char> 83 c3 01 add $0x1,%ebx 81 fb 80 00 00 00 cmp $0x80,%ebx 75 ee jne 40120a <main+0x4b> e9 5f 01 00 00 jmp 401380 <main+0x1c1> 49 8d 71 01 lea 0x1(%r9),%rsi b9 08 00 00 00 mov $0x8,%ecx 41 80 79 01 78 cmpb $0x78,0x1(%r9) 0f 85 82 00 00 00 jne 4012b7 <main+0xf8> eb 03 jmp 40123a <main+0x7b> 4c 89 ce mov %r9,%rsi 48 83 c6 01 add $0x1,%rsi 44 89 e9 mov %r13d,%ecx eb 74 jmp 4012b7 <main+0xf8> f6 c4 01 test $0x1,%ah 74 31 je 401279 <main+0xba> 0f be c2 movsbl%dl,%eax 83 e8 37 sub $0x37,%eax 39 c8 cmp %ecx,%eax 7f 2f jg 401281 <main+0xc2> 0f af f9 imul %ecx,%edi 01 c7 add %eax,%edi 48 83 c6 01 add $0x1,%rsi 0f b6 16 movzbl(%rsi),%edx 48 0f be c2 movsbq%dl,%rax 41 0f b7 04 40 movzwl(%r8,%rax,2),%eax f6 c4 10 test $0x10,%ah 74 15 je 401281 <main+0xc2> f6 c4 08 test $0x8,%ah 74 d2 je 401243 <main+0x84> 0f be c2 movsbl%dl,%eax 83 e8 30 sub $0x30,%eax eb d5 jmp 40124e <main+0x8f> 0f be c2 movsbl%dl,%eax 83 e8 57 sub $0x57,%eax eb cd jmp 40124e <main+0x8f> 84 d2 test %dl,%dl 75 3d jne 4012c2 <main+0x103> 83 ff 7f cmp $0x7f,%edi 7f 51 jg 4012db <main+0x11c> e8 07 ff ff ff call 401196 <show_char> 48 83 c3 08 add $0x8,%rbx 4c 39 e3 cmp %r12,%rbx 0f 84 e4 00 00 00 je 401380 <main+0x1c1> 4c 8b 0b mov (%rbx),%r9 41 0f b6 01 movzbl(%r9),%eax 3c 30 cmp $0x30,%al 0f 84 76 ff ff ff je 401221 <main+0x62> 3c 78 cmp $0x78,%al 74 88 je 401237 <main+0x78> 4c 89 ce mov %r9,%rsi b9 0a 00 00 00 mov $0xa,%ecx 4c 8b 45 00 mov 0x0(%rbp),%r8 bf 00 00 00 00 mov $0x0,%edi eb 99 jmp 40125b <main+0x9c> bf 28 20 40 00 mov $0x402028,%edi b8 00 00 00 00 mov $0x0,%eax e8 5f fd ff ff call 401030 <printf@plt> bf 01 00 00 00 mov $0x1,%edi e8 a5 fd ff ff call 401080 <exit@plt> 4c 89 ce mov %r9,%rsi bf 2f 20 40 00 mov $0x40202f,%edi b8 00 00 00 00 mov $0x0,%eax e8 43 fd ff ff call 401030 <printf@plt> eb a0 jmp 40128f <main+0xd0> 48 8d 5c 24 40 lea 0x40(%rsp),%rbx 48 89 de mov %rbx,%rsi bf 02 00 00 00 mov $0x2,%edi e8 5f fd ff ff call 401060 <tcgetattr@plt> 48 89 e7 mov %rsp,%rdi b9 0f 00 00 00 mov $0xf,%ecx 48 89 de mov %rbx,%rsi f3 a5 rep movsl%ds:(%rsi),%es:(%rdi) 48 89 e7 mov %rsp,%rdi e8 3a fd ff ff call 401050 <cfmakeraw@plt> 48 89 e2 mov %rsp,%rdx be 00 00 00 00 mov $0x0,%esi bf 02 00 00 00 mov $0x2,%edi e8 48 fd ff ff call 401070 <tcsetattr@plt> 41 bc ff ff ff ff mov $0xffffffff,%r12d bd ff ff ff ff mov $0xffffffff,%ebp eb 1d jmp 401352 <main+0x193> 89 df mov %ebx,%edi e8 5a fe ff ff call 401196 <show_char> 48 8b 35 3d 31 00 00 mov 0x313d(%rip),%rsi # 404480 <stdout@@GLIBC_2.2.5> bf 0d 00 00 00 mov $0xd,%edi e8 f3 fc ff ff call 401040 <putc@plt> 41 89 ec mov %ebp,%r12d 89 dd mov %ebx,%ebp 48 8b 3d 37 31 00 00 mov 0x3137(%rip),%rdi # 404490 <stdin@@GLIBC_2.2.5> e8 32 fd ff ff call 401090 <getc@plt> 83 e0 7f and $0x7f,%eax 89 c3 mov %eax,%ebx 41 39 c4 cmp %eax,%r12d 75 cd jne 401335 <main+0x176> 39 c5 cmp %eax,%ebp 75 c9 jne 401335 <main+0x176> 48 8d 54 24 40 lea 0x40(%rsp),%rdx be 00 00 00 00 mov $0x0,%esi bf 02 00 00 00 mov $0x2,%edi e8 f0 fc ff ff call 401070 <tcsetattr@plt> b8 00 00 00 00 mov $0x0,%eax 48 81 c4 88 00 00 00 add $0x88,%rsp 5b pop %rbx 5d pop %rbp 41 5c pop %r12 41 5d pop %r13 c3 ret