2009-10-01 54 views
1

我想在C代碼中使用匯編使用C變量。 我的代碼如下所示:內聯彙編在C:INT命令和C變量

__asm { INT interruptValue }; 

其中「interruptValue」是一個變量,我從用戶那裏得到(例如15或15小時)。 當我嘗試編譯,我得到:

Assembler error: 'Invalid instruction operands'

我不知道什麼是正確的類型interruptValue。我試了很久\ int \ short \ char \ char *但是他們都沒有工作。

回答

8

INT操作碼不允許指定變量(寄存器或內存)作爲參數。你必須使用一個常量表達式,如INT 13h

如果你真的想調用變量中斷(並且我不能想象任何情況這樣做),請使用類似switch語句的內容來決定使用哪個中斷。

事情是這樣的:

switch (interruptValue) 
{ 
    case 3: 
     __asm { INT 3 }; 
     break; 
    case 4: 
     __asm { INT 4 }; 
     break; 
... 
} 

編輯:

這是一個簡單的動態形式給出:

void call_interrupt_vector(unsigned char interruptValue) 
{  
    //the dynamic code to call a specific interrupt vector 
    unsigned char* assembly = (unsigned char*)malloc(5 * sizeof(unsigned char)); 
    assembly[0] = 0xCC;   //INT 3 
    assembly[1] = 0x90;   //NOP 
    assembly[2] = 0xC2;   //RET 
    assembly[3] = 0x00; 
    assembly[4] = 0x00; 

    //if it is not the INT 3 (debug break) 
    //change the opcode accordingly 
    if (interruptValue != 3) 
    { 
     assembly[0] = 0xCD;    //default INT opcode 
     assembly[1] = interruptValue; //second byte is actual interrupt vector 
    } 

    //call the "dynamic" code 
    __asm 
    { 
     call [assembly] 
    } 

    free(assembly); 
} 
+2

作爲一個有趣的練習,你很可能中斷使用自修改代碼編寫一個變量,您只需將INT指令的第二個字節的值更改爲您想要的任何中斷。 – Falaina 2009-10-01 14:59:29

+1

@Falaina:你需要注意INT 3,因爲它有不同的操作碼,但是聽起來不錯。 – 2009-10-01 15:23:03

+0

@Falina:我如何修改代碼?我能想到的只是將指令放在一個不同的函數中,然後用函數地址的偏移量來改變一些字節。 – Eldad 2009-10-04 16:39:56