What's wrong with this code?
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:
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.:)
Re: What's wrong with this code?
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,
Code:
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
Re: What's wrong with this code?
Now it's saying:
C:\masm32\sample.asm(11) : error A2023: instruction operand must have size
Code:
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?
Re: What's wrong with this code?
try this,
Code:
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.
Re: What's wrong with this code?
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.
Re: What's wrong with this code?
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 ?
Re: What's wrong with this code?
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:
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;
}
Re: What's wrong with this code?
I am not sure, but you could try this,
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;
}
Re: What's wrong with this code?
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?
Re: What's wrong with this code?
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.
Re: What's wrong with this code?
Can you post your vc project with that obj file ?
Re: What's wrong with this code?
I'm using Dev-Cpp. How do I post my proj file using that?
Re: What's wrong with this code?
Are you sure dev-cpp support using obj files from masm32 ?
I know VC++ accepts obj files from masm.
Re: What's wrong with this code?
On this subject, Dev-Cpp's faq is useless.
Re: What's wrong with this code?
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.
Re: What's wrong with this code?
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.