Guest User

Untitled

a guest
Feb 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. ;calculates the result of squaring the number. example :
  2. ;10 squared = sum of adding 10, ten times.
  3.  
  4. BR main
  5.  
  6. num: .word 0
  7. val: .word 0
  8. res: .word 0
  9. msg1: .ascii "Number: \x00"
  10. msg2: .ascii " is invalid \x00"
  11. msg3: .ascii " Squared = \x00"
  12.  
  13.  
  14. checkVal: LDA 2,s
  15. CPA 0,i ;checks if number > 0
  16. brgt valid
  17. LDA 0,i ; puts 0 in the return value(to be interpreted as 'false')
  18. STA 4,s
  19. RET0
  20. valid: LDA 1,i ; puts 1 in the return value(to be interpreted as 'true')
  21. STA 4,s
  22. RET0
  23.  
  24.  
  25. sqNum: SUBSP 4,i ; 2 local variables
  26. LDA 0,i
  27. STA 2,s
  28. while: LDA 2,s ;squares the number
  29. CPA 6,s
  30. BRGE out
  31. LDA 2,s
  32. ADDA 1,i
  33. STA 2,s
  34. LDA 0,s
  35. ADDA 6,s
  36. STA 0,s
  37. BR while
  38. out: LDA 0,s ; done calculating the result
  39. STA 8,s ; save result in return value.
  40. ADDSP 4,i
  41. RET0
  42.  
  43. main: DECI num,d
  44. SUBSP 2,i ; return value
  45. SUBSP 2,i ; parameter
  46. LDA num,d
  47. STA 0,s
  48. CALL checkVal
  49. ADDSP 2,i ; parameters go away
  50. LDA 0,s
  51. STA val,d
  52. ADDSP 2,i ;clear return value
  53. LDA val,d
  54. brgt cont
  55. STRO msg1,d
  56. DECO num,d
  57. STRO msg2,d
  58. BR done
  59.  
  60. cont: SUBSP 2,i ; return value
  61. SUBSP 2,i ;parameter
  62. LDA num,d
  63. STA 0,s
  64. CALL sqNum
  65. ADDSP 2,i ; clear parameter
  66. LDA 0,s
  67. STA res,d
  68. ADDSP 2,i ; clear return value
  69. STRO msg1,d
  70. DECO num,d
  71. STRO msg3,d
  72. DECO res,d
  73.  
  74. done: STOP
  75. .end
Add Comment
Please, Sign In to add comment