News:

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

Main Menu

Recursion is simple in 64 bit MASM

Started by hutch--, November 29, 2017, 01:19:35 PM

Previous topic - Next topic

hutch--

Recursion in an x86/64 world is effectively STACK recursion and it is here where you really do need a stack frame so that any data that is needed is stored in 64 bit shadow space. If you were writing an app that needed very high levels of recursion, you would need to set the linker option to increase the default stack size but under limited usage a default stack is fine. By producing code that is fully Microsoft ABI compliant, you can easily use recursive procedures as you are not messing with the stack alignment and the stack frame macro handles the stack for each level of recursion.

This is the test piece. Zip file contains the source and batch file that builds the test piece.

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

    include \masm32\include64\masm64rt.inc

    .data?
      ref dq ?

    .code

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

entry_point proc

    mov ref, 0

    invoke MessageBox,0,"I am at the entry point","Title",MB_OK

    invoke func1, ref

    invoke MessageBox,0,"I am leaving the entry point","Title",MB_OK

    invoke ExitProcess,0

    ret

entry_point endp

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

func1 proc pref:QWORD

    invoke MessageBox,0,"On the way up",str$(pref),MB_OK

    add pref, 1                 ; increment the counter
    cmp pref, 5                 ; set the recursion depth
    je @F

    invoke func1, pref          ; call the next iteration of recursion

  @@:
    invoke MessageBox,0,"On the way down",str$(pref),MB_OK

    ret

func1 endp

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

    end

comment # ****************************************************************

    .text:0000000140001000 C8800000                   enter 0x80, 0x0
    .text:0000000140001004 4883EC60                   sub rsp, 0x60
    .text:0000000140001008 48C7050D12000000000000     mov qword ptr [0x140002220], 0x0
    .text:0000000140001013 48C7C100000000             mov rcx, 0x0
    .text:000000014000101a 488B1557100000             mov rdx, qword ptr [0x140002078]
    .text:0000000140001021 4C8B055E100000             mov r8, qword ptr [0x140002086]
    .text:0000000140001028 49C7C100000000             mov r9, 0x0
    .text:000000014000102f FF1573110000               call qword ptr [MessageBoxA]
    .text:000000014000102f
    .text:0000000140001035 488B0DE4110000             mov rcx, qword ptr [0x140002220]
    .text:000000014000103c E831000000                 call sub_140001072
    .text:000000014000103c
    .text:0000000140001041 48C7C100000000             mov rcx, 0x0
    .text:0000000140001048 488B155E100000             mov rdx, qword ptr [0x1400020ad]
    .text:000000014000104f 4C8B0570100000             mov r8, qword ptr [0x1400020c6]
    .text:0000000140001056 49C7C100000000             mov r9, 0x0
    .text:000000014000105d FF1545110000               call qword ptr [MessageBoxA]
    .text:000000014000105d
    .text:0000000140001063 48C7C100000000             mov rcx, 0x0
    .text:000000014000106a FF1528110000               call qword ptr [ExitProcess]
    .text:000000014000106a
    .text:0000000140001070 C9                         leave
    .text:0000000140001071 C3                         ret
    ; --------------------------------------------------------------------------
    ; sub_140001072
    ; --------------------------------------------------------------------------
    sub_140001072   proc
    .text:0000000140001072 C8800000                   enter 0x80, 0x0
    .text:0000000140001076 4883EC60                   sub rsp, 0x60
    .text:000000014000107a 48894D10                   mov qword ptr [rbp+0x10], rcx
    .text:000000014000107e 488B4D10                   mov rcx, qword ptr [rbp+0x10]
    .text:0000000140001082 488B1545100000             mov rdx, qword ptr [0x1400020ce]
    .text:0000000140001089 49C7C00A000000             mov r8, 0xa
    .text:0000000140001090 FF1522110000               call qword ptr [_i64toa]
    .text:0000000140001090
    .text:0000000140001096 48C7C100000000             mov rcx, 0x0
    .text:000000014000109d 488B154A100000             mov rdx, qword ptr [0x1400020ee]
    .text:00000001400010a4 4C8B0523100000             mov r8, qword ptr [0x1400020ce]
    .text:00000001400010ab 49C7C100000000             mov r9, 0x0
    .text:00000001400010b2 FF15F0100000               call qword ptr [MessageBoxA]
    .text:00000001400010b2
    .text:00000001400010b8 4883451001                 add qword ptr [rbp+0x10], 0x1
    .text:00000001400010bd 48837D1005                 cmp qword ptr [rbp+0x10], 5
    .text:00000001400010c2 7409                       je 0x1400010cd
    .text:00000001400010c2
    .text:00000001400010c4 488B4D10                   mov rcx, qword ptr [rbp+0x10]
    .text:00000001400010c8 E8A5FFFFFF                 call sub_140001072
    .text:00000001400010c8
    .text:00000001400010cd
    .text:00000001400010cd 0x1400010cd:
    .text:00000001400010cd 488B4D10                   mov rcx, qword ptr [rbp+0x10]
    .text:00000001400010d1 488B151E100000             mov rdx, qword ptr [0x1400020f6]
    .text:00000001400010d8 49C7C00A000000             mov r8, 0xa
    .text:00000001400010df FF15D3100000               call qword ptr [_i64toa]
    .text:00000001400010df
    .text:00000001400010e5 48C7C100000000             mov rcx, 0x0
    .text:00000001400010ec 488B151D100000             mov rdx, qword ptr [0x140002110]
    .text:00000001400010f3 4C8B05FC0F0000             mov r8, qword ptr [0x1400020f6]
    .text:00000001400010fa 49C7C100000000             mov r9, 0x0
    .text:0000000140001101 FF15A1100000               call qword ptr [MessageBoxA]
    .text:0000000140001101
    .text:0000000140001107 C9                         leave
    .text:0000000140001108 C3                         ret
    sub_140001072   endp

************************************************************************ #