I am reading a book about assembly switch statement, the code has cases/branches when the input n is case: 100, 102, 103, 104, 106. It simplified the jump table by subtracting 1
isn't line 7 suppose to be
jmp *.L7(,%eax)
if the index of jump table is held in%eax
?
Each entry in the jump table is a long
, which is 4 bytes. Hence eax
is scaled by 4.
And why did they changed the number into unsigned in line 5 by doing
ja .L2
?
The point is to exclude any number that's less than 100 and greater than 106. I assume it's obvious how it excludes values greater than 106.
So let's say n
was less than 100, e.g. 99. If we then subtract 100 from that we get -1, which when viewed as an unsigned 32-bit value is 4294967295, which is obviously "above" 6, and the jump to .L2
is taken like it should.
subl $100, %eax ; eax = 99-100 == -1
cmpl $6, %eax ; set flags based on -1 - 6 == -7 => ZF=0 and CF=0
ja .L2 ; jump if ZF=0 and CF=0