flat assembler
Message board for the users of flat assembler.

Index > Windows > enter and leave

Author
Thread Post new topic Reply to topic
ishkabible



Joined: 13 Sep 2010
Posts: 54
ishkabible 11 Oct 2010, 00:56
i'm trying to understand how to use enter and leave, i would like to take the code shown below and get rid of the macros. i don't know how to implement the offsets for the variables i'm using however. how would i write i,p, and List in the procedure that contains them.



Code:
format PE console
entry main

include 'INCLUDE\MACRO\import32.inc'
include 'INCLUDE\MACRO\proc32.inc'

section '.data' data readable writeable

pausemsg db "pause>nul",0
isprime db "%i is prime",10,0

section '.code' code readable executable

proc SeviePrimesUpTo, upto:DWORD

     locals
        p dd 2
        i dd 0
        List dd 0 ;pointer to dynamicly allocated memroy
     endl

     push [upto]
     call [malloc]
     mov [List],eax
     add esp, 4        ;<-- here what dose this do? why is it here?

     initFor:
        mov eax,[i]
        cmp eax,[upto]
        jnl endFor

            mov eax, [List]
            mov edx, [i]
            mov byte[eax+edx-1],1

        inc [i]
        jmp initFor
     endFor:

     initMain:

        mov eax,[p]
        imul eax,eax
        cmp eax,[upto]

        jnle endMain

             mov eax,[List]
             mov edx,[p]
             cmp byte[eax+edx-1], 0
             je endIfMain

                 mov eax,[p]
                 imul eax,eax
                 mov [i],eax

                 initForMain:
                      mov eax,[i]
                      cmp eax,[upto]

                      jnle endForMain

                          mov eax,[List]
                          mov edx,[i]
                          mov byte[eax+edx-1],0

                      mov eax,[i]
                      add eax,[p]
                      mov [i],eax
                      jmp initForMain

                   endForMain:

             endIfMain:

          inc [p]
          jmp initMain
     endMain:

     mov [i],2
     initPrint:
        mov eax,[i]
        cmp eax,[upto]
            jnl endPrint

                mov eax,[List]
                mov edx,[i]
                cmp byte[eax+edx-1],0

                je endPrintIf

                     push [i]
                     push isprime
                     call[printf]
                     add esp,8

                endPrintIf:

            inc [i]
            jmp initPrint
     endPrint:


     push [List]
     call [free]
     add esp,4
     ret
endp

proc main
     mov eax,49
     push 49
     call SeviePrimesUpTo
     push pausemsg
     call [system]
     push 0
     call [exit]
endp

section '.idata' import data readable
library msvcrt,'msvcrt.dll'

import msvcrt,\
printf,'printf',\
scanf,'scanf',\
system,'system',\
getchar,'getchar',\
realloc,'realloc',\
malloc,'malloc',\
free,'free',\
calloc,'calloc',\
exit,'exit'
    
Post 11 Oct 2010, 00:56
View user's profile Send private message Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler 11 Oct 2010, 01:40
Code:
    locals
        p dd 2
        i dd 0
        List dd 0 ;pointer to dynamicly allocated memroy
     endl
     ; ...
     endp
    

==
Code:
    push ebp
    mov ebp,esp
    sub esp,12 ; 12 bytes b/c each of the three dds are 4 bytes
    ; FYI, label is built in directive, not macro.
    label upto dword at ebp + 4 ; Makes any reference to upto the same thing as ebp + 4, ie upto == ebp + 4
    
    label p dword at ebp - 4
    label i dword at ebp - 8
    label List dword at ebp - 12
    ; ...
    ret 12 ; Free stack space allocated above
    
Post 11 Oct 2010, 01:40
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20298
Location: In your JS exploiting you and your system
revolution 11 Oct 2010, 04:47
And remember the initialisation values also:
Code:
;...
mov [p],2
mov [i],0
mov [List],0
;...    
Post 11 Oct 2010, 04:47
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 11 Oct 2010, 06:01
ishkabible,

When most of your locals are initialized, you may use push initial_value for them instead of sub esp, X / mov [local_var], initial_value (uninitialized locals can be skipped with sub esp or push).

add esp, 4 removes argument for malloc() from stack: malloc() uses cdecl calling conventions; same for printf(), free() and system() (after which you've forgot to do it).
Post 11 Oct 2010, 06:01
View user's profile Send private message Reply with quote
ishkabible



Joined: 13 Sep 2010
Posts: 54
ishkabible 11 Oct 2010, 17:28
@Tyler, thanks that will get me started
@baldr, thank you for helping but that was a comment that was explained earlier in another thread, i should have revomved after i understood why it was there
Post 11 Oct 2010, 17:28
View user's profile Send private message Reply with quote
Tyler



Joined: 19 Nov 2009
Posts: 1216
Location: NC, USA
Tyler 11 Oct 2010, 23:07
vid's example of using Fasm with MSVC has a good example of a procs and their hand written alternatives.

Code:
;-----------------------------------------------------------------------------
; declare "fasm_ccall_avg" using pure assembly
; this computes average value of two unsigned numbers
fasm_ccall_avg:

    ;same as fasm_stdcall_avg
   push    ebp
 mov     ebp, esp
label .num1 dword at ebp+8        ;num1 argument is now located at ebp+8
label .num2 dword at ebp+12           ;num2 argument is now is located at ebp+12

        sub     esp, 2*4
label .loc1 dword at ebp-4
label .loc2 dword at ebp-8

    push    ebx esi edi

     mov     eax, [.num1]
        add     eax, [.num2]
        rcr     eax, 1

  pop     edi esi ebx
 mov     esp, ebp
    pop     ebp

     ;this is only difference. In ccall, we leave arguments on stack
     retn 0


;-----------------------------------------------------------------------------
; declare "fasm_ccall_avg2" using FASM macros
; this is same as "fasm_ccall_avg", just done with macros
proc fasm_ccall_avg2 c num1, num2  ;note the "c" declaration modifier
local loc1 dd ?
local loc2 dd ?
 mov     eax, [num1]
 add     eax, [num2]
 rcr     eax, 1
      ret
endp
    
Post 11 Oct 2010, 23:07
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4016
Location: vpcmpistri
bitRAKE 12 Oct 2010, 20:03
ENTER is one of those instructions that does more than anyone really needs. Not only does it setup the frame, but it also copies data into the new frame. LEAVE on the other hand is a single byte, and usually does exactly what is needed to clean-up the frame. Volume 1, Chapter 6, of the Intel manuals explains the details.
Post 12 Oct 2010, 20:03
View user's profile Send private message Visit poster's website Reply with quote
Kenneth Zheng



Joined: 30 Apr 2008
Posts: 14
Location: Shanghai, China
Kenneth Zheng 13 Oct 2010, 06:42
Enter instuction is one very slowly instruction. So most of complier don't use it to create stack frame. They used below instrctions to do it:
Code:
1. 16Bit Code:
    push    bp
  mov     bp, sp
      sub     bp, xxx
     
1. 32Bit Code:
      push    ebp
 mov     ebp, esp
    sub     ebp, xxx        
    
1. 64Bit Code: (fastcall)
   push    rbp
 mov     rbp, rsp
    sub     rsp, xxx
    

Kenneth Zheng
October 13th, 2010

_________________
Pure Assembly Language Funs
Post 13 Oct 2010, 06:42
View user's profile Send private message MSN Messenger Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr 13 Oct 2010, 07:06
bitRAKE,

enter looks bizarre only for those who isn't familiar with block-structured languages. Nested procedure can access arguments/locals of all outer procedures, how you'll implement this without display (which enter creates)?
Post 13 Oct 2010, 07:06
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.