Skip to content
Low-level programming · Higher-level thinking
Login Register
Forum / Bugs and Fixes

Nested procedures problem

A community discussion post from the original code:zIRON forum.

0CodErr member
Posts
201
Threads
37
Location
Unknown

Nested procedures problem

At first code without problems:

program RAW_IMAGE 'test';

#set bits 32;

procedure p0(){
  procedure p1(){
    procedure p2(){
      procedure p3(){
        procedure p4(){
          eax = 0x0FEDCBA9;       
        }
        p4();              
      }
      p3();          
    }
    p2();  
  }
  p1();
}
p0(); 

Output seems correct:

00000000  E800000000        call dword 0x5
00000005  55                push ebp
00000006  8BEC              mov ebp,esp
00000008  E802000000        call dword 0xf
0000000D  C9                leave
0000000E  C3                ret
0000000F  55                push ebp
00000010  8BEC              mov ebp,esp
00000012  E802000000        call dword 0x19
00000017  C9                leave
00000018  C3                ret
00000019  55                push ebp
0000001A  8BEC              mov ebp,esp
0000001C  E802000000        call dword 0x23
00000021  C9                leave
00000022  C3                ret
00000023  55                push ebp
00000024  8BEC              mov ebp,esp
00000026  E802000000        call dword 0x2d
0000002B  C9                leave
0000002C  C3                ret
0000002D  55                push ebp
0000002E  8BEC              mov ebp,esp
00000030  B8A9CBED0F        mov eax,0xfedcba9
00000035  C9                leave
00000036  C3                ret

Next, comment call to p1(); in p0 procedure:

program RAW_IMAGE 'test';

#set bits 32;

procedure p0(){
  procedure p1(){
    procedure p2(){
      procedure p3(){
        procedure p4(){
          eax = 0x0FEDCBA9;       
        }
        p4();              
      }
      p3();          
    }
    p2();  
  }
//  p1();
}
p0();

Output was only this:

00000000  E800000000        call dword 0x5
00000005  55                push ebp
00000006  8BEC              mov ebp,esp

Replies

Colin admin
Posts
928
Threads
49
Location
Bulgaria

Thanks for report, fixed for next release.

0CodErr member
Posts
201
Threads
37
Location
Unknown
program RAW_IMAGE 'test';
#set bits 32;
procedure X1(){
  inline procedure X2(){
    int3;
  }
}
X2();

It compiles successfully. But I think that there should not compile. Because X2 in X1 scope.

Colin admin
Posts
928
Threads
49
Location
Bulgaria

Fixed for next release, was an major issue with scope related to macros. Thanks for report.

0CodErr member
Posts
201
Threads
37
Location
Unknown

Seems this work incorrect:

program PE32_CUI 'Test';
#include 'console.zir';

procedure p0(dword count){
  dword y; y = 100500;
  procedure p1(dword x, count){
    if (count <> 0){
      println x;
      count--;
      p1(y, count);
    }
  }
  p1(y, count);
}
p0(10);

wait_key();
ExitProcess(0);
100500 9 8 7 6 5 4 3 2 1 Press any key to continue

And pay attention that the LEVEL of NESTING may be deeper

Colin admin
Posts
928
Threads
49
Location
Bulgaria

After some careful consideration, I've decided not to fix this and instead remove access to local variables outside the sub function scope, instead they should be passed to the sub function.

Several reasons made me come to this decision, the first is that automatically, the base function would need to push the stack pointer to the sub function and for sub-sub functions would require 2 pushed stack pointers, this is extremely inefficient, and I would never advise anyone programs like this.

The sub functions work great as a way of simplifying a function. As for your code, you just need to replace in your recursive p1 call to "x" instead of "y".

procedure p0(dword count){
	dword y; y = 100500;
	
	procedure p1(dword x, cnt){
		if (cnt <> 0){
			println x;
			cnt--;
			p1(x, cnt);
		}
	}
	
	p1(y, count);
}
p0(10);

I'm not sure I understand the purpose of this procedure, I guess it is just an example.