EXAMPLE OF FACTORIAL WRITTEN ON ONE FLAT INSTRUCTION
====================================================
int factorial(x)
int x;
{
if (x>0)
return(factorial(x-1)*x);
else
return(1);
}
Initial Stack RAM when invoked. Initial Static RAM when invoked.
| | | |
|---------------------------| |-----|
| | | |
|---------------------------| |-----|
| | | 1 | factorial.static.1
|---------------------------| |-----|
| x (operand for factorial) | | #07 | factorial.static.entry.02 PIN ADD
|---------------------------| |-----|
| Return PIN ADDRESS | | #01 | factorial.static.entry.01 PIN ADD
|---------------------------| |-----|
factorial.static.entry.01 holds the pin address #01
factorial.static.entry.02 holds the pin address #07
factorial.static.1 holds the constant 1
factorial.automatic.x holds the value of x for each invocation
============================================================================
Send Function 1 Send Function 2
PIN #01 PIN #02
| |
|-------| |-------|
| #03 | | #06 |
|-------| |-------|
Function Register 1 Function Register 2
|------------------------------| |-----------------------|
|pop stack and save in | |subtract using |
|factorial.automatic.x | |factorial.static.1 |
|------------------------------| |-----------------------|
Open Data Register
PIN #03
|
|---------------|
| Factorial |
| Operand |
| From Stack |
|---------------|
CC1 PIN CC2 PIN CC3 PIN
| | |
|-------| |-------| |-------|
| #10 | | #10 | | #02 |
|-------| |-------| |-------|
| < | | 0 | | > |
|-------| |-------| |-------|
===========================================================================
Send Function 1 Send Function 2
PIN #04 PIN #05
| |
|-------| |-------|
| #05 | | NULL |
|-------| |-------|
Function Register 1 Function Register 2
|--------------------------------| |--------------------------------|
|push stack using | |push stack and CALL using |
|factorial.static.entry.02 (#07) | |factorial.static.entry.01 (#01) |
|--------------------------------| |--------------------------------|
Open Data Register
PIN #06
|
|---------------|
| |
| x-1 |
| |
|---------------|
CC1 PIN CC2 PIN CC3 PIN
| | |
|-------| |-------| |-------|
| #04 | | #04 | | #04 |
|-------| |-------| |-------|
| < | | 0 | | > |
|-------| |-------| |-------|
============================================================================
Send Function 1 Send Function 2
PIN #07 PIN #08
| |
|-------| |-------|
| #09 | | NULL |
|-------| |-------|
Function Register 1 Function Register 2
|----------------------------| |------------------|
|pop stack and multipy using | |pop stack to temp |
| | |push stack |
|factorial.automatic.x | |RETURN using temp |
|----------------------------| |------------------|
Open Data Register
PIN #09
|
|----------------|
| |
|x*factorial(x-1)|
| |
|----------------|
CC1 PIN CC2 PIN CC3 PIN
| | |
|-------| |-------| |-------|
| #08 | | #08 | | #08 |
|-------| |-------| |-------|
| < | | 0 | | > |
|-------| |-------| |-------|
============================================================================
Send Function 1 Send Function 2
PIN #10 PIN #11
| |
|-------| |-------|
| NULL | | |
|-------| |-------|
Function Register 1 Function Register 2
|------------------------------| |-----------------------|
|pop stack to temp | | |
|push using factorial.static.1 | | |
|RETURN using temp | | |
|------------------------------| |-----------------------|
Open Data Register
PIN #12
|
|---------------|
| |
| |
| |
|---------------|
CC1 PIN CC2 PIN CC3 PIN
| | |
|-------| |-------| |-------|
| | | | | |
|-------| |-------| |-------|
| < | | 0 | | > |
|-------| |-------| |-------|
============================================================================
|