Store "JMP [EAX*4+ESP]

From: Bryan Parkoff (bryan.nospam.parkoff_at_nospam.com)
Date: 01/21/04


Date: Wed, 21 Jan 2004 09:04:45 +0000 (UTC)


    You know that I am doing clever by placing Pointer Address into Stack
Segment instead of Data Segment. I allow switch (xx) & case 0 through 4 to
access into Stack Segment. Look at my example below.

inline __declspec (naked) VOID __Class::Test(VOID) CONST

{

      __asm

      {

            sub esp, 14H

            mov eax, offset __0

            mov dword ptr [esp], eax

            mov eax, offset __1

            mov dword ptr [esp+4h], eax

            mov eax, offset __2

            mov dword ptr [esp+8h], eax

            mov eax, offset __3

            mov dword ptr [esp+0ch], eax

            mov eax, offset __4

            mov dword ptr [esp+10h], eax

            mov eax,0

            cmp eax,4

            ja __Exit

            jmp dword ptr [eax*4+esp]

            __0:

            mov edx, 0

            jmp __Exit

            __1:

            mov edx, 1

            jmp __Exit

            __2:

            mov edx, 2

            jmp __Exit

            __3:

            mov edx, 3

            jmp __Exit

            __4:

            mov edx, 4

            __Exit:

            add esp, 14H

            ret

      }

}

    My example code above is INSIDE C++ class!! Here is the initalization
below.

            mov eax, offset __0

            mov dword ptr [esp], eax

            mov eax, offset __1

            mov dword ptr [esp+4h], eax

            mov eax, offset __2

            mov dword ptr [esp+8h], eax

            mov eax, offset __3

            mov dword ptr [esp+0ch], eax

            mov eax, offset __4

            mov dword ptr [esp+10h], eax

    Each __? is a pointer to "case ?". They are done at initalization. I
will consider to move initialization to another function later because my
function above can be slow if initialization must be performed each time.

            mov eax,0

            cmp eax,4

            ja __Exit

            jmp dword ptr [eax*4+esp]

    Notice "JMP DWORD PTR [eax*4+esp]"? I will consider to rename from it
to "JMP DWORD PTR [eax*4+this]" later because C++ class is able to handle
huge stack segment at initialization time and modification time.
Initialization time is always fixed for pointer address and variable, but
modication time can be used to change the variable -- not fixed pointer
address.
    The problem is that inline assembler in C/C++ is not flexible like MASM.
You can't place JMP [eax*4+0xxxxxxxxH] in __asm blocks, but MASM can however
I can be able to overcome __asm's limitation.
    I prefer to use __asm blocks under C/C++ so I can be able to mix
assembly code and C++ code together. I don't like to work MASM that it
can't be mixed with C++. (I mean one MASM module and one C++ module, but my
example code above has only ONE module to MIX assembly code and C++ code).
    Does it make sense? I do know that it can create a huge bugs, but it is
too easy for me to locate bugs. Not very bad for me. I do realize that
__asm feature will be removed from C/C++ next version, but I decide to
remain on my current version for over 10 to 20 years because I don't need to
worry about the __asm's and C/C++ compiler's improvement.
    Please state your opinion what you think my code. I must be able to
construct C++ class from assembly language using MASM, but it can be very
challenge!!

-- 
Bryan Parkoff


Relevant Pages

  • Re: Store "JMP [EAX*4+ESP]
    ... > huge stack segment at initialization time and modification time. ... > Initialization time is always fixed for pointer address and variable, but> modication time can be used to change the variable -- not fixed pointer> address. ... I don't like to work MASM that it> can't be mixed with C++. ... (I mean one MASM module and one C++ module, but my> example code above has only ONE module to MIX assembly code and C++ code). ...
    (comp.lang.asm.x86)
  • Strict Pointer Aliasing Question
    ... I create one 32 Bits variable and four pointer variables. ... but global object can remain in data segment ... I understand that store objects in stack segment is ...
    (comp.lang.cpp)
  • Re: Initialising a pointer
    ... Pointers to stack segment are not really a good idea ... return ptr; ... the function returns a pointer. ... as the content lies on the stack. ...
    (comp.lang.c)
  • Re: Initialising a pointer
    ... Pointers to stack segment are not really a good idea ... No, it returns a pointer to a string literal, which isn't an automatic ... return ptr; ... Ian Collins. ...
    (comp.lang.c)