這個想法是微不足道的。如果你不知道夠不夠用匯編語言,嘗試理解以下等價的C代碼(我花了定義流程的自由_ *()作爲印刷不同的字母和主拋()):
#include <stdio.h>
void Process_A(void)
{
printf("A\n");
}
void Process_B(void)
{
printf("B\n");
}
void Process_C(void)
{
printf("C\n");
}
void Process_D(void)
{
printf("D\n");
}
typedef struct
{
char Char;
void (*Subroutine)(void);
} CaseTableEntry;
CaseTableEntry CaseTable[] =
{
{ 'A', &Process_A }, // equivalent to "BYTE 'A'" + "DWORD Process_A"
{ 'B', &Process_B },
{ 'C', &Process_C },
{ 'D', &Process_D }
};
void Process(char Char)
{
const size_t NumberOfEntries = sizeof(CaseTable)/sizeof(CaseTableEntry);
CaseTableEntry* entry = &CaseTable[0]; // equiv to "mov ebx, CaseTable"
size_t count = NumberOfEntries; // equiv to "mov ecx, NumberOfEntries"
do
{
// "L1:" would be here
if (entry->Char == Char) // equiv to "cmp al,[ebx]" + "jne L2"
{
entry->Subroutine(); // equiv to "call PTR [ebx + 1]"
break; // equiv to "jmp L3"
}
// "L2:" would be here
entry++; // equiv to "add ebx, EntrySize"
} while (--count > 0); // equiv to "loop L1"
// "L3:" would be here
}
int main(void)
{
Process('A');
Process('B');
Process('X');
Process('C');
Process('D');
return 0;
}
輸出:
A
B
C
D
唯一的問題在這裏可以之類的東西$
和mov ebx, CaseTable
。
$ evaluates to the assembly position at the beginning of the line containing the expression; so you can code an infinite loop using JMP $.
因此EntrySize = ($ - CaseTable)
計算表的第一條目的大小,同樣NumberOfEntries = ($ - CaseTable)/EntrySize
首先計算整個表的大小,然後通過一個入口的給你表的條目數的大小劃分它。
與其他彙編程序(例如MASM和TASM)不同,在NASM mov ebx, CaseTable
中意味着將加載到ebx中的名稱爲CaseTable
的對象的地址。在其他彙編程序中,這可能意味着從名爲CaseTable
的對象的前四個字節讀入ebx。 同樣,DWORD Process_A
定義了一個DWORD,其中包含名爲Process_A
的對象的地址。
在其他組裝商中,等效物可能需要寫成mov ebx, OFFSET CaseTable
和DWORD OFFSET Process_A
。
其餘的,請參考您的書,the official NASM documentation和Intel's/AMD's x86 CPU手冊。基本上做你的功課。如果有什麼不清楚的地方,請問具體的問題。
如果你熟悉C可能是想起來更有幫助是'struct'而不是一個二維數組。 'struct {char lookup; function_ptr_t進程; } CaseTable [] = {{'A',Process_A},/ * ... * /};''#define NumberOfEntries(sizeof(CaseTable)/ sizeof(CaseTable [0]))' – user786653
順便說一句,二維數組。它是一組元素,每個元素都有它自己的兩個元素。數組通常具有相同大小的元素。 「BYTE'A'」和「DWORD Process_A」不會。 –