![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: Helltown
Posts: 162
Rep Power: 4
![]() |
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! |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: Helltown
Posts: 162
Rep Power: 4
![]() |
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;
}
__________________
Spread your wings and fly! Chicken! |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 popsWhen 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 |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: Helltown
Posts: 162
Rep Power: 4
![]() |
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! |
|
|
|
|
|
#6 |
|
Newbie
|
The problem is in a way you call MessageBox:
call MessageBoxA call dword ptr [MessageBoxA]
__________________
Vulnerant omnes, ultima necat. |
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
As I said, this instruction,
*(str+x) -= 0x30;
__________________
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 |
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#9 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
The Edit button is disabled half an hour after you post.
|
|
|
|
|
|
#10 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: Helltown
Posts: 162
Rep Power: 4
![]() |
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! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|