News:

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

Main Menu

Passing strings to the stack

Started by Vortex, November 13, 2016, 05:30:35 AM

Previous topic - Next topic

Vortex

Here is a macro to copy strings to the stack :

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 :

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 :

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


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

Attached is the version assembled with ml64.

Example :

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

Interesting, Erol :t

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

Vortex

Hi Jochen,

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

jj2007

Yes, that's right. Of course, they would be read-only.

jj2007

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

Hi Jochen,

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

jj2007

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