NASM - The Netwide Assembler

NASM Forum => Programming with NASM => Topic started by: Flybro on January 16, 2012, 03:41:41 AM

Title: Substitute arguments
Post by: Flybro on January 16, 2012, 03:41:41 AM
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: [Select]
#include <iostream>
#include "functions.h"

using namespace std;

int main() {

    const char* customer = "FLYBRO";

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

functions.h
Code: [Select]
extern "C" void begood(const char *customer);

assembler.nasm
Code: [Select]
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


Cheers,
Flybro
Title: Re: Substitute arguments
Post by: Frank Kotler on January 16, 2012, 11:23:36 AM
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: [Select]
mov edi, [ebp + [arg_0]]
but there is no such instruction. So you could do:

Code: [Select]
...
mov eax, [arg_0]
mov edi, [ebp + eax]
...
or, simpler and more usual...
Code: [Select]
...
mov edi, [ebp + 8]
mov esi, [ebp + 12] ; "arg_1", if we had one...
...

I'm glad to see you've got:
Code: [Select]
extern "C" void begood(const char *customer);
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

Title: Re: Substitute arguments
Post by: Flybro on January 17, 2012, 11:46:14 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.
You're right, my bad  :-[.

Quote
...
mov edi, [ebp + 8]
mov esi, [ebp + 12] ; "arg_1", if we had one...
...
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...

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