The MASM Forum

Projects => Poasm => Pelle's C compiler and tools => Topic started by: Vortex on February 28, 2022, 01:06:44 AM

Title: Inline assembly and calling API functions
Post by: Vortex on February 28, 2022, 01:06:44 AM
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.
Title: Re: Inline assembly and calling API functions
Post by: hutch-- on May 29, 2022, 10:17:08 PM
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 ?
Title: Re: Inline assembly and calling API functions
Post by: Vortex on May 30, 2022, 06:09:49 AM
Hi Hucth,

Thanks, I removed the square brackets and the recompiled code didn't crash. Maybe, I was mistaken somewhere.
Title: Re: Inline assembly and calling API functions
Post by: hutch-- on May 31, 2022, 07:31:07 PM
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.
Title: Re: Inline assembly and calling API functions
Post by: 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:
Title: Re: Inline assembly and calling API functions
Post by: Greenhorn on June 01, 2022, 04:57:47 AM
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.
Title: Re: Inline assembly and calling API functions
Post by: Gunther on June 01, 2022, 08:32:26 AM
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.
Title: Re: Inline assembly and calling API functions
Post by: jj2007 on June 01, 2022, 08:40:18 AM
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:
Title: Re: Inline assembly and calling API functions
Post by: Greenhorn on June 01, 2022, 09:11:50 PM
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.
Title: Re: Inline assembly and calling API functions
Post by: Greenhorn on June 01, 2022, 09:21:53 PM
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:
(https://media1.giphy.com/media/WrNfErHio7ZAc/giphy.gif?cid=790b76118b81f1dfe5159e379c906cc08405ad32b10bf458&rid=giphy.gif&ct=g)
Title: Re: Inline assembly and calling API functions
Post by: 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 used to see this type of code from the guys who still wrote TASM.
Title: Re: Inline assembly and calling API functions
Post by: jj2007 on June 02, 2022, 06:07:58 PM
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: