.makeexe jump main // function fff(n) // { local s1=0, s2=0, s3=0; // if (n>0) // s1 = fff(n-1); // s2 = n; // print n as single digit // if (n>1) // s3 = fff(n/2); // return s1+s2+s3; } // n is 1st (only) parameter: FP+2 // s1 is 1st local: FP-1 // s2 is 2nd local: FP-2 // s3 is 3rd local: FP-3 fff: // standard function entry code push fp store sp, fp sub sp, 3 // s1=0; load r1, 0 store r1, [fp-1] // s2=0; load r1, 0 store r1, [fp-2] // s3=0; load r1, 0 store r1, [fp-3] // if (n>0) s1=fff(n-1) // if n<=0 skip over next to skip1 compz [fp+2] jcond leq, skip1 // s1=fff(n-1) load r1, [fp+2] sub r1, 1 push r1 call fff add sp, 1 store r1, [fp-1] skip1: // s2=n; load r1, [fp+2] store r1, [fp-2] // print n as digit load r1, [fp+2] add r1, '0' type r1 // if (n>1) s3=fff(n/2) // if n<=1 skip over next to skip2 load r1, [fp+2] comp r1, 1 jcond leq, skip2 // s3=fff(n/2) load r1, [fp+2] div r1, 2 push r1 call fff add sp, 1 store r1, [fp-3] skip2: // return s1+s2+s3; load r1, [fp-1] add r1, [fp-2] add r1, [fp-3] // standard function exit code load sp, fp pop fp ret main: // fff(4); push 4 call fff add sp, 1 // put result in r9 just so it stands out load r9, r1 halt