General > The Laboratory

Nested Procedures

(1/1)

aw27:
MASM supports nested procedures and at the same time does not support nested procedures  :dazzled:- it is always ready to give the "Cannot nest procedures" error  :shock:. Period.

This is a 4 level deep procedure nesting exercise. I just made it to understand how the ENTER instruction that nobody uses work.  :t


--- Code: ---.386

.MODEL FLAT, stdcall

includelib \masm32\lib\msvcrt.lib
printf proto C :ptr, :vararg
 
.data
fmt db "Variable from level %d to level %d",10,0
   
.code

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

LOC1 equ <dword ptr [ebp-4]>

nestum PROC
enter  4,0
mov eax, [ebp+8]
mov LOC1, eax
jmp n0
nest1 PROC
enter 4,1
jmp n1
nest2 PROC
enter 4,2
jmp n2
nest3 proc
enter 4, 3
mov eax, [ebp+8]
mov LOC1, eax
INVOKE printf, offset fmt, LOC1, 4
push ebp
mov ebp, [ebp]
INVOKE printf, offset fmt, LOC1, 4

mov ebp, [ebp]
INVOKE printf, offset fmt, LOC1, 4
mov ebp, [ebp]
INVOKE printf, offset fmt, LOC1, 4

pop ebp
leave
ret 4
nest3 endp
n2:
mov eax, [ebp+8]
mov LOC1, eax
INVOKE printf, offset fmt, LOC1, 3

push ebp
mov ebp, [ebp]
INVOKE printf, offset fmt, LOC1, 3

mov ebp, [ebp]
INVOKE printf, offset fmt, LOC1, 3
pop ebp

push 3
call nest3

leave
ret 4
nest2 ENDP
n1:
mov eax, [ebp+8]
mov LOC1, eax
INVOKE printf, offset fmt, LOC1, 2

push ebp
mov ebp, [ebp]
INVOKE printf, offset fmt, LOC1, 2
pop ebp

push 2
call nest2

leave
ret 4
nest1 ENDP
n0:
INVOKE printf, offset fmt, LOC1, 1
push 1
call nest1
leave
ret 4
nestum ENDP

main PROC public
push 0
call nestum
ret
main ENDP

end main   

--- End code ---

Variable from level 0 to level 1
Variable from level 1 to level 2
Variable from level 0 to level 2
Variable from level 2 to level 3
Variable from level 1 to level 3
Variable from level 0 to level 3
Variable from level 3 to level 4
Variable from level 2 to level 4
Variable from level 1 to level 4
Variable from level 0 to level 4

hutch--:
It is an interesting study although not many higher level languages use internal procedure nesting, what I wonder is if the is much difference between conventional nesting by calling a chain of external procedures as against building the nesting within one procedure. I have very occasionally nested a procedure within another by defining a normal stack frame manually but I don't see any real advantage in doing so.

aw27:

--- Quote from: hutch-- on September 25, 2017, 01:22:28 AM ---It is an interesting study although not many higher level languages use internal procedure nesting, what I wonder is if the is much difference between conventional nesting by calling a chain of external procedures as against building the nesting within one procedure. I have very occasionally nested a procedure within another by defining a normal stack frame manually but I don't see any real advantage in doing so.

--- End quote ---

The Pascal Language, namely Delphi support nested procedures. What I find nice is the capability to access variables of outer functions without passing them as parameters. There are a few other alternatives, including anonymous functions but they carry a larger overhead. BTW, I don't think Delphi uses the ENTER instruction.  :badgrin:

Navigation

[0] Message Index

Go to full version