flat assembler
Message board for the users of flat assembler.

Index > Windows > How to pass a local variable into procedure

Author
Thread Post new topic Reply to topic
rinart73



Joined: 11 Dec 2014
Posts: 16
rinart73 12 Dec 2014, 14:05
Code:
format pe console
entry start

include 'win32a.inc'


section '.text' code executable
  start:
    call main
    invoke  ExitProcess,0


  proc main
    local Text2 db "01234"

    push Text
    push sFormat
    call [_printf]
    add esp, 8
    call [_getchar]

    sub esp, 12
    movsx eax, [Text]
    mov [ebp -  4], eax
    movsx eax, [Text2]
    mov [ebp -  8], eax
    mov dword[ebp - 12], 9

    ;push 4
    ;movsx eax, [!Text2]
    ;push eax
    ;movsx eax, [!Text]
    ;push eax
    call _memcpy
    add esp, 12

    push Text
    push sFormat
    call [_printf]
    add esp, 8
    call [_getchar]
  ret
  endp

  ;; INTERNAL
  proc _memcpy src:DWORD, dst:DWORD, siz:DWORD
    ;cld
    mov esi, [ebp+8];mov esi, [src]
    mov edi, [ebp+12];mov edi, [dst]
    mov ecx, [ebp+16];mov ecx, [siz]
    rep movsb
  ret
  endp


section '.rdata' data readable writeable
 sFormat db "%s",10,0
 Text db "9876543210",0


section '.idata' data readable import
 library kernel32, 'kernel32.dll',\
         msvcrt,   'msvcrt.dll'
 import kernel32, ExitProcess, 'ExitProcess'
 import msvcrt, _printf, 'printf',\
                _getchar, 'getchar'    

I'm trying to copy one string to another. But it's not works, string don't changes.
Post 12 Dec 2014, 14:05
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20299
Location: In your JS exploiting you and your system
revolution 12 Dec 2014, 14:09
I assume you actually want to use LEA instead of MOVSX.
Code:
lea eax, [Text] ;and the same for the other movsx    
Post 12 Dec 2014, 14:09
View user's profile Send private message Visit poster's website Reply with quote
rinart73



Joined: 11 Dec 2014
Posts: 16
rinart73 12 Dec 2014, 14:18
Can you explain the difference, please?

UPD: This isn't works. _memcpy won't change variable Text :/
Post 12 Dec 2014, 14:18
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20299
Location: In your JS exploiting you and your system
revolution 12 Dec 2014, 14:28
You are putting the arguments for memcpy in the wrong place. You can use the macro cinvoke to do the work for you.
Code:
cinvoke _memcpy,Text,addr Text2,9    
Post 12 Dec 2014, 14:28
View user's profile Send private message Visit poster's website Reply with quote
rinart73



Joined: 11 Dec 2014
Posts: 16
rinart73 12 Dec 2014, 14:40
I don't understand. Why ebp is the wrong place? I found it in tutorials.
And I tried to use "push" and proc _memcpy, arg1,arg2,arg3...

cinvoke is fast? What is the difference from "invoke" and "call"?
Post 12 Dec 2014, 14:40
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20299
Location: In your JS exploiting you and your system
revolution 12 Dec 2014, 14:44
Your parameters need to follow ESP not EBP.

invoke is a macro, call is a CPU instruction.

cinvoke (ccall convention) and invoke (stdcall convention) only differ in the way the stack is restored when the procedure returns. cinvoke will place the "add esp,12", but invoke won't. This is because of the stdcall and ccall convention differences.
Post 12 Dec 2014, 14:44
View user's profile Send private message Visit poster's website Reply with quote
rinart73



Joined: 11 Dec 2014
Posts: 16
rinart73 12 Dec 2014, 14:45
cinvoke _memcpy, Text, addr Text2, 9
Error: invalid value : pushd addr ..var?TK

cinvoke _memcpy, Text, dword[Text2], 9
Error: operand size not specified: call[_memcpy]
Post 12 Dec 2014, 14:45
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20299
Location: In your JS exploiting you and your system
revolution 12 Dec 2014, 14:48
Oh, sorry, your _memcpy is not the MSVCRT version. In that case use "stdcall _memcpy,...".

Also, don't put the ! in front of the names, it has no meaning unless you define your variable names with the ! also.
Post 12 Dec 2014, 14:48
View user's profile Send private message Visit poster's website Reply with quote
rinart73



Joined: 11 Dec 2014
Posts: 16
rinart73 12 Dec 2014, 14:53
I use it for reason, believe me Very Happy

stdcall _memcpy, !Text, dword[!Text2], 9
Result: Crash
Post 12 Dec 2014, 14:53
View user's profile Send private message Reply with quote
rinart73



Joined: 11 Dec 2014
Posts: 16
rinart73 12 Dec 2014, 14:53
I use it for reason, believe me Very Happy

stdcall _memcpy, Text, dword[Text2], 9
Result: Crash
Post 12 Dec 2014, 14:53
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20299
Location: In your JS exploiting you and your system
revolution 12 Dec 2014, 15:09
Use "addr" not "dword":
Code:
stdcall _memcpy, Text, addr Text2, 9    
Post 12 Dec 2014, 15:09
View user's profile Send private message Visit poster's website Reply with quote
rinart73



Joined: 11 Dec 2014
Posts: 16
rinart73 12 Dec 2014, 15:18
all is working Smile thnx
Post 12 Dec 2014, 15:18
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.