Click to See Complete Forum and Search --> : What's wrong with this code?
Apollyon
August 7th, 2006, 07:53 PM
I'm getting some errors from MASM32 which I have no idea what they mean:
Assembling: C:\masm32\sample.asm
C:\masm32\sample.asm(10) : error A2008: syntax error : ,
Here's my code:
.486
.model flat
.code
_sample proc
push ebp
mov ebp,esp
sub esp,8
push eax
mov eax,[ebp+4]
mul eax,[ebp+8]
add eax,10
pop eax
mov esp,ebp
pop ebp
ret
_sample endp
end _sample
I'm using MASM32 on WinXP, and this ASM code is designed to be called from C++ code.
Thanks for the help.:)
Krishnaa
August 7th, 2006, 08:16 PM
The MUL instruction dont take 2 arguments, it multiplies the argument to EAX, as EAX is default used for multiplication, it puts the answer in EAX:EDX.
so to fix your code you need to remove eax from mul instrcution, as follows,
push ebp
mov ebp,esp
sub esp,8
push eax
mov eax,[ebp+4]
mul [ebp+8]
add eax,10
pop eax
mov esp,ebp
pop ebp
ret
Apollyon
August 7th, 2006, 09:00 PM
Now it's saying:
C:\masm32\sample.asm(11) : error A2023: instruction operand must have size
Code:
.486
.model flat
.stack
.code
_sample proc
push ebp
mov ebp,esp
sub esp,8
push eax
mov eax,[ebp+4]
mul [ebp+8]
add eax,10
pop eax
mov esp,ebp
pop ebp
ret
_sample endp
end _sample
What does that mean?
Krishnaa
August 7th, 2006, 09:12 PM
try this,
push ebp
mov ebp,esp
sub esp,8
push eax
mov eax,[ebp+4]
mul DWORD PTR[ebp+8]
add eax,10
pop eax
mov esp,ebp
pop ebp
ret
MASM needs to know the size of operand.
Apollyon
August 7th, 2006, 09:25 PM
It works. What does "dword ptr" mean, though? And, how do I link this to a C++ program? I keep getting [Linker error] undefined reference to `sample' errors.
Krishnaa
August 7th, 2006, 09:38 PM
The MASM needs to know what size the pointer point to, thats what DWORD PTR specifies.
The assembly code can be used with c++ code by compiling assembly code into obj file and then specifying this obj file in linkers list and using the functions from it like you do for dll's.
You will need to put c++ equivalent syntax of assembly routines/funtions.
BTW what are you trying to do ? is that a code you got from somewhere ?
Apollyon
August 7th, 2006, 10:26 PM
Yeah, I got it from a site, but I don't remember what. It's supposed to compute a=a*b+10. And I DID put the .obj file in my project list, but it still keeps saying it's undefined. How do I use a DLL, b/c I've never used one.
Here'ss the C++ code:
#include <iostream>
#include <cstdlib>
using std::cout;
using std::endl;
using std::system;
extern "C" {int sample();}
int main()
{
cout << sample() << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Krishnaa
August 7th, 2006, 10:37 PM
I am not sure, but you could try this,
#include <iostream>
#include <cstdlib>
using std::cout;
using std::endl;
using std::system;
extern "C" int _sample();
int main()
{
cout << _sample() << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Apollyon
August 7th, 2006, 11:13 PM
Tried that too. Didn't work. Maybe an ASM god could visit us here, and help us out. Anyone else willing to give a try?
Calculator
August 9th, 2006, 10:48 AM
Don't push/pop eax, there's nothing there.
How did it not work?
Linker errors?
Compilation errors?
Human error?
It looks like your routine expects two arguments. Do that.
Krishnaa
August 9th, 2006, 12:00 PM
Can you post your vc project with that obj file ?
Apollyon
August 9th, 2006, 04:57 PM
I'm using Dev-Cpp. How do I post my proj file using that?
Krishnaa
August 9th, 2006, 05:13 PM
Are you sure dev-cpp support using obj files from masm32 ?
I know VC++ accepts obj files from masm.
Apollyon
August 9th, 2006, 06:30 PM
On this subject, Dev-Cpp's faq is useless.
nolxev
August 12th, 2006, 04:27 AM
You should read compiler documentation (not IDE one) to understand how to link C++ and ASM code, I remember DevCpp uses gcc/MingW so have a search about that.
kahlinor
August 23rd, 2006, 03:32 AM
Dev-Cpp (and MinGW) use binutils - the 'nix lineup: AS, AR, LD, etc.
A COFF object won't link. There might be a conversion utility somewhere, I don't know.
Meanwhile, try writing the code in AS using intel-syntax option.
If you have a high-speed connection, try Microsoft's free offering of Visual C which works fine with MASM.
codeguru.com
Copyright WebMediaBrands Inc., All Rights Reserved.