Full anatomy of the Linux kernel keyboard. S Part Code Analysis (KEY_MAP)

Source: Internet
Author: User

Keyboard. S Part Code Analysis (KEY_MAP)


Keyboard in the middle there is a paragraph, I did not see at first, what is the meaning of

Key_map:.byte 0,27.ascii "1234567890-=". Byte 127,9.ascii "qwertyuiop[". Byte 13,0.ascii "ASDFGHJKL; '". Byte ", 0.ascii" \\zxcvbnm,./". Byte 0, ' *,0,32/* 36-39 */.fill 16,1,0/* 3a-49 */.byte '-,0,0,0, ' +/* 4a-4e */.byte 0,0,0,0,0 , 0,0/* 4f-55 */.byte ' <.fill 10,1,0shift_map:.byte 0,27.ascii "[Email protected]#$%^&* () _+". Byte 127,9.ascii " qwertyuiop{} ". Byte 13,0.ascii" Asdfghjkl:\ "". Byte ' ~,0.ascii "| Zxcvbnm<>? ".  byte 0, ' *,0,32/* 36-39 */.fill 16,1,0/* 3a-49 */.byte '-,0,0,0, ' +/* 4a-4e */.byte 0,0,0,0,0,0,0/* 4F-55 */.byte ' >.fill 10,1,0alt_map:.byte 0,0.ascii "\[email protected]\0$\0\0{[]}\\\0". Byte 0,0.byte 0,0,0,0,0,0,0,0,0,0,0.byte ' ~,13,0. byte 0,0,0,0,0,0,0,0,0,0,0.byte 0,0.byte 0,0,0,0,0,0,0,0,0,0,0.byte 0,0,0,0/* 36-39 */.fill 16,1,0/* 3A-49 */.byte 0,0,0 , 0,0/* 4a-4e */.byte 0,0,0,0,0,0,0/* 4f-55 */.byte ' |. Fill 10,1,0

. Byte The assembly directive defines 0 or more comma-separated byte values, and the value of each expression is 1 byte

The. ASCII "string" is assigned and stored as a string from the current position of the position counter, and can be used to write multiple strings separately, such as. ASCII "Hello World", "Don t Panic" This assembly instruction allows as to assemble these strings at successive addresses, and does not automatically add 0 (NUL) after each string




Write a code test, so a test encountered a variety of troubles, the first time to write a compilation, the first time with the assembly and C-linked, tangled for two days ... Fortunately, there are friends to help hold, discuss. It's a breath-holding ah ... Damn bug.

/**************************************************************************source File:key.ccode Writer:eofcode Date:2014.05.11e-mail: [Email protected]code purpose:         Just a test code for assembly source code---keyboard. S. If there is something wrong with my code, please  touche me by e-mail.************************************************ /#include <stdio.h>extern int map (int number); int main () {        int temp = 0;        int foo  = 0;        for (temp = 0;temp < 97;temp++)        {                foo = map (temp);                printf ("%d:%c \ n", Temp,foo);        }        return 0;}




/*******************************************************************************source File:map.ccode Writer: Eofcode date:2014.05.11e-mail: [email protected]code purpose:just a test code for assembly source code---K Eyboard. S. If there is something wrong with my code, please touche me by e-mail.**************************************************** /.global map.text.align 8key_map:. Byte 0,27. ASCII "1234567890-=". Byte        127,9. ASCII "qwertyuiop[]". Byte 13,0. ASCII "ASDFGHJKL; '"  . Byte ", 0. ASCII" \\zxcvbnm,./". Byte 0, ' *,0,32/* 36-39 */. Fill 16,1,0/* 3a-49 */. Byte '-,0,0,0, ' +/* 4a-4e */. Byte 0,0,0,0,0,0,0/* 4f-55 */. Byte ' <. Fill        10,1,0.align 8map:pushq%rbp movq%rsp,%rbp subq $32,%rsp leaq KEY_MAP,%RCX      Movq%rdi,-8 (%RBP) addq-8 (%RBP),%RCX  MOVSBQ (%RCX),%rax Leaveq retq 


The purpose of the program is to print out the ASCII code stored key_map as characters.

WTF

There is a tragic fact in this process,

In the RET, there will always be SIGSEGV, the hint can not write to access memory .... Do not understand, do not understand

And then this tangled day passed! The Endless gdb! T-t later the viewer learned lesson, hope not like me to hit the day time on this bug.

(GDB)
MOVSBQ (%RCX),%rax
(GDB)
Leaveq
(GDB)
Map () at./map.s:30
Retq
(GDB)
Cannot access memory at address 0x7fff00000008
(GDB)

Program received signal SIGSEGV, segmentation fault.


The solution to the problem is just dramatic, because I was written before Movq-4 (%RBP),%RCX not-4! Yes-8, this is MOVQ, register is a 64-bit register! That's it....


The result is a good proof of my idea.

[Email protected]:~/desktop$ gcc-g-C/key.c
[Email protected]:~/desktop$ as-g-O./map.o./map.s
[Email protected]:~/desktop$ gcc-g-O./key.out./key.o./MAP.O
[Email protected] ubuntu:~/desktop$./key.out > Temp

0:^@
1:^[
2:1
3:2
4:3
5:4
6:5
7:6
8:7
9:8
10:9
11:0
12:-
13:=
14:^?
15:
16:q
17:w
18:e
19:r
20:t
21:y
22:u
23:i
24:o
25:p
26:[
27:]

77:^@
78:+
79:^@
80:^@
81:^@
82:^@
83:^@
84:^@
85:^@
86:<
87:^@
88:^@
89:^@
90:^@
91:^@
92:^@
93:^@
94:^@
95:^@
96:^@








Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.