News:

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

Main Menu

Calling procs with no stack frame from a proc with a stack frame.

Started by hutch--, July 19, 2016, 10:13:02 PM

Previous topic - Next topic

hutch--

This test piece starts with an entry point that has a stack frame comprising of the MASM default pair, ENTER and LEAVE. The stack frame is created by including one variable as an argument. I have encapsulated the technique in a macro to automate the entry point. The purpose of the test piece was to call nested procs that have no stack frame and it works without the menual stack corrections done with RSP.

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    include \masm64\include\masm64rt.inc

    appentry MACRO
      EXITM <proc anon:QWORD>   ;; create stack frame
    ENDM

    .code

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

entry_point appentry()

    LOCAL retval :QWORD

    invoke bareproc
    mov retval, rax

    conout lf,"Return value = ",str$(retval),lf,lf

    waitkey
    void(ExitProcess,0)

    ret

entry_point endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

bareproc proc

  ; ------------------------
  ; proc with no stack frame
  ; ------------------------
    LOCAL64 var         ; non stack QWORD variable
    mov var, 12345678
    mov rcx, var

    call bareproc2

    ret

bareproc endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

bareproc2 proc

  ; ------------------------
  ; proc with no stack frame
  ; ------------------------
    mov rax, rcx

    ret

bareproc2 endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    end


This is the asm dump from ArkAsm.


%define u(x) __utf16__(x)
%define w(x) __utf32__(x)

segment .text
enter 0x40, 0x0
call sub_14000108a
mov qword ptr [rbp-8], rax
mov rcx, qword ptr [rbp-8]
mov rdx, qword ptr [0x140003000]
mov r8d, 0xa
call qword ptr [_i64toa]
mov rcx, qword ptr [0x14000300b]
call 0x1400010b0
movabs rcx, 0x140003013
call 0x1400010b0
mov rcx, qword ptr [0x140003000]
call 0x1400010b0
mov rcx, qword ptr [0x140003026]
call 0x1400010b0
mov rcx, qword ptr [0x140003031]
call 0x1400010b0
mov rcx, qword ptr [0x140003056]
call 0x1400010b0
mov rcx, qword ptr [0x140003061]
call 0x1400010b0
call sub_140001130
xor ecx, ecx
call qword ptr [ExitProcess]
leave
ret
; --------------------------------------------------------------------------
; sub_14000108a
; --------------------------------------------------------------------------
sub_14000108a   proc
mov qword ptr [0x140003069], 0xbc614e
mov rcx, qword ptr [0x140003069]
call sub_1400010a2
ret
sub_14000108a   endp

; --------------------------------------------------------------------------
; sub_1400010a2
; --------------------------------------------------------------------------
sub_1400010a2   proc
mov rax, rcx
ret
int3
int3
int3
int3
int3
int3
int3
int3
int3
int3

0x1400010b0:
enter 0x50, 0x0
mov qword ptr [0x140003080], r12
mov qword ptr [0x140003088], r13
mov qword ptr [0x140003090], r14
mov r12, rcx
mov rcx, -0xb
call qword ptr [GetStdHandle]
mov r13, rax
mov rax, r12
sub rax, 0x1

0x1400010e3:
add rax, 0x1
cmp byte ptr [rax], 0x0
jne 0x1400010e3
sub rax, r12
mov r14, rax
mov rcx, r13
mov rdx, r12
mov r8, r14
lea r9, [rbp-0x10]
and qword ptr [rbp-0x30], 0x0
call qword ptr [WriteFile]
mov rax, qword ptr [rbp-0x10]
mov r12, qword ptr [0x140003080]
mov r13, qword ptr [0x140003088]
mov r14, qword ptr [0x140003090]
leave
ret
sub_1400010a2   endp

int3
int3
int3
int3
int3
int3
int3
int3
int3
int3
int3
; --------------------------------------------------------------------------
; sub_140001130
; --------------------------------------------------------------------------
sub_140001130   proc
enter 0x40, 0x0
mov rcx, -0xa
call qword ptr [GetStdHandle]
mov rcx, rax
call qword ptr [FlushConsoleInputBuffer]

0x14000114a:
mov ecx, 0x1
xor edx, edx
call qword ptr [SleepEx]
call qword ptr [_kbhit]
test rax, rax
je 0x14000114a
call qword ptr [_getch]
leave
ret
sub_140001130   endp