Click to See Complete Forum and Search --> : some c code puzzle me..


muyu
04-08-2003, 07:32 AM
static char hs_jump[CODESIZE] =
"\xb8\x00\x00\x00\x00" /* movl $0,%eax */
"\xff\xe0" /* jmp *%eax */
;
and
void (*handle_scancode) (unsigned char, int) =
(void (*)(unsigned char, int)) HS_ADDRESS;

// it is two section code above, i hasn't ever seeing and isn't understanding it, who can help me , thanks you.


(upper code it's from below)
static struct semaphore hs_sem, log_sem;
static int logging=1;

#define CODESIZE 7
static char hs_code[CODESIZE];
static char hs_jump[CODESIZE] =
"\xb8\x00\x00\x00\x00" /* movl $0,%eax */
"\xff\xe0" /* jmp *%eax */
;

void (*handle_scancode) (unsigned char, int) =
(void (*)(unsigned char, int)) HS_ADDRESS;

void _handle_scancode(unsigned char scancode, int keydown)
{
if (logging && keydown)
log_scancode(scancode, LOGFILE);


down(&hs_sem);

memcpy(handle_scancode, hs_code, CODESIZE);
handle_scancode(scancode, keydown);
memcpy(handle_scancode, hs_jump, CODESIZE);

up(&hs_sem);
}

michaelk
04-08-2003, 08:38 AM
To make things clear as mud...

Originally posted by muyu
static char hs_jump[CODESIZE] =
"\xb8\x00\x00\x00\x00" /* movl $0,%eax */
"\xff\xe0" /* jmp *%eax */ ;
#define CODESIZE 7

The character string hs_jump length is being assigned 7 characters. The string "\xb8..." is the assembled instruction code for the assembly language you see in the comment i.e /* movl $0,%eax */. Each character is defined as a hex byte \xHH. The \ is a special string character.
If you use special format characters for printing etc. i.e. \t which is the tab character or ASCII value=9. Remember that char strings are nothing more than an array of numbers.


void (*handle_scancode) (unsigned char, int) =
(void (*)(unsigned char, int)) HS_ADDRESS;
[/B]

I am still working on this one. So what does does this program do?

bwkaz
04-08-2003, 10:10 AM
First off, since this is a programming question, here it is, in the Programming forum. ;)

Originally posted by muyu
void (*handle_scancode) (unsigned char, int) =
(void (*)(unsigned char, int)) HS_ADDRESS; You're declaring a variable (named handle_scancode), whose type is "pointer to a function that takes two parameters, the first an unsigned char, the second an int, and returns void".

Then, you set it equal to HS_ADDRESS, which must be the address of something useful. The stuff before HS_ADDRESS is just a cast, so that the compiler doesn't complain -- you're casting the HS_ADDRESS (which probably is of type int or unsigned int) to, again, "a pointer to a function that takes two parameters, an unsigned char and an int, and returns void".

muyu
04-09-2003, 11:05 PM
certainly, here it is. thanks so much.

throught you explained in detail, i see.
but,
"\xb8\x00\x00\x00\x00" /* movl $0,%eax */
"\xff\xe0" /* jmp *%eax */
;
How can it be implemented?
OH,HS_ADDRESS=0x$($(shell ksyms -a | grep handle_scancode | awk '{print $1}')), Is this address function entry point?

bwkaz
04-10-2003, 10:13 AM
Yes, it is. ksyms -a prints the name and address of each symbol in the currently-running kernel, and the awk prints the first field (which is the address).

As for your other question, I'm not sure what you mean...

binaryDigit
04-10-2003, 10:26 AM
usually that particular address is reserved for __start. (well it's __start if you look at the .text section of the code)
looks like the program is actually doing a jump back to start after the keydown variable goes to zero (IIRC gcc examines the .text section when looking at a line of code like that). that's about all i can tell you, because that's about all i understand. (and even then you can see i don't know much) :D