Switch Case Assembly Language

Catie picture Catie · Oct 24, 2010 · Viewed 22.3k times · Source

I am looking at the assembly language code of a switch statement.

I understand how the code works and what the cases are. My question is how do I decide on the case names?

Below is the assembly language code, which will be followed with my interpretation of it. I basically just need to use the jump table and fill in the case names.

    1 8048420: push %ebp
    2 8048421: mov %esp, $ebp
    3 8048423: mov 0x8(%ebp), %eax       // x
    4 8048426: mov 0xc(%ebp), %edx       // n
    5 8048429: sub $0x32, %edx           // so least value of case is 32
    6 804842c: cmp $0x5, %edx            // max value is 37
    7 804842f: ja 8048448 <switch+0x28>  // if >37, go to default
    8 8048431: jmp *0x80485d0(, %edx, 4)  //THIS RIGHT HERE ?
    9 8048438: shl $0x2, %eax             // CASE A
   10 804843b: jmp 804844b <switch+0x2b>  //break;
   11 804843d: sar $0x2, %eax             //CASE B
   12 8048440: jmp 804844b <switch+0x2b>  //break
   13 8048442: lea (%eax, %eax, 2), %eax  //CASE C
   14 8048445: imul %eax, %eax     
   15 8048448: add $0xa, %eax             //fall through to default
   16 804844b: pop %ebp                   //return
   17 804844c: ret

The jump table that the gdb command creates: I am doing x/6w 0x80485d0

0x80485d0: 0x08048438 0x08048448 0x08048438 0x0804843d
0x80485e0: 0x08048442 0x08048445

My interpretation:

int result = x;
switch(n) {
case __:
    x = x << 2;
    break;  
case __:
    x = x >> 2
    break;
case __:
    x = 4*x;
    x = x*x
case __: //default
    x += 0xa 
return x;
}

I just don't understand how to look up the jump table and decide which values of n between 32 and 37 fit in which of the case blanks.

Any help would be appreciated. Thank you.

Answer

outis picture outis · Oct 24, 2010

As Oli says, there's not much more to do. n-50 is stored in %edx, then switch+0x11 jumps to the address stored in 0x80485d0 + %edx * 4. Looking at the table, that's switch+0x18 when n==50 or 52, switch+0x28 when n==51, switch+0x1d when n==53, switch+0x22 when n==54 and switch+0x25 when n==55.