#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    149
    Rep Power
    36

    Homework: Jump Tables and Switch Cases x86 64


    I'm stuck on a problem where I have to provide x86 code for a switch statement given in C. My instructor gives an example in one of his lectures, but I am having difficulty understanding this example. We use Red Hat Linux for this course.



    In the first section (which is red)
    The code checks if a is less than 1, and jumps to the default if it is which makes sense.

    In the second section (which is green)
    The code checks if a is greater than 4, and jumps to the default if it is which makes sense.

    In the third section (which is red)
    .T gets put into register %rbx
    1 is subtracted from the register, %eax, which has a (why?)
    8 is put into %ecx
    the product 8 * (a - 1) is stored into %eax (why?)

    The instruction "movslq" is used, but I do not know what it does. I've used movl and the conditional move instructions, but I'm not quite sure what movslq is and am having a hard time finding this out with google.

    And everything after that point is uncertain to me because I do not understand why some things are being done, for the most part, or what movslq does.
    Attached Images
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    929
    Originally Posted by jakotheshadows
    1 is subtracted from the register, %eax, which has a (why?)
    .T is an array and EAX will be used to index that array. But EAX contains a value in 1..4 instead of starting at 0.

    Originally Posted by jakotheshadows
    8 is put into %ecx
    the product 8 * (a - 1) is stored into %eax (why?)
    The elements of .T are quads (8 bytes long). Presumably your instructor wants to show you explicitly how you index an array rather than using the indexing constructs.

    Originally Posted by jakotheshadows
    The instruction "movslq" is used, but I do not know what it does.
    EAX is a 32-bit register, RAX is a 64-bit register. So you need an instruction that extends a 32-bit value to a 64-bit one for this operation. MOVSLQ is move and sign-extend a value from a 32-bit source to a 64-bit destination.

    Comments on this post

    • jakotheshadows agrees : Thanks for the quick reply!
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);

IMN logo majestic logo threadwatch logo seochat tools logo