Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.66 KB | None | 0 0
  1. ;Patrick Huie
  2. ;11/9/2019
  3. ;Assignment #3 - Calculator
  4.  
  5. base !10
  6. pagewidth 127
  7.  
  8. ;
  9. MAX_DIGITS EQU $14
  10. org $90
  11. ;
  12. ;Variables
  13. ;
  14. in_1: rmb max_digits
  15. in_2: rmb max_digits
  16. sum: rmb max_digits
  17. temp: rmb 1 ;parameter for nibble_shift, instruction, and find_msd
  18. display_pointer: rmb 1
  19. digit_pointer: rmb 1
  20. msd: rmb 1 ;address of most significant digit in sum that is not 0
  21.  
  22.  
  23. org $fffe
  24. fdb start
  25.  
  26. include "ioJK3.asm"
  27. ;
  28. ;This program functions as a calculator by inputting
  29. ;max_digits number of digits into two variables in_1
  30. ;and in_2 and then outputs the sum to another variable
  31. ;sum on the 7 segment display.
  32. ;
  33. start: mov #%00010000,ptb
  34. mov #%00111110,ddrb
  35. bset 0,config1
  36. rsp
  37. clra
  38. jsr digit
  39. ldx #in_1
  40. bsr var_init
  41. bra collect_input_1
  42.  
  43.  
  44. var_init: clr {max_digits + max_digits},x ;Clear sum
  45. clr max_digits,x ;Clear in_2
  46. clr ,x ;Clear in_1
  47. incx ;Move to next
  48. ;byte for these
  49. ;3 values
  50. cpx #{in_1 + max_digits}
  51. bcs var_init
  52. clr temp ;Clear temp
  53. var_end: rts
  54.  
  55. ;This portion of the program takes in the input
  56. ;from the keyboard in the BCD form -- 1 digit per nibble
  57. ;for in_1, in_2, and sum
  58. ;
  59. ;Input for first number
  60. collect_input_1: mov #in_1,display_pointer ;Pass the address of in_1 to eight_digits
  61. lsl display_pointer
  62. bclr 0,display_pointer
  63. jsr eight_digits ;to display it to the 7-segment display
  64.  
  65. jsr new_key
  66. mov #in_1,temp ;Pass in_1 as a parameter
  67. bsr instruction ;If not 0-9 process the proper function
  68. ;of the inputted key
  69.  
  70. psha
  71. ldhx #{in_1 + max_digits - 2}
  72. mov #{in_1 - 1},temp ;Pass in_1 as a parameter
  73. ;by setting temp to its value
  74. bsr nibble_shift
  75. pula
  76. ora in_1
  77. sta in_1
  78.  
  79. bra collect_input_1
  80.  
  81. ;Input for second number
  82. collect_input_2: mov #in_2,display_pointer ;Pass the address of in_2 to eight_digits
  83. lsl display_pointer
  84. bclr 0,display_pointer
  85. jsr eight_digits ;to display it to the 7-segment display
  86.  
  87. jsr new_key
  88. mov #in_2,temp ;Pass in_2 as a parameter
  89. bsr instruction ;If not 0-9 process the proper function
  90. ;of the inputted key
  91.  
  92. psha
  93. ldhx #{in_2 + max_digits - 2}
  94. mov #{in_2 - 1},temp ;Pass in_2 as a parameter
  95. ;by setting temp to its value
  96. bsr nibble_shift
  97. pula
  98. ora in_2
  99. sta in_2
  100.  
  101. bra collect_input_2
  102.  
  103. ;Shifts each digit up a nibble
  104. nibble_shift: lda 1,x
  105. nsa ;Swap nibbles and set lower nibble
  106. and #$F0 ;to 0
  107. sta 1,x
  108. cpx temp ;If at the LSD then do not or with
  109. beq nibble_end ;upper nibble of next lowest byte
  110. lda ,x ;or the lower nibble with the
  111. nsa ;upper nibble of the next lowest byte
  112. and #$0F
  113. ora 1,x
  114. sta 1,x
  115. decx
  116. bra nibble_shift
  117. nibble_end: rts
  118.  
  119.  
  120. ;If the input entered is not a number this subroutine performs the proper
  121. ;function depending on the value. Takes temp as param
  122. instruction:
  123. clear: cmp #$C ;If "C" pressed clear all vars
  124. bne add_key ;by calling var_init
  125. ldx #in_1
  126. jsr var_init
  127. rsp
  128. bra collect_input_1
  129. add_key: cmp #$A
  130. bcs instr_end
  131. rsp
  132. psha
  133. lda temp
  134. cmp #in_2
  135. beq add_key_2
  136. pula
  137. add_key_1: cmp #$A
  138. bhi collect_input_1
  139. beq collect_input_2 ;If "A" pressed then branch to input
  140. ;second number -- end of input
  141. add_key_2: pula
  142. cmp #$B
  143. bhi collect_input_2
  144. beq addition ;If "A" pressed then branch to perform
  145. ;calculator operation -- end of input
  146. instr_end: rts
  147.  
  148.  
  149. ;Different calculator operations -- for now just add
  150. ;Later -- possibly -- add functionality for sub, mul,
  151. ;and div
  152. addition: mov #sum,display_pointer ;After the operation is applied
  153. lsl display_pointer ;display_pointer will have to
  154. bclr 0,display_pointer ;point to sum -- display_pointer
  155. ;is not needed for the rest
  156. ;of operation
  157. clc ;Clear carry for first digit
  158. ldx #in_1
  159. add_loop: lda ,x
  160. adc max_digits,x ;Add the first 2 numbers together
  161. ;which are spaced apart by max_digits
  162. daa
  163. sta {max_digits + max_digits},x ;Sum is spaced 2*max_digits
  164. ;away from in_1
  165. incx
  166. cbeqx #{in_1 + max_digits},display
  167. bra add_loop
  168.  
  169.  
  170. ;Display the passed parameter pointed to by the
  171. ;display_pointer to the 7-segment display
  172. display: ldhx #{sum + max_digits}
  173. mov #sum,temp
  174. bsr find_msd
  175. scroll_display: bsr eight_digits
  176. invalid_key: jsr new_key
  177. disp_right: cmp #$F ;If "F" pressed scroll right
  178. bne disp_left
  179. psha
  180. lda display_pointer
  181. cmp #{msd-7} ;If the display_pointer is higher
  182. bcc invalid_key ;than msd-7 wait for another input
  183. inc display_pointer ;otherwise scroll right and display
  184. ;with eight_digits
  185. pula
  186. bra scroll_display
  187. disp_left: cmp #$E ;If "E" pressed scroll left
  188. bne clear_2
  189. psha
  190. lda display_pointer
  191. cmp #sum ;If the display_pointer is equal
  192. beq invalid_key ;to #sum wait for another input
  193. dec display_pointer ;otherwise scroll left and display
  194. ;with eight_digits
  195. pula
  196. bra scroll_display
  197. clear_2: cmp #$C ;If "C" pressed clear all vars
  198. bne invalid_key
  199. ldx #in_1
  200. jsr var_init
  201. jmp collect_input_1
  202.  
  203.  
  204. ;This subroutine finds the MSD of the param (temp).
  205. find_msd: decx ;Move to next byte
  206. cpx temp ;If at last byte
  207. beq found_msd ;then set msd to #sum
  208. lda ,x
  209. and #$F0 ;Check if upper nibble 0
  210. sec ;Set C to indicate upper nibble
  211. tsta
  212. bne found_msd
  213. lda ,x
  214. and #$0F ;Check if lower nibble 0
  215. clc ;Clear C to indicate lower nibble
  216. tsta
  217. beq find_msd ;If 0's in both nibbles
  218. ;move to next byte
  219. found_msd: stx msd ;Store the address of first
  220. rol msd ;non-zero value with bit 0
  221. rts ;indicating upper or lower nibble
  222.  
  223.  
  224.  
  225. ;Takes the parameter pointed to by the display_pointer
  226. ;and puts it into digits0-7
  227. eight_digits: mov #digit0,digit_pointer ;Start digit_pointer at digit0
  228. eight_loop: lda display_pointer ;Load LSD
  229. sec
  230. rora ;Convert to actual mem address
  231. tax ;and load into h:x register
  232. clrh
  233. lda ,x ;to get data at display_pointer
  234. bcc isolate_nibble ;If accessing lower nibble don't nsa
  235. nsa
  236. isolate_nibble: and #$0F
  237. jsr ascii_7seg ;Convert to ASCII
  238. ldx digit_pointer ;Load the digit pointer
  239. sta ,x ;and store ASCII value
  240. ;at where x points to
  241. inc display_pointer
  242. inc digit_pointer
  243. lda digit_pointer
  244. cmp #{digit7 + 1} ;Check to see if all eight
  245. bne eight_loop ;digits have been filled
  246. rts
  247.  
  248.  
  249. ;Old scroll_display:
  250. ;lda display_pointer
  251. ;psha
  252. ;sec
  253. ;rora
  254. ;tax
  255. ;clrh
  256. ;pula
  257. ;sta display_pointer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement