// the library definition file. // defines two functions: fact and print // Very bad style: the functions do not take // parameters, they use global variables that // are defined elsewhere .export fact, print .import facin, facout // factorial. The plan: // r2 will be the answer so far. // r3 is the loop counter, starting from N and // working down towards 0. fact: load r2, 1 // r2 = 1 load r3, [facin] // r3 = N loop: comp r3, 0 // compare r3 with zero jcond leq, done // if r3<=0 it's all over mul r2, r3 // r2 *= r3 dec r3 // r3 -= 1 jump loop // back around the loop again done: store r2, [facout] // save the answer ret // return // print. The plan. // Print number in decimal in the Australian way, // digits reversed. // r5 is number to be printed. Keep dividing it by ten // until it goes away. For each value of r5, r5 % 10 is // the next digit to be printed. print: load r5, [facout] // r5 is N ploop: load r1, r5 // r1 = temp copy of N mod r1, 10 // r1 is least sig. digit of N add r1, '0' // convert to ascii type r1 // send the character div r5, 10 // N /= 10 comp r5, 0 // compare N with 0 jcond gtr, ploop // back round the loop if more type '\n' // newline for tidiness ret // return