Borland x86 inlined assembler; get a label's address?

前端 未结 12 874
清酒与你
清酒与你 2021-01-05 03:15

I am using Borland Turbo C++ with some inlined assembler code, so presumably Turbo Assembler (TASM) style assembly code. I wish to do the following:

void foo         


        
12条回答
  •  悲哀的现实
    2021-01-05 03:42

    Here's a possible method:

    // get_address
    // gets the address of the instruction following the call
    // to this function, for example
    //     int addr = get_address (); // effectively returns the address of 'label'
    //   label:
    int get_address ()
    {
        int address;
        asm
        {
            mov eax,[esp+8]
            mov address,eax
        }
        return address;
    }
    // get_label_address
    // a bit like get_address but returns the address of the instruction pointed
    // to by the jmp instruction after the call to this function, for example:
    //     int addr;
    //     asm
    //     {
    //       call get_label_address // gets the address of 'label'
    //       jmp label
    //       mov addr,eax
    //     }
    //     
    //   label:
    // note that the function should only be called from within an asm block.
    int get_label_address()
    {
        int address = 0;
        asm
        {
            mov esi,[esp+12]
            mov al,[esi]
            cmp al,0ebh
            jne not_short
            movsx eax,byte ptr [esi+1]
            lea eax,[eax+esi-1]
            mov address,eax
            add esi,2
            mov [esp+12],esi
            jmp done
        not_short:
            cmp al,0e9h
            jne not_long
            mov eax,dword ptr [esi+1]
            lea eax,[eax+esi+2]
            mov address,eax
            add esi,5
            mov [esp+12],esi
            jmp done
        not_long:
            // handle other jmp forms or generate an error
        done:
        }
        return address;
    }
    int main(int argc, char* argv[])
    {
        int addr1,addr2;
        asm
        {
            call get_label_address
            jmp Label1
            mov addr1,eax
        }
    
        addr2 = get_address ();
    Label1:
        return 0;
    }
    

    It's a bit hacky but it works in the version of Turbo C++ that I have. It almost certainly is dependant on the compiler and optimisation settings.

提交回复
热议问题