News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Push a string onto the stack

Started by Timofeyka, July 09, 2021, 06:30:02 PM

Previous topic - Next topic

Timofeyka

Hello.
I need to put strings on the stack as parameters for MessageBoxA, but my current code just doesn't work
  .386
  .model flat, stdcall
  option casemap :none
  extern MessageBoxA@16:near
  include C:\masm32\include\masm32rt.inc
  include C:\masm32\include\PushText.inc
  .data
      a db "test!",0
      b db "test!!",0
  .code
  main:
  push 0
  PushText "A"
  PushText "B"
  push 0
  call MessageBoxA@16
  end main
http://easyprogramminglanguage.rf.gd/

jj2007

Hi Timo,

A few rules:

- include C:\masm32\include\PushText.inc: my crystal ball has no idea what's inside. Besides, some of us really hate "projects" with many little files that contain only a few lines of useful stuff. Put your macros into the main text, except if they are more than, say, a hundred lines long.

- use include \Masm32\include\..., not C:\Masm32; many members use other drives

- "doesn't work" is a safe recipe for not getting any help at all; tell us what doesn't work, i.e. error messages or other problems.

Timofeyka

Quote from: jj2007 on July 09, 2021, 06:47:06 PM
Timo,
A few rules:
- include C:\masm32\include\PushText.inc: my crystal ball has no idea what's inside.
- use include \Masm32\include\..., not C:\Masm32; many members use other drives
- "doesn't work" is a safe recipe for not getting any help at all; tell us what doesn't work, i.e. error messages or other problems.
The fact of the matter is that there is nothing to report - there are no errors. It just doesn't work.
http://easyprogramminglanguage.rf.gd/

Timofeyka

ptEsp=0
PushText MACRO arg, obs:=<0>
Local c1$, c1x$, q$, ct, txLen, tmp$, txRest, pNum
  ifb <arg>
   add esp, ptEsp
   ptEsp=0
   EXITM
  endif
  txLen SIZESTR <arg>
  txRest=(txLen-2) mod 4
  ct=3-txRest   ; OPT_Errlines 0
  tmp$ CATSTR <Len=>, %txLen, <, rest=>, %txRest, <, count=>, %ct
  % echo tmp$
  tmp$ equ <0>
  mov edx, esp
  pNum=0
  While txLen gt 1
   pNum=(pNum shl 8) or tmp$
   txLen=txLen-1
   c1$ SUBSTR <arg>, txLen, 1
   tmp$ CATSTR <">, c1$, <">
   ifidn c1$, <\>
      c1x$ SUBSTR <arg>, txLen+1, 1
      ifidn c1x$, <n>
         tmp$ equ <13>
      endif
   endif
   ifidn c1$, <n>
      c1x$ SUBSTR <arg>, txLen-1, 1
      ifidn c1x$, <\>
         tmp$ equ <10>
      endif
   endif
   ct=ct+1
   if ct ge 4
      if obs
         push not pNum
      else
         push pNum
      endif
      ct=0
      pNum=0
      ptEsp=ptEsp+4
   endif
  Endm
  if obs
   .Repeat
      not byte ptr [edx]
      dec edx
   .Until edx<esp
  endif
ENDM

in PushText.inc
http://easyprogramminglanguage.rf.gd/

jj2007

OK, thanks, now it's clearer:
- how did you assemble your code? With \Masm32\qEditor.exe, with RichMasm, with another IDE?
- if it was with qEditor, did you use the console assembly and link?
- does the *.exe file exist?
- when you run it from a DOS prompt, what do you see?

Timofeyka

I compile with ml, and link with link as a console application(without any IDE). When you start the application, the command line opens, it is on the screen for a couple of seconds, and then it closes. Yes, there is an exe file
http://easyprogramminglanguage.rf.gd/

jj2007

Ok. In the meantime, I found the post where you got the PushText.inc, and surprise, surprise: it was me :badgrin:

Try this:
  main:
  PushText "Hello "
  print esp
  PushText "World"
  inkey esp
  exit
  end main


Note that after each PushText, the stack pointer is different. That is why MessageBox won't work, unless you store the new pointers somewhere. What exactly are your intentions with PushText?

Timofeyka

Quote from: jj2007 on July 09, 2021, 07:09:23 PM
Ok. In the meantime, I found the post where you got the PushText.inc, and surprise, surprise: it was me :badgrin:

Try this:
  main:
  PushText "Hello "
  print esp
  PushText "World"
  inkey esp
  exit
  end main

Strange, it works ...
http://easyprogramminglanguage.rf.gd/

Timofeyka

I myself only need a stack to store arguments for commands.
http://easyprogramminglanguage.rf.gd/

hutch--

Timo,

Passing arguments on the stack "by their address" for strings and by value for anything else is normal, are you trying to do something different ?

Timofeyka

Quote from: hutch-- on July 09, 2021, 07:24:36 PM
Timo,

Passing arguments on the stack "by their address" for strings and by value for anything else is normal, are you trying to do something different ?
No, only pass arguments to commands
http://easyprogramminglanguage.rf.gd/

jj2007

Quote from: Timofeyka on July 09, 2021, 07:19:23 PM
Strange, it works ...

I almost never post code that doesn't work :cool:

Quote from: Timofeyka on July 09, 2021, 07:21:48 PM
I myself only need a stack to store arguments for commands.

PushText is a fairly exotic way to do that, there are simpler ones, and you know them already:
  .data
      a db "test!",0
      b db "test!!",0


So why do you want to take the complicated way?

Timofeyka

Quote from: jj2007 on July 09, 2021, 07:31:03 PM
Quote from: Timofeyka on July 09, 2021, 07:19:23 PM
Strange, it works ...

I almost never post code that doesn't work :cool:

Quote from: Timofeyka on July 09, 2021, 07:21:48 PM
I myself only need a stack to store arguments for commands.

PushText is a fairly exotic way to do that, there are simpler ones, and you know them already:
  .data
      a db "test!",0
      b db "test!!",0


So why do you want to take the complicated way?
I can't say to be honest, you just need to pass arguments to commands and call them without procedures and other rubbish.
http://easyprogramminglanguage.rf.gd/

hutch--

The problem for us is what you are after is an old hacking technique to pre-load the stack and dump arguments into a location that does not normally accept arguments of that type. Later OS versions have broken that hack but its a grossly inefficient way to pass arguments. Conventional stack works fine and for speed, pass in registers but passing string data rather than addresses does not have a valid purpose apart from being a broken hacking technique.

Timofeyka

Thanks for the answers. I decided to add variables to the stack.
http://easyprogramminglanguage.rf.gd/