我在理解如何通過彙編語言確定例50,52等方面有點麻煩。彙編代碼和開關語句的情況
據我所知,跳轉表對應於在每種情況下要執行的操作,並且檢查edx> 5是否意味着案例的範圍是從0到5?我相信1被遺漏,因爲這是默認情況,但爲什麼5被遺漏?
我覺得應該有個案55:where result * = result,no?
如果有人能幫忙解釋一下,那會很棒。謝謝!
int switch_prob(int x) { int result = x; switch (x) { case 50: case 52: result <<= 2; break; case 53: result >>= 2; break; case 54: result *= 3; break; default: result += 10; } return result; }
圖3.38示出了用於該過程的拆卸對象的代碼。我們只對 感興趣的是第4行至第16行所示的代碼部分。我們可以在第4行看到 參數x(相對於%ebp的偏移8)被加載到寄存器%eax中,對應於 程序變量結果。的「LEA爲0x0(%ESI),%ESI」第11行的指令是一個NOP 指令插入,使上12行開始的指示上一個地址,它是一個 倍數的16
跳轉表駐留在不同的記憶區域。使用調試器GDB 我們可以檢查從地址0x8048468開始的六個4字節的內存字與 命令x/6w 0x8048468。 GDB打印的情況如下:
(gdb) x/6w 0x8048468: 0x080483d5 0x080483eb 0x080483d5 0x0x80483e0 0x8048478: 0x080483e5 0x080483e8 (gdb)
彙編代碼:
1: 080483c0 <switch_prob>: 2: 80483c0: push %ebp 3: 80483c1: mov %esp,%ebp 4: 80483c3: mov 0x8(%ebp),%eax // X is copied into eax ; eax = x 5: 80483c6: lea 0xffffffce(%eax),%edx // placeholder 6: 80483c9: cmp $0x5, %edx // Compare edx (3rd argument) with 5; Edx - 5 // clearly, edx is x 7: 80483cc: ja 80483eb <switch_prob+0x2b> // if edx - 5 > 0, Jump into line 16 (default) 8: 80483ce: jmp *0x8048468(,%edx,4) // Go into the jump table 9: 80483d5: shl $0x2, %eax // eax << 2 10: 80483d8: jmp 80483ee <switch_prob+0x2e> // Jump to line 17 11: 80483da: lea 0x0(%esi),%esi // esi = esi NOP. Filling in N bytes 12: 80483e0: sar $0x2, %eax // eax >> 2 13: 80483e3: jmp 80483ee <switch_prob+0x2e> // Jump to line 17 14: 80483e5: lea (%eax, %eax, 2), %eax // eax = eax + 2(eax) 15: 80483e8: imul %eax, %eax // eax *= eax 16: 80483eb: add $0xa, %eax // eax += 10 17: 80483ee: mov %ebp, %esp // esp = ebp 18: 80483f0: pop %ebp 19: 80483f1: ret
那麼你確實有五個交換機的情況。 –
因爲情況5對應於0x80483e8,我不應該有6嗎? @JoachimPileborg – Crowning
從零開始計數,從'0'到'5'(包括)有六個數字。 :) –