ATT syntax.
I’m trying to understand a practice problem we talked about in class.
We were given the following partial assembly code for a switch statement:
movl 8(%ebp), %eax
addl $2, %eax
cmpl $6, %eax
ja .L2
jmp *.L8(,%eax,4)
//rest of switch statement would go here
.L8
.long .L3
.long .L2
.long .L4
.long .L5
.long .L6
.long .L6
.long .L7
I know that the second portion is the jump table. What I can’t figure out is how to calculate the original C-level switch case values. How can I do this?
Analyze the code.
The function’s parameter + 2 is used as an index into the table. So, the constants start at -2 and go on to -1, 0, etc.
The maximum constant is 4, see there’s a check for parameter + 2 > 6, or, equivalently, for parameter > 4. The number of elements in the table reflects that too.