Author Topic: Inline assembly and calling API functions  (Read 911 times)

Vortex

  • Moderator
  • Member
  • *****
  • Posts: 2674
Inline assembly and calling API functions
« 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 :

Code: [Select]
// 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.
« Last Edit: June 01, 2022, 05:00:12 AM by Vortex »

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 9558
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Inline assembly and calling API functions
« Reply #1 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 ?
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

Vortex

  • Moderator
  • Member
  • *****
  • Posts: 2674
Re: Inline assembly and calling API functions
« Reply #2 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.

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 9558
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Inline assembly and calling API functions
« Reply #3 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.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

jj2007

  • Member
  • *****
  • Posts: 12691
  • Assembler is fun ;-)
    • MasmBasic
Re: Inline assembly and calling API functions
« Reply #4 on: June 01, 2022, 02:25:47 AM »
I 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

  • Member
  • ***
  • Posts: 395
Re: Inline assembly and calling API functions
« Reply #5 on: June 01, 2022, 04:57:47 AM »
I 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

  • Member
  • *****
  • Posts: 4072
  • Forgive your enemies, but never forget their names
Re: Inline assembly and calling API functions
« Reply #6 on: June 01, 2022, 08:32:26 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.
Get your facts first, and then you can distort them.

jj2007

  • Member
  • *****
  • Posts: 12691
  • Assembler is fun ;-)
    • MasmBasic
Re: Inline assembly and calling API functions
« Reply #7 on: June 01, 2022, 08:40:18 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.

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

  • Member
  • ***
  • Posts: 395
Re: Inline assembly and calling API functions
« Reply #8 on: June 01, 2022, 09:11:50 PM »
Gunther,

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

  • Member
  • ***
  • Posts: 395
Re: Inline assembly and calling API functions
« Reply #9 on: June 01, 2022, 09:21:53 PM »
I have always known that, Greenhorn.
Obviously not.

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--

  • Administrator
  • Member
  • ******
  • Posts: 9558
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Inline assembly and calling API functions
« Reply #10 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.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

jj2007

  • Member
  • *****
  • Posts: 12691
  • Assembler is fun ;-)
    • MasmBasic
Re: Inline assembly and calling API functions
« Reply #11 on: June 02, 2022, 06:07:58 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.

Code: [Select]
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
Code: [Select]
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: