News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Inline assembly and calling API functions

Started by Vortex, February 28, 2022, 01:06:44 AM

Previous topic - Next topic

Vortex

In the inline asm block, the API function MessageBox should be enclosed between square brackets otherwise wrong call to API will lead to application crash :

// Source code built with PellesC 11.00.2

#include <stdio.h>
#include <windows.h>
char t1[]="This is a test1.";
char t2[]="This is a test2.";

int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, WCHAR *pszCmdLine, int nCmdShow)
{
__asm{

push 0
push OFFSET t1
push OFFSET t1
        push 0
call DWORD PTR MessageBox
}

MessageBox(0,t2,t2,0);
return 0;
}


Edit : The square brackets are removed from the call to MessageBox, no any crash after compilation. Thanks to Hutch.

hutch--

Hi Erol,

My C is very rusty but I wonder if you could write a C prototype so you did not have to enclose the MessageBox in square brackets ?

Vortex

Hi Hucth,

Thanks, I removed the square brackets and the recompiled code didn't crash. Maybe, I was mistaken somewhere.

hutch--

That makes sense, it means that Pelle has followed the same approach as Microsoft with MASM where the square brackets are only taken notice of if its a register address notation. I confess I always hated compulsory square brackets as TASM and a few others did.

jj2007

Quote from: hutch-- on May 31, 2022, 07:31:07 PMI confess I always hated compulsory square brackets as TASM and a few others did.

FreeBasic inline assembly, for example, needs them. About as useful as the ; behind each C/C++ line :sad:

Greenhorn

Quote from: jj2007 on June 01, 2022, 02:25:47 AM
Quote from: hutch-- on May 31, 2022, 07:31:07 PMI confess I always hated compulsory square brackets as TASM and a few others did.

FreeBasic inline assembly, for example, needs them. About as useful as the ; behind each C/C++ line :sad:

The C/C++ parser does not parse in "lines". You can write your whole code into one line, if you want to. The ; indicates the end of a statement. And the curly brackets {} indicate a code block.
Kole Feut un Nordenwind gift en krusen Büdel un en lütten Pint.

Gunther

Quote from: Greenhorn on June 01, 2022, 04:57:47 AM
The C/C++ parser does not parse in "lines". You can write your whole code into one line, if you want to. The ; indicates the end of a statement. And the curly brackets {} indicate a code block.

Yes, that's the point. By the way, it's similar in PASCAL.

But we've here a schoolmasterish attitude. Anyone who has a different opinion is a flat-earther, Nazi or whatever. That's how far we've come.
You have to know the facts before you can distort them.

jj2007

Quote from: Greenhorn on June 01, 2022, 04:57:47 AMThe C/C++ parser does not parse in "lines". You can write your whole code into one line, if you want to. The ; indicates the end of a statement. And the curly brackets {} indicate a code block.

I have always known that, Greenhorn. And it's about as intelligent as saying that BASIC must have line numbers. But go on, defend the "features" of C/C++. Such as the BREAK after each CASE statement :greensml:

Greenhorn

Gunther,

Quote from: Gunther on June 01, 2022, 08:32:26 AM
Yes, that's the point. By the way, it's similar in PASCAL.

But we've here a schoolmasterish attitude. Anyone who has a different opinion is a flat-earther, Nazi or whatever. That's how far we've come.

you forgot to mention the tinfoil hat wearer.
Kole Feut un Nordenwind gift en krusen Büdel un en lütten Pint.

Greenhorn

Quote from: jj2007 on June 01, 2022, 08:40:18 AM
I have always known that, Greenhorn.
Obviously not.

Quote from: jj2007 on June 01, 2022, 08:40:18 AM
And it's about as intelligent as saying that BASIC must have line numbers. But go on, defend the "features" of C/C++. Such as the BREAK after each CASE statement :greensml:

Kole Feut un Nordenwind gift en krusen Büdel un en lütten Pint.

hutch--

I vaguely remember Kernihan lamenting about the "break" in C. I am fascinated that it has never been simplified yet. If blocks and switch blocks have been simpler to use in most languages for many years. I guess its some form of conservatism in keeping C the way it was long ago when it was a lot closer to assembler and funny enough, that form is easy enough to duplicate in assembler anyway.

I used to see this type of code from the guys who still wrote TASM.

jj2007

Quote from: hutch-- on June 02, 2022, 12:16:41 PM
I vaguely remember Kernihan lamenting about the "break" in C. I am fascinated that it has never been simplified yet. If blocks and switch blocks have been simpler to use in most languages for many years. I guess its some form of conservatism in keeping C the way it was long ago when it was a lot closer to assembler and funny enough, that form is easy enough to duplicate in assembler anyway.

I can live happily without that "conservatism", which they apply btw also for the latest most fashionable C++ standards.

include \masm32\MasmBasic\MasmBasic.inc ; download
  Init
  Dim My$() ; a really dynamic string array
  For_ ct=0 To 9
Let My$(ct)=Str$("This is string #%i", ct)+" created at "+fTime$(0, "HH:mm:ss.fff")
Let Mid$(My$(ct), 35)=" let's try a buffer overrun" ; a famous feature of C/C++ ;-)
PrintLine My$(ct)
  Next
EndOfCode

This is string #0 created at 10:07 let's
This is string #1 created at 10:07 let's
This is string #2 created at 10:07 let's
This is string #3 created at 10:07 let's
This is string #4 created at 10:07 let's
This is string #5 created at 10:07 let's
This is string #6 created at 10:07 let's
This is string #7 created at 10:07 let's
This is string #8 created at 10:07 let's
This is string #9 created at 10:07 let's


Try the same in C/C++ :cool: