Author Topic: Push a string onto the stack  (Read 1016 times)

Timofeyka

  • Regular Member
  • *
  • Posts: 29
Push a string onto the stack
« on: July 09, 2021, 06:30:02 PM »
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

jj2007

  • Member
  • *****
  • Posts: 11551
  • Assembler is fun ;-)
    • MasmBasic
Re: Push a string onto the stack
« Reply #1 on: July 09, 2021, 06:47:06 PM »
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

  • Regular Member
  • *
  • Posts: 29
Re: Push a string onto the stack
« Reply #2 on: July 09, 2021, 06:48:21 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.

Timofeyka

  • Regular Member
  • *
  • Posts: 29
Re: Push a string onto the stack
« Reply #3 on: July 09, 2021, 06:49:42 PM »
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

jj2007

  • Member
  • *****
  • Posts: 11551
  • Assembler is fun ;-)
    • MasmBasic
Re: Push a string onto the stack
« Reply #4 on: July 09, 2021, 06:52:46 PM »
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

  • Regular Member
  • *
  • Posts: 29
Re: Push a string onto the stack
« Reply #5 on: July 09, 2021, 07:01:44 PM »
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

jj2007

  • Member
  • *****
  • Posts: 11551
  • Assembler is fun ;-)
    • MasmBasic
Re: Push a string onto the stack
« Reply #6 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:
Code: [Select]
  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

  • Regular Member
  • *
  • Posts: 29
Re: Push a string onto the stack
« Reply #7 on: July 09, 2021, 07:19: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:
Code: [Select]
  main:
  PushText "Hello "
  print esp
  PushText "World"
  inkey esp
  exit
  end main
Strange, it works ...

Timofeyka

  • Regular Member
  • *
  • Posts: 29
Re: Push a string onto the stack
« Reply #8 on: July 09, 2021, 07:21:48 PM »
I myself only need a stack to store arguments for commands.

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 8497
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Push a string onto the stack
« Reply #9 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 ?
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

Timofeyka

  • Regular Member
  • *
  • Posts: 29
Re: Push a string onto the stack
« Reply #10 on: July 09, 2021, 07:27:49 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

jj2007

  • Member
  • *****
  • Posts: 11551
  • Assembler is fun ;-)
    • MasmBasic
Re: Push a string onto the stack
« Reply #11 on: July 09, 2021, 07:31:03 PM »
Strange, it works ...

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

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:
Code: [Select]
  .data
      a db "test!",0
      b db "test!!",0

So why do you want to take the complicated way?

Timofeyka

  • Regular Member
  • *
  • Posts: 29
Re: Push a string onto the stack
« Reply #12 on: July 09, 2021, 07:41:14 PM »
Strange, it works ...

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

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

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 8497
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: Push a string onto the stack
« Reply #13 on: July 09, 2021, 07:54:12 PM »
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.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :skrewy:

Timofeyka

  • Regular Member
  • *
  • Posts: 29
Re: Push a string onto the stack
« Reply #14 on: July 09, 2021, 07:59:37 PM »
Thanks for the answers. I decided to add variables to the stack.