help with Memory Copy...

Hi' all,

first a bit backgound knowledge to help you aid me :)

i use the yasm assembler (the NASM syntax), and I call this
function from C++ and it is defined as:

[code]extern "C" void GetMem(void*);[/code]

the pointer i an address to a 256 byte array which I intend to
copy to my assembly alocated memory called "intmem".

here's the code I have now, and somehow it mangles the stack
and mess up the return addresses
[code]BITS 32
SECTION .bss
global _GetMem
intmem: times 300 db 0

SECTION .text
_GetMem:
push ebp
mov ebp, esp
sub esp, 0x20
push ecx ; saves the ecx register incase the caller expects
; it to be untouched
push edx ; saves the ecx register incase the caller expects
; it to be untouched
lea edx, [ebp+8]
xor ecx, ecx
mov cl, 0xfc
xor eax, eax
.next:
mov eax, [edx+ecx]
mov [ecx+intmem], eax
sub cx, 0x0003
loop _GetMem.next
mov eax, [edx]
mov [intmem], eax
pop edx
pop ecx
mov esp, ebp
pop ebp
ret
[/code]

can you help me to what I'm doing wrong? and please explain it.

Comments

  • :
    : the pointer i an address to a 256 byte array which I intend to
    : copy to my assembly alocated memory called "intmem".
    :
    : here's the code I have now, and somehow it mangles the stack
    : and mess up the return addresses

    You use "lea edx, [ebp+8]", which is equivalent in C to:
    [code]
    void GetMem(void* pBuffer)
    {
    char** ppBuffer = (char**)&pBuffer;
    // And then you use the value of ppBuffer as a destinationa address:
    ppBuffer[12] = 'x';
    ...
    }
    [/code]
    (Just an example)

    A pointer in C is passed to assembly as a simple integer value on the stack, which is the address of the buffer.
    So "mov edx, [ebp+8]" will do the trick ;)

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • : :
    : : the pointer i an address to a 256 byte array which I intend to
    : : copy to my assembly alocated memory called "intmem".
    : :
    : : here's the code I have now, and somehow it mangles the stack
    : : and mess up the return addresses
    :
    : You use "lea edx, [ebp+8]", which is equivalent in C to:
    : [code]:
    : void GetMem(void* pBuffer)
    : {
    : char** ppBuffer = (char**)&pBuffer;
    : // And then you use the value of ppBuffer as a destinationa address:
    : ppBuffer[12] = 'x';
    : ...
    : }
    : [/code]:
    : (Just an example)
    :
    : A pointer in C is passed to assembly as a simple integer value on
    : the stack, which is the address of the buffer.
    : So "mov edx, [ebp+8]" will do the trick ;)

    sorry for this confusion, the address passed is to an object define like
    this:
    [code]
    class ObjName {
    char array[256];
    public:
    ObjName(){};
    ~ObjName(){};
    };
    [/code]

    and it's the address of array I need, to copy the data there
    to my assembly block.

    (I'm considering to just save the address of the array and then use that,
    the memory block is used by a few other functions as a lookup table.
    I'd like to get this copy method right though.)

    I've tried to change the instruction:
    [code] lea edx, [ebp+8][/code]
    with
    [code] mov edx, [ebp+8][/code]

    but the problem still persists.
    -- as life grows older, I gain experience.
  • :
    : sorry for this confusion, the address passed is to an object define
    : like
    : this:
    : [code]:
    : class ObjName {
    : char array[256];
    : public:
    : ObjName(){};
    : ~ObjName(){};
    : };
    : [/code]:
    :
    : and it's the address of array I need, to copy the data there
    : to my assembly block.

    That does not change what I said... but it does complicate things. The mov is still the way to go, but not in this form.

    :
    : (I'm considering to just save the address of the array and then use
    : that,
    : the memory block is used by a few other functions as a lookup table.
    : I'd like to get this copy method right though.)

    Yeah, please do that. It's very hard to access the array from pure assembly because - unlike C++ - the assembler does not know about structures and classes, so you'll have to manually calculate the position of 'array' within the class.
    Now it looks pretty transparent now (offset 0) but it gets complicated: 1) there's a VTABLE at offset 0 if there are virtual functions involved (either in the base class or in the derived) and 2) If you change one little thing - like add 'int x' before 'array' - the code won't work anymore.
    :
    : I've tried to change the instruction:
    : [code]: lea edx, [ebp+8][/code]:
    : with
    : [code]: mov edx, [ebp+8][/code]:
    :
    : but the problem still persists.
    : -- as life grows older, I gain experience.
    The code works now for when &([italic]ObjName[/italic].array) is passed to the functions.

    If you insist on using the object instead of the array, you could build a simple wrapper:
    [code]
    void wrapper(ObjName* obj)
    {
    GetMem( obj->GetArrayAddress() );
    }
    [/code]

    Anyway, good luck. If there are any further problems, I'll be glad to help.

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • Great thanks for the bit of insight,

    I think I'll try to make that get address method
    for my object and see how that works out.

    Regards
    Ren
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories