NASM - The Netwide Assembler
NASM Forum => Programming with NASM => Topic started 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
#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
extern "C" void begood(const char *customer);
assembler.nasm
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
-
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:
mov edi, [ebp + [arg_0]]
but there is no such instruction. So you could do:
...
mov eax, [arg_0]
mov edi, [ebp + eax]
...
or, simpler and more usual...
...
mov edi, [ebp + 8]
mov esi, [ebp + 12] ; "arg_1", if we had one...
...
I'm glad to see you've got:
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
-
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 :-[.
...
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.
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