NASM Forum > Programming with NASM

Substitute arguments

(1/1)

Flybro:
Hi guys,

I'm trying to re-use disassembled code as a function called from .cpp but because there is a plenty of arguments inside I have question about: how can I quickly substitute all of them?

The "arg_0   dd 8" doesn't works in this case, after executing arg_0 has some random value and application crash.

> 1. nasm -f macho assembler.nasm
> 2. g++ -m32 -Wall main.cpp assembler.o -o application.exe

For example:
main.cpp

--- Code: ---#include <iostream>
#include "functions.h"

using namespace std;

int main() {

    const char* customer = "FLYBRO";

    begood(customer);
    cout << "Customer: " << hex << uppercase << customer << endl;
    return 0;
}

--- End code ---

functions.h

--- Code: ---extern "C" void begood(const char *customer);

--- End code ---

assembler.nasm

--- Code: ---global _begood

        section .data
        arg_0   dd 8

        section .text

_begood:

        push    ebp
        mov     ebp, esp
        sub     esp, 30h

        mov     edi, [ebp + arg_0]
        ......
       
        add     esp, 30h
        mov     esp, ebp
        pop     ebp
        ret

--- End code ---


Cheers,
Flybro

Frank Kotler:
Hi Flybro,

I think you'll find that arg_0 doesn't have a "random" value, but is the memory address where the variable arg_0 is stored. You want 8, the "[contents]" of arg_0... If there were such an instruction:


--- Code: ---mov edi, [ebp + [arg_0]]

--- End code ---
but there is no such instruction. So you could do:


--- Code: ---...
mov eax, [arg_0]
mov edi, [ebp + eax]
...

--- End code ---
or, simpler and more usual...

--- Code: ---...
mov edi, [ebp + 8]
mov esi, [ebp + 12] ; "arg_1", if we had one...
...

--- End code ---

I'm glad to see you've got:

--- Code: ---extern "C" void begood(const char *customer);

--- End code ---
figured out... to keep C++ from "decorating" (mutilating) your function name (other than prepending the underscore).

That's untested - haven't got a Mac, and I'm not very familiar with C++, but I think it'll work...

Best,
Frank

Flybro:

--- Quote from: Frank Kotler on January 16, 2012, 11:23:36 AM ---I think you'll find that arg_0 doesn't have a "random" value, but is the memory address where the variable arg_0 is stored.
--- End quote ---
You're right, my bad  :-[.


--- Quote ---...
mov edi, [ebp + 8]
mov esi, [ebp + 12] ; "arg_1", if we had one...
...

--- End quote ---
That's how I did it so far and basically I'm fine with that.



--- Quote ---figured out... to keep C++ from "decorating" (mutilating) your function name (other than prepending the underscore).

That's untested - haven't got a Mac, and I'm not very familiar with C++, but I think it'll work...
--- End quote ---

Oh yeah, it works very well. Actually, my primary language is Objective-C and have to say that objects made by nasm are perfect match with Objective-C apps compiled by LLVM GCC 4.2 under XCode.

btw. I have one spare iMac 21 (intel) perhaps you are interested in for developing some stuff on mac platform, just send me a note.

Cheers,
Flybro

Navigation

[0] Message Index

Go to full version