// Task - find the factorial of five // Method - think at a high level. Write it out nice and // cleanly in a familiar higher level language, and think // step by step about how each part works. // Notice when single stepping how jumps and global variable // accesses use PC-relative addressing // x = 5 // fact = 1 // while (x > 1) // { fact *= x; // x -= 1; } .makeexe jump start // x = 5 x: .data 5 // initialising a global variable: this code // is not "reentrant". After partially executing // you can't just jump back to start if you // want to start again. It would have to be // reloaded from the executable file // fact = 1 fact: .data 1 // while (x > 1) start: loop: load r1, [x] comp r1, 1 // if x<1 Nflag=1 else Nflag=0, and // if x==1 Zflag=1 else Zflag=0 jcond leq, endloop // jump if Nflag || Zflag // { fact *= x load r1, [fact] mul r1, [x] store r1, [fact] // x -= 1 dec [x] // end of while jump loop endloop: halt