Author Topic: Passing strings to the stack  (Read 1220 times)

Vortex

  • Member
  • *****
  • Posts: 1730
Passing strings to the stack
« on: November 13, 2016, 05:30:35 AM »
Here is a macro to copy strings to the stack :

Code: [Select]
StrToStack  This string is passed to the stack
esp points to the address of the string in the stack.

The StrToStack macro splits a string to DWORD values and pushes them to the stack :

Code: [Select]
kc
ats
eht
ot d
essa
p si
gni
rts
sihT

push    27491
push    1635021600
push    1701344288
push    1869881444
push    1702064993
push    1881174889
push    543649385
push    1920234272
push    1936287828

Examples :

Code: [Select]
include Test.inc
include StrToStackMacro.inc

; DEBUG_STR_TO_STACK=1

.code

start:

    invoke  StdOut,\
            @StrToStack(Line copied to the stack)

    add     esp,stackcount

    invoke  ExitProcess,0

END start

Code: [Select]
include Test.inc
include StrToStackMacro.inc

; DEBUG_STR_TO_STACK=1

.data

frmt    db 13,10,13,10,'%s',13,10,'%s'
        db 13,10,13,10
        db 'Length of string1 = %d',13,10
        db 'Length of string2 = %d',13,10
        db 0

.data?

str1    dd ?
str2    dd ?
len1    dd ?
len2    dd ?
stack   dd ?

buffer  db 128 dup(?)

.code

start:

    StdOutX     <Macro test>

    StrToStack  This string is passed to the stack

    mov     str1,esp
    mov     stack,stackcount
    mov     len1,stringlen

    mov     str2,\
            @StrToStack(The second long line copied to the stack)
   
    add     stack,stackcount
    mov     len2,stringlen

    invoke  wsprintf,ADDR buffer,\
            ADDR frmt,str1,str2,len1,len2
           
    invoke  StdOut,ADDR buffer

    add     esp,stack

    invoke  ExitProcess,0

END start

stackcount : defined by the macro, number of bytes to balance the stack to remove the string
stringlen    : defined by the macro, the length of the string in the stack

Vortex

  • Member
  • *****
  • Posts: 1730
Re: Passing strings to the stack
« Reply #1 on: November 13, 2016, 05:53:20 AM »
Attached is the version assembled with ml64.

Example :

Code: [Select]
include \masm32\include64\masm64rt.inc

include StrToStackMacro64.inc

; DEBUG_STR_TO_STACK = 1

.code

start PROC

    invoke  StdOut,@StrToStack(Hello from Masm64)

;   stackcount = macro value holding the value
;                to balance the stack
   
    add     rsp,stackcount

    invoke  ExitProcess,0
    ret

start ENDP

END

jj2007

  • Member
  • *****
  • Posts: 7752
  • Assembler is fun ;-)
    • MasmBasic
Re: Passing strings to the stack
« Reply #2 on: November 13, 2016, 06:20:08 AM »
Interesting, Erol :t

I've seen compilers do that, and wonder how efficient it is, speed- and size-wise ::)

Vortex

  • Member
  • *****
  • Posts: 1730
Re: Passing strings to the stack
« Reply #3 on: November 13, 2016, 08:01:11 AM »
Hi Jochen,

It could be useful to "move" strings to the code section for some relocatable code.

jj2007

  • Member
  • *****
  • Posts: 7752
  • Assembler is fun ;-)
    • MasmBasic
Re: Passing strings to the stack
« Reply #4 on: November 13, 2016, 08:09:07 AM »
Yes, that's right. Of course, they would be read-only.

jj2007

  • Member
  • *****
  • Posts: 7752
  • Assembler is fun ;-)
    • MasmBasic
Re: Passing strings to the stack
« Reply #5 on: March 01, 2017, 03:28:32 AM »
Code: [Select]
include \masm32\include\masm32rt.inc
include PushText.inc

.code
start:

  PushText "The new PushText macro provides a simple option\nto place strings on the stack.\nVortex aka Erol had the original idea, which I refined\na little bit for inclusion in the MasmBasic library."
  print esp

  PushText "\n\nFor a longer text, just split it\ninto several statements."
  inkey esp

  PushText    ; no args: correct the stack
  exit

end start

PushText "some text", 1 may serve to hide the text a little bit ;)

Vortex

  • Member
  • *****
  • Posts: 1730
Re: Passing strings to the stack
« Reply #6 on: March 01, 2017, 05:06:45 AM »
Hi Jochen,

Thanks for the attached example :t Your code is modular and well organized.

jj2007

  • Member
  • *****
  • Posts: 7752
  • Assembler is fun ;-)
    • MasmBasic
Re: Passing strings to the stack
« Reply #7 on: March 01, 2017, 06:17:28 AM »
Thanks, Erol :icon14:

include \masm32\MasmBasic\MasmBasic.inc      ; download
  Init
  PushText "The new PushText macro provides a simple option\nto place strings on the stack.\nVortex aka Erol had the original idea, which I refined\na little bit for inclusion in the MasmBasic library."
  PrintLine esp
  PushText "Добро пожаловать"      ; it works with Unicode, too!
  PrintLine esp
  PushText
  Inkey "Not bad, eh?"
EndOfCode


Output:
Code: [Select]
The new PushText macro provides a simple option
to place strings on the stack.
Vortex aka Erol had the original idea, which I refined
a little bit for inclusion in the MasmBasic library.
Добро пожаловать
Not bad, eh?