### Forums: Forum of Decimal BASIC (Thread #39739)

#### Stack Usage? (2018-07-06 21:53 by toml_12953 #81579)

In the various version of Decimal BASIC (Decimal, Acc and Acc2) how many bytes does each call to an iterative function take? Is it dependent on the number of parameters?

I have an Ackermann function and I'm trying to calculate the number of bytes on the stack at each iteration.

FUNCTION ackermann(m,n)
LET stksiz=stksiz+32 ! The 32 is the number to be replaced so I need to know how much each call adds to stack size
LET ncalls = ncalls + 1
LET curstack = stksiz
IF curstack > stacksize THEN
LET stacksize = curstack
END IF
IF m = 0 THEN
LET ackermann=n+1
EXIT FUNCTION
END IF
IF n = 0 THEN
LET ackermann=ackermann(m-1,1)
EXIT FUNCTION
END IF
LET ackermann=ackermann(m-1,ackermann(m,n-1))
END FUNCTION

A number takes 8 bytes, correct? That would mean that this would take 16 bytes plus some for the return address. Is the total number easily calculable?

You can not use Wiki syntax
You are not logged in. To discriminate your posts from the rest, you need to pick a nickname. (The uniqueness of nickname is not reserved. It is possible that someone else could use the exactly same nickname. If you want assurance of your identity, you are recommended to login before posting.) Login

#### Re: Stack Usage? (2018-07-07 14:31 by SHIRAISHI Kazuo #81583)

> In the various version of Decimal BASIC (Decimal, Acc and Acc2) how many bytes does each call to an iterative function take? Is it dependent on the number of parameters?
>
> I have an Ackermann function and I'm trying to calculate the number of bytes on the stack at each iteration.
>
> FUNCTION ackermann(m,n)
> LET stksiz=stksiz+32 ! The 32 is the number to be replaced so I need to know how much each call adds to stack size
> LET ncalls = ncalls + 1
> LET curstack = stksiz
> IF curstack > stacksize THEN
> LET stacksize = curstack
> END IF
> IF m = 0 THEN
> LET ackermann=n+1
> EXIT FUNCTION
> END IF
> IF n = 0 THEN
> LET ackermann=ackermann(m-1,1)
> EXIT FUNCTION
> END IF
> LET ackermann=ackermann(m-1,ackermann(m,n-1))
> END FUNCTION
>
> A number takes 8 bytes, correct? That would mean that this would take 16 bytes plus some for the return address. Is the total number easily calculable?

That is right for BASICAcc and BASICAcc2 binary mode.
Single numeric variable Parameters takes 8 bytes for each, and the return address takes 8 bytes on the 64 bit mode.

You can not use Wiki syntax
You are not logged in. To discriminate your posts from the rest, you need to pick a nickname. (The uniqueness of nickname is not reserved. It is possible that someone else could use the exactly same nickname. If you want assurance of your identity, you are recommended to login before posting.) Login

#### Re: Stack Usage? (2018-07-07 17:25 by SHIRAISHI Kazuo #81585)

That was a wrong conclusion.
The assembler code for the ackermann function is as follows.
System stack wastes more memory at least 112 bytes for one call.

output\BASICunit.pas:43 begin
0000000100050D30 55 push %rbp
0000000100050D31 4889e5 mov %rsp,%rbp
0000000100050D34 488d6424a0 lea -0x60(%rsp),%rsp
0000000100050D39 48894de8 mov %rcx,-0x18(%rbp)
0000000100050D3D c5fb114df8 vmovsd %xmm1,-0x8(%rbp)
0000000100050D42 c5fb1155f0 vmovsd %xmm2,-0x10(%rbp)

You can not use Wiki syntax
You are not logged in. To discriminate your posts from the rest, you need to pick a nickname. (The uniqueness of nickname is not reserved. It is possible that someone else could use the exactly same nickname. If you want assurance of your identity, you are recommended to login before posting.) Login

#### Re: Stack Usage? (2018-07-07 18:47 by toml12953 #81588)

> That was a wrong conclusion.
> The assembler code for the ackermann function is as follows.
> System stack wastes more memory at least 112 bytes for one call.
>
> output\BASICunit.pas:43 begin
> 0000000100050D30 55 push %rbp
> 0000000100050D31 4889e5 mov %rsp,%rbp
> 0000000100050D34 488d6424a0 lea -0x60(%rsp),%rsp
> 0000000100050D39 48894de8 mov %rcx,-0x18(%rbp)
> 0000000100050D3D c5fb114df8 vmovsd %xmm1,-0x8(%rbp)
> 0000000100050D42 c5fb1155f0 vmovsd %xmm2,-0x10(%rbp)

So I should add 112 to stksiz on each iteration?

You can not use Wiki syntax
You are not logged in. To discriminate your posts from the rest, you need to pick a nickname. (The uniqueness of nickname is not reserved. It is possible that someone else could use the exactly same nickname. If you want assurance of your identity, you are recommended to login before posting.) Login

#### Re: Stack Usage? (2018-07-08 09:31 by SHIRAISHI Kazuo #81592)

I found the stack size change by 160 bytes on Win64.

You can modify the Pascal source codes in the code view window.

Add the following codes just above the line (* Main Program *)

var StackPrev:NativeInt=0;
function StackDiff(var StackPrev:NativeInt):double;
var
StackCur:NativeInt;
begin
asm
mov StackCur,rsp
end;
result:=StackCur-StackPrev;
StackPrev:=StackCur;
end;

And then add the following code into the appropriate position where you like.

console.PRINT([],rsNone, false ,[StackDiff(StackPrev), TNewLine.create ]);

For example,

begin
{\$MAXFPUREGISTERS 0}
console.PRINT([],rsNone, false ,[StackDiff(StackPrev), TNewLine.create ]);
result:=0;

You can not use Wiki syntax
You are not logged in. To discriminate your posts from the rest, you need to pick a nickname. (The uniqueness of nickname is not reserved. It is possible that someone else could use the exactly same nickname. If you want assurance of your identity, you are recommended to login before posting.) Login

#### Re: Stack Usage? (2018-07-14 10:30 by Shiraishi Kazuo #81601)

On BASICAcc2, you can set the system stack size exceeding 127 Mbytes
by editing SystemStackSize in the initilization section on the Pascal source codes.

Example.

.................................................
.................................................
initialization
AppName:='C:\Users\............\Desktop\ackermann';
SystemStackSize := 446676598784;
signiwidth0:=14;
TextMode := true ;
GraphMode:= false ;
BMPSize:=BMP1001;
TextProblemCoordinate:= false ;
UseCharInput:= false ;
FontCharSet:=1;
FontSize:=0;
FontName:='default';
FontStyle:=[];
Finalization
end.

You can not use Wiki syntax
You are not logged in. To discriminate your posts from the rest, you need to pick a nickname. (The uniqueness of nickname is not reserved. It is possible that someone else could use the exactly same nickname. If you want assurance of your identity, you are recommended to login before posting.) Login

#### Re: Stack Usage? (2018-07-07 17:54 by SHIRAISHI Kazuo #81587)

EXIT FUNCTION may increase the execution time.
They can be removed.
Example.

100 FUNCTION ackermann(m,n)
110 IF m = 0 THEN
120 LET ackermann=n+1
130 ELSEIF n = 0 THEN
140 LET ackermann=ackermann(m-1,1)
150 else
160 LET ackermann=ackermann(m-1,ackermann(m,n-1))
170 END IF
180 END FUNCTION
190 INPUT m,n
200 PRINT ackermann(m,n)
210 END