Re: [cc65] odd results

Date view Thread view Subject view

From: BlackJack/Civitas (blackjack_at_civitas64.de)
Date: 2001-02-26 19:36:03


Hi troy,

>[code]
>    if (more==1)
>        {
>             *clearkeybuffer=0;
>             VIC.spr_ena=0;
>             return pmenu; // *** this is when I want to exit
>        }
>    *time1=0;	// delay the user imput to stop sprite run-a-way
>    while (*time1<10)
>        {}
>    choiceA(pmenu);

This is not a 'jmp choiceA(pmenu)' but a 'jsr'! So it doesn't jump to the 
start of this routine but returns if the 'return pmenu' is executed in the 
if(more==1) branch. The result is simply thrown away and then the next 
line (return 0;) is reached!

A quick fix would be to write 'return choiceA(pmenu);' (and forget the 
'return 0;') but then you are using a recursive function call[1] where it 
is absolutly unecessary.

The standard way of executing a codeblock again and again is to put it 
into an endless loop with  'for (;;) { ...code... }' 
or 'while (1) { ...code... }'

Somewhere in ...code... you have to use 'return' or 'break' to exit this 
loop.

>    return 0; // **** bogus return to make the compiler happy
>                     // and the reason I keep getting 0 for a carry value
>                     // what ever I put here get returned. not pmenu
>above.

As I mentioned above this return is not that bogus. If you don't hit the 
return key the first time this routine runs, this line will be executed. 
CC65 let you leave this return out but inserts it silently for you.

[1] recursive functions call:

Just a quick description. Everytime a function is called it allocates 
memory for the given argument(s) on the stack (and this is also true for 
local variables that are not declared 'static').

So if you call the same function within the function body, the local 
variables are allocated again and are independent from the first call.

Let's have a look at this code:

1:void f(unsigned char x) {
2:	if (x>0) f(x-1);
3:	printf("%d ", x);
4:}

This will print all numbers from 0 to x. Let's trace 'f(2)' step by step:

Step (Linenumber)
1. (1) x is created on top of the stack and set to 2 (Stack = 2)
2. (2) x is greater than 0 so call f with x-1 = 1 as argument. This is a
      jsr not a jmp so we will return here!)
3. (1) x is created on top of the stack and set to 2 (Stack = 1,2)
4. (2) x is still greater than 0 so we call f again with x-1 = 0
5. (1) again a local x is created on the stack (Stack = 0,1,2)
6. (2) now the if is skipped.
7. (3) print x (=0), remove x from the stack (Stack = 1,2) and return
       to the point where the function was called (that was in step 4)
8. (3) we are now in line 3 again (right behind the f(x-1);) and print
       x (=1), remove x from the stack (Stack = 2) and again return to
       the caller (this one was in step 2)
9. (3) print x (=2), remove x from stack and return to the caller.       

One popular examples of recursive functions are fibonacci numbers. The 
definition is: Every fibonacci number is the sum of the two previous. With 
fib(0) = 0 and fib(1) = 1.

unsigned long fib(unsigned long n) {
	if (n<2) return n;
	return fib(n-1) + fib(n-2);
}

Ciao,
	Marc 'BlackJack' Rintsch
-- 
_ _____  __  __ _____________________________________________________
 /  __/ /_/  \_\ CiViTAS - "Lameness rulez"
/  /   ____  ____ http://www.civitas64.de
\  \_ / /\ \/ /\ \ mailto:blackjack_at_civitas64.de  
 \___/_/  \__/  \_\ C64 forever...
----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo_at_musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.


Date view Thread view Subject view

This archive was generated by hypermail 2.1.3 : 2001-12-14 22:05:39 CET