Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 4th, 2005, 11:15 PM   #1
rsnd
Hobbyist Programmer
 
rsnd's Avatar
 
Join Date: Jun 2005
Location: Helltown
Posts: 162
Rep Power: 4 rsnd is on a distinguished road
Whats wrong with this inline code?

[PHP]int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{

char * msg = "128\0";
char * msg2 = "Equal\0";
char * msg3 = "NotEqual\0";
MessageBox(0,NULL,NULL,0);
__asm{
mov eax, 0x04;
push eax;
push msg;

call str2Int;

mov ebx, eax;
cmp ebx,128;


je B;

mov ecx, 0x00000000;
push ecx;
push msg3;
push msg;
push ecx;

call MessageBoxA

jmp C;
B:
mov ecx, 0x00000000;
push ecx;
push msg2;
push msg;
push ecx;

call MessageBoxA

C:
}
MessageBox(0,NULL,NULL,0);
return 0;
}[/PHP]
Gives no error messages but it stuffs up when i run it!!!
__________________
Spread your wings and fly! Chicken!
rsnd is offline   Reply With Quote
Old Jul 5th, 2005, 9:08 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I'm compelled to say that your ability to ask a question in a sensible way sucks. If you want good answers, you need to ask good questions. In this milieu, information is key. You don't show much. For instance, you don't show "str2Int", so one can't tell what calling convention it uses. The fact that you don't clean up the stack after calling it indicates __stdcall, but the fact that your program runs off into the weeds and barfs on its shoes (invalid memory access) may be a sign that you neglected to consider the issue.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 5th, 2005, 8:04 PM   #3
rsnd
Hobbyist Programmer
 
rsnd's Avatar
 
Join Date: Jun 2005
Location: Helltown
Posts: 162
Rep Power: 4 rsnd is on a distinguished road
hmmm...I tent to assume im too predictable. Isn't it supposed to use C calling convension by default?
OK I, using VC++6.0...here is the complete code:
#include <windows.h>
//#include "nConsole.h"

int str2Int(char*, int);


int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	
	char * msg = "128\0";
	char * msg2 = "Equal\0";
	char * msg3 = "NotEqual\0";
	MessageBox(0,NULL,NULL,0);
	__asm{
		mov eax, 0x04;
		push eax;
		push msg;

		call str2Int;

		mov ebx, eax;
		cmp ebx,128;


		je B;

		mov ecx, 0x00000000;
		push ecx;
		push msg3;
		push msg;
		push ecx;

		call MessageBoxA

		jmp C;
B:
		mov ecx, 0x00000000;
		push ecx;
		push msg2;
		push msg;
		push ecx;

		call MessageBoxA

C:
	}
	MessageBox(0,NULL,NULL,0);
	return 0;
}


int str2Int(char * str, int len){
    int x, y;
    byte z;
    y = 0;
    for(x=0;x<len;x++){
        *(str+x) -= 0x30;
    }
    z = *(str+x);
    y = z;
    for(x =0;x<len;x++){
        z = *(str+x);
        y *= 10;
        y+= z;
    }
    for(x=0;x<len;x++){
        *(str+x) += 0x30;    
    }
    return y;
}
Thanks
__________________
Spread your wings and fly! Chicken!
rsnd is offline   Reply With Quote
Old Jul 5th, 2005, 10:16 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Your compiler uses the __cdecl calling convention by default. However, your original post didn't indicate whether str2Int was written in C/C++ and could be presumed to be __cdecl, or whether you had written it in assembler, also, which of course has no "calling convention."

The __cdecl convention is that the caller cleans up the stack (removes the parameters that were placed there prior to the call). The advantage to this convention is that one can use a variable number of arguments (ala "printf"); the caller knows how many he pushed, so he knows how many to remove. The called function has no idea. The disadvantage to the method is increased footprint as each caller has to perform cleanup instead of it being incorporated in the called function.

Since you are calling the function from assembly language, it is your responsibility to clean up the stack. You aren't.
        push eax;        // this is the length (args pushed right to left)
        push msg;       // this is the pointer to the message

        call str2Int;     // make the call

        mov ebx, eax;  // eax is the return value, which you are testing
        cmp ebx,128;

// You have nothing to get the parameters off the stack before you proceed
// You need to add a couple of pops
This issue is going to give you problems when you reach the end of the program because the stack pointer is not going to be pointing to the correct return address when you reach the end of main. You will get problems sooner if you add more to the code. THIS IS NOT YOUR CURRENT PROBLEM, HOWEVER.

When you declare a string as follows:

char * msg = "128\0";

the compiler sets aside static memory for the string and stack-memory for a pointer ("msg") and puts the appropriate value in the pointer. It's strictly a favor by the compiler, at compile time, for as you know, one cannot assign a C-style string to an array using the "=" operator. (You don't need to overtly add the terminating zero when you express it as you have; the compiler does THAT for you automatically, also).

It is not universally true that the string is "const" (write-protected), but in your implementation, using your compiler, it is. You are trying to modify the string via the pointer you passed as a parameter. That is a memory access violation. The quick cure is to put the string on the stack:

char msg = "128";

"msg", since it refers to an array, does double duty as a pointer under the circumstances, so you can make the call as you're presently doing. That double duty occassionally will get one in trouble because of the false assumption that an array and a pointer are the same thing, but in this case, you're fine. You may modify it at will; just remember to observe the length requirement which you pass, so as not to trundle off the end into verboten territory.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 6th, 2005, 5:33 AM   #5
rsnd
Hobbyist Programmer
 
rsnd's Avatar
 
Join Date: Jun 2005
Location: Helltown
Posts: 162
Rep Power: 4 rsnd is on a distinguished road
Thanks DaWei. But what was the problem? The str2int function seems fine to me...I think the problem is with the message Box call!!
this crashes:
[php]#include <windows.h>

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
char msg []= "128";
char msg2 []= "Equal";
char msg3 []= "NotEqual";
MessageBox(0,NULL,NULL,0);
__asm{

mov ecx, 0x00;
push ecx;
push msg3;
push msg;
push ecx;

call MessageBoxA

pop eax;
pop eax;
pop eax;
pop eax;
}
MessageBox(0,NULL,NULL,0);
return 0;
}[/php]
__________________
Spread your wings and fly! Chicken!
rsnd is offline   Reply With Quote
Old Jul 6th, 2005, 5:47 AM   #6
omega_red
Newbie
 
omega_red's Avatar
 
Join Date: Jun 2005
Location: Poland
Posts: 5
Rep Power: 0 omega_red is on a distinguished road
Send a message via Yahoo to omega_red
The problem is in a way you call MessageBox:
call MessageBoxA
At assembly level, MessageBoxA is a label to an offset in the Import Table of your exe. It points to a DWORD that contains real address of the API. You need to dereference that address:
call dword ptr [MessageBoxA]
It should work fine.
__________________
Vulnerant omnes, ultima necat.
omega_red is offline   Reply With Quote
Old Jul 6th, 2005, 5:54 AM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
As I said, this instruction,
        *(str+x) -= 0x30;
is attempting to modify read-only memory. You're not getting far enough for the message box call to be a problem, at this point.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 6th, 2005, 6:38 AM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Incidentally, Omega is correct about your need for an indirect call, when you get that far. Big K, I tried to edit this into the above post, but there's no "EDIT" button. ???
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 6th, 2005, 7:35 AM   #9
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
The Edit button is disabled half an hour after you post.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Jul 12th, 2005, 4:38 AM   #10
rsnd
Hobbyist Programmer
 
rsnd's Avatar
 
Join Date: Jun 2005
Location: Helltown
Posts: 162
Rep Power: 4 rsnd is on a distinguished road
Hmmm...ok...that didnot work!!!
[php]#include <windows.h>

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
char msg []= "128";
char msg2 []= "Equal";
char msg3 []= "NotEqual";
MessageBox(0,NULL,NULL,0);
__asm{

mov ecx, 0x00;
push ecx;
push msg3;
push msg;
push ecx;

call dword ptr [MessageBoxA];

pop eax;
pop eax;
pop eax;
pop eax;
}
MessageBox(0,NULL,NULL,0);
return 0;
}
[/php]
__________________
Spread your wings and fly! Chicken!
rsnd is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:21 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC