Sign in to follow this  
Glak

help with nasm please

Recommended Posts

(note: I originally posted this in the beginners' forum but I got no responses and so I am figuring that this forum might be better as assembly is generally considered a more advanced topic) I'm trying to do some assembly programming so my first goal is to be able to call an assembly function from C++. Here is what I have so far: test.cpp
#include <iostream>
using namespace std;

extern "C" int TimesThree(int i);

int main()
{
	int x=5;
	x=TimesThree(x);
	cout<<x<<endl;

	return 0;
}

tt.asm:
global _TimesThree

_TimesThree:
	push	ebp
	mov	ebp, esp
	mov	eax, [ebp+8]
	imul	eax, [eax], 3
	leave
	ret

so what I do is: nasm -f obj tt.asm cl /EHsc test.cpp tt.obj it gives a warning about converting OMF to COFF but if I try to fix that by changing nasm's output format to coff it doesn't work (unresolved function). So anyway an exe is produced but it always crashes. I am assuming that it is in the assembly code but I think that I followed the description of how to do the C calling convention in the nasm docs pretty well Well if anyone knows how to fix this please help. Thank you

Share this post


Link to post
Share on other sites
It's worth your while rechecking your original topic. You got information as to why it's not working, as well as a pointer or two.

There's nothing wrong with the code per se - NASM and Microsoft disagree on the interpretation of the COFF format.

[Edited by - TheUnbeliever on March 19, 2007 5:19:09 PM]

Share this post


Link to post
Share on other sites
thank you to everyone who helped, I finally got it working tonight. I'm just posting this update so that if anyone else is having this problem you will know that someone out there has solved it. The working configuration ended up being nasm with djgcc. Djgcc is superior to MinGW in terms of how easy it is to install and use. The real problem that I was having is that I didn't put segment .text at the beginning of my file. Yep, something that simple. None of the example code that I saw had it, not even the code in the nasm help file. I can't believe that none of the tutorials mentioned this essential fact. I can't believe nasm didn't just spit out a warning saying "segment not specified, assuming text". One guy's tutorial had it though, Paul Carter (if you google his name the tutorial will probably come up). I don't think he even explained it or if he did it got lost somewhere in there (as it is actually a book and not just about assembly). So anyway I figured that I would throw it in my code to make things work and it did.

Before that I tried giving up on the whole assembler. I figured that I would just generate machine code with an API (afterall, I will be generating the assembly with an API) so I kept compiling a .c file with visual studio and looking at it with a hex editor and disassembler. I figured out most of it but despite finding the length field of the actual code portion in two places modifying it made no difference, it wouldn't work if I inserted or removed instructions. Those four bytes near the end (or maybe the ones in front, though they seem to be a timestamp) are probably a checksum or something.

Ok this is a bit of a ramble, but I am just so relieved and I want to give hope to those out there who try this. (Also there was a bug in my code, I was dereferencing an int) Thanks again

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this