News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Does displaying stack is wrong?

Started by kejt, January 27, 2013, 03:02:22 AM

Previous topic - Next topic

kejt

Hi guys,
I have a question. Does displaying stack is wrong? Can it cause any problems? Here is the sample
code:

.386
.model flat, stdcall
option casemap:none

include /masm32/include/masm32rt.inc

.data?
stop db ?
.data

.code
start:
call writeA

push 1
push offset stop
call StdIn

push 0
call ExitProcess

writeA proc
LOCAL Buff0:DWORD
LOCAL Buff1:DWORD

mov Buff0, "!K"
mov Buff1, "CATS"

lea eax, Buff1
push eax
call StdOut


ret


writeA endp
end start

dedndave

with a little modification, i had no problems
.386
.model flat, stdcall
option casemap:none

include /masm32/include/masm32rt.inc

you can get rid of the first 3 lines, as the masm32rt.inc file takes care of those
and - i use "\" instead of "/"

the program seems to run, although it isn't pretty   :P
so - what is the question ?

kejt

I'm only worried about: does it is safe.

dedndave

it is reasonably safe
when you create local variables, the assembler creates them in the order listed
(the first one listed is at the higher stack address)

you can also do it this way
writeA  PROC

    push    "!K"
    push    "CATS"
    push    esp
    call    StdOut
    add     esp,8
    ret

writeA  ENDP


or this way
writeA  PROC

    push    "!K"
    push    "CATS"
    INVOKE  StdOut,esp
    add     esp,8
    ret

writeA  ENDP

kejt


dedndave

i didn't mention it, but we would normally put the string in the initialized data section

.data

szString db 'STACK!',0


then, eliminate the writeA proc and replace the call with

    INVOKE  StdOut,offset szString

you see, even though you can create the string on the stack,
it takes more bytes of code than it does for the string, itself

now, if the string is variable (different strings at different times),
we might create it on the stack and display it from there

Gunther

Hi kejt,

welcome to the forum.  :t

Gunther
You have to know the facts before you can distort them.

jj2007

Quote from: kejt on January 27, 2013, 03:20:53 AM
I'm only worried about: does it is safe.

Absolutely safe. The stack is a memory area that all your proggies are using all the time, so you can do with it what you want. Here is a variant of your prog. Launch Olly to compare it to your version.

writeA proc
  push "!e"
  push "fas "
  push "si K"
  push "CATS"
  push " ehT"
  invoke StdOut, esp
  add esp, 5*DWORD
  ret
writeA endp

kejt

I was gone for a while. I know that there are other more efficient methods of displaying strings, but I need to do it this way. I have a proc which returns string result to local variables. I have needed to save some bytes. Just wanted to know: does it is safe. Thanks all for answers.

dedndave

here's an example of when to use the stack   :P

EDIT: oops - forgot to preserve EBX
updated version attached

Tedd

Quote from: kejt on January 27, 2013, 05:51:24 AM
I was gone for a while. I know that there are other more efficient methods of displaying strings, but I need to do it this way. I have a proc which returns string result to local variables. I have needed to save some bytes. Just wanted to know: does it is safe. Thanks all for answers.
It's safe to use the stack, as long as you only use 'your' space (local variables or stack frame) and don't overwrite outside of that. You can pass data on the stack to other functions that you have called, but you shouldn't return data on the stack to functions that called yours.
Potato2

Vortex

Hi kejt,

Another quick example :

include     \masm32\include\masm32rt.inc

.data

format1     db 'Stack = %X , Value = %u',0

.data?

buffer      db 64 dup(?)

.code

start:

    push    1000

    mov     edx,esp
    invoke  wsprintf,ADDR buffer,ADDR format1,edx,DWORD PTR [edx]
    invoke  StdOut,ADDR buffer

    pop     eax

    invoke  ExitProcess,0

END start