Advertisement
Guest User

prog.s

a guest
Apr 25th, 2011
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. ! main for factorial program
  2. loadi 0 1 ! line 0, R0 = fact(R1)
  3. read 1 ! input R1
  4. call 6 ! call fact
  5. load 0 33 ! receive result of fact
  6. write 0
  7. halt
  8. ! fact function
  9. compri 1 1 ! line 6
  10. jumpe 14 ! jump over the recursive call to fact if
  11. jumpl 14 ! R1 is less than or equal 1
  12. call 16 ! call mult (R0 = R0 * R1)
  13. load 0 34 ! receive result of mult
  14. subi 1 1 ! decrement multiplier (R1) and multiply again
  15. call 6 ! call fact
  16. load 0 33
  17. store 0 33 ! line 14, return R0 (result of fact)
  18. return
  19. ! mult function
  20. loadi 2 8 ! line 16, init R2 (counter)
  21. loadi 3 0 ! init R3 (result of mult)
  22. shr 1 ! line 18 (loop), shift right multiplier set CARRY
  23. store 2 35 ! save counter
  24. getstat 2 ! to find CARRY's value
  25. andi 2 1
  26. compri 2 1
  27. jumpe 25 ! if CARRY==1 add
  28. jump 26 ! otherwise do nothing
  29. add 3 0
  30. shl 0 ! make multiplicand ready for next add
  31. load 2 35 ! restore counter
  32. subi 2 1 ! decrement counter
  33. compri 2 0 ! if counter > 0 jump to loop
  34. jumpg 18
  35. store 3 34 ! return R3 (result of mult)
  36. return
  37. noop ! line 33, fact return value
  38. noop ! line 34, mult return value
  39. noop ! line 35, mult counter
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement