Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 67.70 KB | None | 0 0
  1. ; multi-segment executable file template.
  2.  
  3. data segment
  4. name_contents db "c:\contents.bin", 0
  5. handler dw 0
  6.  
  7. string_menu_1 db "- Import Spreadsheet" ;length=20
  8. string_menu_2 db "- Show spreadsheet" ;length=18
  9. string_menu_3 db "- Edit spreadsheet" ;length=18
  10. string_menu_4 db "- Export spreadsheet" ;length=20
  11. string_menu_5 db "- About" ;length=7
  12. string_menu_6 db "- Exit" ;length=6
  13.  
  14. string_formula db "Formula"
  15. string_result db "Result"
  16. string_menu db "Menu"
  17. string_filename db "Filename"
  18. string_error db "ERROR FILE UNEXISTENT"
  19. string_remainder db ";R:"
  20.  
  21. string_about_1 db "Programa feito por:" ;length=19
  22. string_about_2 db "Francisco Peres N: 50034";length=25
  23. string_about_3 db "Joao Ferreira N: 50608" ;length=23
  24.  
  25. str_buffer db 6 dup(0)
  26. str_buffer2 db 7 dup(0)
  27.  
  28. str_buffer_result db 6 dup(0)
  29.  
  30. remainder dw 00h
  31. num_hcell dw 04h
  32. matrix_lenght dw 160 ;estava 240 antes
  33. matrix_height dw 65
  34. column_number db 'A','1',':','0',';',"FOR:" ;buffer para export e import
  35. cell db 16 dup(-128) ;format A1, A2, A3, A4, B1, etc
  36. formula_var db 3 dup(-1) ;formato (ctre 1 e 16 de seguimento A1=0, A2=1, etc)
  37. ;coluna_var: cell_1; operation; cell_2. coluna//linha. operacao=0 formula vazia
  38. name_file db "c:\", ?
  39. ends
  40.  
  41. stack segment
  42. dw 128 dup(0)
  43. ends
  44.  
  45. code segment
  46. start:
  47. ; set segment registers:
  48. mov ax, data
  49. mov ds, ax
  50. mov es, ax
  51.  
  52. ;open contents.bin
  53. call open_contents
  54.  
  55. draw_menu:
  56. call reiniciateVideo
  57.  
  58. call menu
  59. nothing_clicked:
  60. call keepCheckingMouse
  61.  
  62. call checkMenu
  63.  
  64. cmp dh, 02h
  65. je nothing_clicked
  66.  
  67. cmp dh, 01h
  68. jne draw_menu
  69.  
  70. ;saves contents.bin
  71. call save_contents
  72.  
  73. mov ax, 4c00h ; exit to operating system.
  74. int 21h
  75.  
  76. ;*****************************************************************
  77. ; menu -
  78. ; descricao: prints menu
  79. ; input - no input needed
  80. ; output - nothing
  81. ; destroi - ax, bx, cx, dx
  82. ;*****************************************************************
  83.  
  84. menu proc
  85. mov al, 01h
  86. mov bx, 0Fh
  87. mov dx, 02h
  88.  
  89. mov cx, 14h;length of string_1
  90. mov bp, offset string_menu_1
  91.  
  92. imprime_string:
  93. inc dh
  94. inc dh ;goes to next line
  95. push bx ;bh is used as an index of how many strings
  96. ;have been printed so it must be pushed before
  97. xor bh, bh ;printing a string due to the int's parametres
  98. mov ah, 13h
  99. int 10h ;prints string
  100.  
  101. pop bx
  102. inc bh ;next string
  103.  
  104. cmp bh, 02h
  105. je menu_2
  106.  
  107. cmp bh, 03h
  108. je menu_3
  109.  
  110. cmp bh, 04h
  111. je menu_4
  112.  
  113. cmp bh, 05h
  114. je menu_5
  115. ja end_of_menu
  116.  
  117. mov cx, 12h
  118. mov bp, offset string_menu_2
  119. jmp imprime_string
  120.  
  121. menu_2:
  122. mov bp, offset string_menu_3
  123. jmp imprime_string
  124.  
  125. menu_3:
  126. mov cx, 14h
  127. mov bp, offset string_menu_4
  128. jmp imprime_string
  129.  
  130. menu_4:
  131. mov cx, 07h
  132. mov bp, offset string_menu_5
  133. jmp imprime_string
  134.  
  135. menu_5:
  136. mov cx, 06h
  137. mov bp, offset string_menu_6
  138. jmp imprime_string
  139.  
  140. end_of_menu:
  141. ret
  142. menu endp
  143.  
  144. ;*****************************************************************
  145. ; checkMenu -
  146. ; descricao: checks position clicked on menu window
  147. ; input - no input needed
  148. ; output - nothing
  149. ; destroi - cx, dx
  150. ;*****************************************************************
  151.  
  152. checkMenu proc
  153. xor dh, dh
  154. ;checks if program is clicked anywhere outside the area of the printed strings
  155. cmp cx, 13h ;left limit x
  156. jb exit_checking_menu
  157. cmp cx, 178 ;right limit x
  158. ja exit_checking_menu
  159. cmp dx, 12h ;superior limit y
  160. jb exit_checking_menu
  161. cmp dx, 6Ch ;inferior limit y
  162. ja exit_checking_menu
  163. ;then tests to see if the click is between the superior limit (y)
  164. cmp dx, 1Bh ;and another y value, both delimitating the first string
  165. ja test_show
  166. call import_spreadsheet
  167. jmp done_checking_menu
  168. ;if it's not there then it tests to see if it's between the superior limit
  169. test_show: ;and an y value, delimitating the first two strings
  170. cmp dx, 2Bh
  171. ja test_edit
  172. call show_spreadsheet
  173. jmp done_checking_menu
  174.  
  175. test_edit:
  176. cmp dx, 3Bh
  177. ja test_export
  178. call edit_spreadsheet
  179. jmp done_checking_menu
  180.  
  181. test_export:
  182. cmp dx, 4Bh
  183. ja test_about
  184. call export_spreadsheet
  185. jmp done_checking_menu
  186.  
  187. test_about:
  188. cmp dx, 5Bh
  189. ja test_mmenu
  190. call show_about
  191. jmp done_checking_menu
  192.  
  193. test_mmenu:
  194. cmp dx, 6Bh
  195. ja exit_checking_menu
  196. jmp definitely_exit:
  197.  
  198. exit_checking_menu:
  199. mov dh, 02h
  200. jmp done_checking_menu
  201.  
  202. definitely_exit:
  203. mov dh, 01h
  204.  
  205. done_checking_menu:
  206. ret
  207. checkMenu endp
  208.  
  209. ;*****************************************************************
  210. ; import_spreadsheet -
  211. ; descricao: imports a file with the extension .txt
  212. ; input - no input needed
  213. ; output - dh=02h if file non-existent
  214. ; destroi - ax, dx, cx, dx, bp, di
  215. ;*****************************************************************
  216.  
  217. import_spreadsheet proc
  218. call importExportFilename ;gets filename which stays in string pointed by bp
  219. call fopen ;tries to open file
  220. jc file_unexistent
  221.  
  222. mov handler, ax
  223. mov dx, offset column_number ;this vector is used for both temporary values and
  224. add dx, 03h ;reading from file point to temporary position in vector
  225.  
  226. import_next_column:
  227. mov di, offset cell
  228. mov ah, 3Fh ;get next column char (or F (OR) if all columsn have been read)
  229. mov bx, handler
  230. mov cx, 01h
  231. int 21h
  232. mov bx, dx ;copies the address of temporary position to bx
  233. ;so it can be directly referenced
  234. cmp ax, 00h ;if at EOF jumps to end, should not be needed
  235. je end_of_import
  236. ;checks what character was read
  237. cmp [bx], 'A'
  238. je import_number
  239. add di, 04h ;the first 4 values from vector cell belong to column A
  240. ;so by adding to di 4 we are now referencing column B
  241. cmp [bx], 'B'
  242. je import_number
  243. add di, 04h
  244.  
  245. cmp [bx], 'C'
  246. je import_number
  247. add di, 04h
  248.  
  249. cmp [bx], 'D'
  250. je import_number
  251. ;if it's none of the columns, we've finished reading all the columns
  252. ;and are now reading the formula
  253. ;formula start
  254. mov di, offset formula_var
  255. mov cx, 01h
  256. eat_for: ;reads until ':' is found
  257. mov ah, 3Fh
  258. mov bx, handler
  259. int 21h
  260. mov bx, dx
  261. cmp [bx], ':'
  262. jne eat_for
  263.  
  264. next_formula_var_0: ;reads character belonging to column
  265. push cx
  266. mov ah, 3Fh
  267. mov bx, handler
  268. mov cx, 01h
  269. int 21h
  270. mov [di], 00h
  271. cmp ax, 00h ;if formula has no value
  272. jne not_eof
  273. pop cx
  274. mov [di+1], -1
  275. jmp end_of_import
  276.  
  277. not_eof:
  278. mov bx, dx
  279. cmp [bx], 'B'
  280. jb next_formula_var_1 ;if it's 'A'
  281. pushf
  282. add [di], 04h
  283. popf
  284. je next_formula_var_1 ;if it's 'B'
  285. cmp [bx], 'C'
  286. pushf
  287. add [di], 04h
  288. popf
  289. je next_formula_var_1 ;if it's 'C'
  290. add [di], 08h ;if it's 'D'
  291.  
  292. next_formula_var_1: ;reads character belonging to line
  293. mov ah, 3Fh
  294. mov bx, handler
  295. int 21h
  296. pop cx
  297.  
  298. mov bx, dx
  299. cmp [bx], '2'
  300. jb next_formula_var_2
  301. pushf
  302. inc [di]
  303. popf
  304. je next_formula_var_2
  305. cmp [bx], '3'
  306. pushf
  307. inc [di]
  308. popf
  309. je next_formula_var_2
  310. inc [di]
  311.  
  312. next_formula_var_2: ;if ch=1 it means we've already read the column and number twice
  313. cmp ch, 01h
  314. je end_of_import
  315.  
  316. inc di ;reads operation sign
  317. mov ah, 3Fh
  318. mov bx, handler
  319. int 21h
  320.  
  321. mov bx, dx
  322. inc ch
  323.  
  324. mov ah, [bx] ;puts operation sign in var
  325. mov [di], ah
  326. inc di
  327. jmp next_formula_var_0
  328. ;formula
  329.  
  330.  
  331. import_number: ;we know the column now we read the line
  332. mov ah, 3Fh
  333. mov bx, handler
  334. int 21h ;read the character of the line
  335.  
  336. mov bx, dx
  337. xor al, al
  338. ;alike what we did with the column we compare the character
  339. cmp [bx], '1' ;and the di is set to increase accordingly
  340. je pre_import_next_number
  341.  
  342. cmp [bx], '2'
  343. jne import_number_3
  344. inc di
  345. jmp pre_import_next_number
  346.  
  347. import_number_3:
  348. cmp [bx], '3'
  349. jne import_number_4
  350. add di, 02h
  351. jmp pre_import_next_number
  352.  
  353. import_number_4:
  354. add di, 03h
  355.  
  356. pre_import_next_number: ;reads the character ':'
  357. mov [di], 00h ;resets value in cell
  358. mov ah, 3Fh
  359. mov bx, handler
  360. int 21h
  361. import_next_number: ;reads the actual ascii characters belonging to the number
  362. push ax ;and converts them to decimal
  363. push cx ;al tells us if number is negative or not and cx
  364. mov ah, 3Fh
  365. mov bx, handler
  366. mov cx, 01h
  367. int 21h ;reads a character
  368.  
  369. mov bx, dx
  370. pop cx
  371. pop ax
  372.  
  373. cmp [bx], ';' ;if all characters of the number have been read
  374. je pre_import_next_column
  375.  
  376. cmp [bx], '-' ;if number is negative
  377. je import_num_negative
  378.  
  379. mov bl, [bx] ;puts the character read in bl
  380. cmp cx, 01h ;cx=1 on the first ascii number
  381. je import_tens
  382.  
  383. push ax
  384. sub bl, '0' ;converts ascii character to decimal
  385. mov ax, [di]
  386. push dx
  387. mul cx ;it will multiply the value either by 10
  388. add bl, al ;depending if it's the first or third character
  389. pop dx ;adds calculated value to value in cell
  390. mov [di], bl
  391. pop ax
  392. jmp import_next_number
  393.  
  394. import_tens: ;if it's the first character read
  395. sub bl, '0' ;converts it
  396. mov [di], bl ;then puts value in cell
  397. mov cx, 0Ah ;
  398. jmp import_next_number
  399.  
  400. import_num_negative: ;if it's negative we put -1 in al
  401. mov al, -1
  402. jmp import_next_number
  403.  
  404. pre_import_next_column: ;if number read is negative
  405. cmp al, -1 ;converts it before going to the next number
  406. jne import_next_column
  407. neg [di]
  408. jmp import_next_column
  409.  
  410. file_unexistent: ;prints error in case file does not exist
  411. mov ax, 1301h
  412. mov bx, 000Fh
  413. mov cx, 0015h
  414. mov dx, 1003h
  415. mov bp, offset string_error
  416. int 10h
  417. mov dh, 02h ;due to the way the menu works this will make it so
  418. jmp end_for_file_unexistent ;the menu isn't redrawn and the error keeps on screen
  419.  
  420. end_of_import:
  421. call fclose
  422. end_for_file_unexistent:
  423. ret
  424. import_spreadsheet endp
  425.  
  426. ;*****************************************************************
  427. ; show_spreadsheet -
  428. ; descricao: shows the spreadsheet on memory
  429. ; input -
  430. ; output -
  431. ; destroi -
  432. ;*****************************************************************
  433.  
  434. show_spreadsheet proc
  435. call reiniciateVideo
  436.  
  437. call createMatrix
  438.  
  439. call createMenuBox
  440.  
  441. call createFormulaBox
  442.  
  443. call createResultBox
  444.  
  445. call showLetters
  446.  
  447. call showNumbers
  448.  
  449. call loadValues
  450.  
  451. mov ax, 00h ;initialize mouse
  452. int 33h
  453.  
  454. keep_checking_show:
  455. call keepCheckingMouse
  456.  
  457. call checkMenuClicked
  458.  
  459. cmp bx, 255
  460. jne keep_checking_show
  461.  
  462. ret
  463. show_spreadsheet endp
  464.  
  465. ;*****************************************************************
  466. ; edit_spreadsheet -
  467. ; descricao: shows and allows the user to edit the spreadsheet
  468. ; input -
  469. ; output -
  470. ; destroi -
  471. ;*****************************************************************
  472.  
  473. edit_spreadsheet proc
  474. call reiniciateVideo
  475.  
  476. call createMatrix
  477.  
  478. call createMenuBox
  479.  
  480. call createFormulaBox
  481.  
  482. call createResultBox
  483.  
  484. call showLetters
  485.  
  486. call showNumbers
  487.  
  488. call loadValues
  489.  
  490. mov ax, 00h ;initialize mouse
  491. int 33h
  492.  
  493. ;wait for user input
  494. keep_checking_edit:
  495. call keepCheckingMouse
  496.  
  497. call cANDeCell ;check and evaluate cell
  498.  
  499. cmp bx, 255 ;exit to MENU
  500. jne keep_checking_edit
  501.  
  502. ret
  503. edit_spreadsheet endp
  504.  
  505. ;*****************************************************************
  506. ; export_spreadsheet -
  507. ; descricao: exports a file with the extension .txt the spreadsheet in the memory
  508. ; input - no input needed
  509. ; output -
  510. ; destroi - ax, dx, cx, dx, bp, di
  511. ;*****************************************************************
  512.  
  513. export_spreadsheet proc
  514. call importExportFilename
  515. call fcreate
  516.  
  517. write_export:
  518. mov handler, ax
  519. xor ax, ax
  520. mov dx, offset column_number ;this vector is used for both temporary values and
  521. mov bx, dx ;reading from file point to temporary position in vector
  522. mov di, offset cell
  523. dec di ;compensates first increment
  524. mov [bx], 'A' ;resets the first two variables in the vector
  525. mov [bx+1], '1'
  526.  
  527. next_cell: ;di is incremented to point to the next cell to write
  528. inc di
  529. mov al, [di]
  530.  
  531. cmp al, -128 ;if the value=/= -128 then it writes the cell info
  532. jne write_cell_info
  533.  
  534. test_next_cell: ;if we're not at [column/'4'] then
  535. cmp [bx+1], '4'
  536. je export_form_resu
  537. inc [bx+1] ;we increment it and test the next cell
  538. jmp next_cell
  539.  
  540. export_form_resu: ;if we're at [column/'4'] then
  541. mov [bx+1], '1' ;we increment the column and put line='0'
  542. inc [bx] ;and test to see if we've read all the columns
  543. cmp [bx], 'E' ;by checking if column='E'
  544. jne next_cell
  545.  
  546.  
  547. ;FORMULA
  548. add dx, 05h ;writes in file "FOR:"
  549. mov cx, 04h
  550. mov bx, handler
  551. mov ah, 40h
  552. int 21h
  553.  
  554. mov di, offset formula_var
  555. dec dx
  556. dec dx
  557. cmp [di+1], -1 ;if formula is empty don't write anything
  558. jne next_formula_var_ex_0
  559. jmp end_of_export
  560.  
  561. next_formula_var_ex_0: ;divides the cell in formula by 4
  562. mov al, [di]
  563. push dx
  564. xor dx, dx
  565. mov cl, 04h
  566. div cl
  567. pop dx
  568. mov bx, dx
  569. push ax
  570.  
  571.  
  572. cmp al, 01h ;depending on the result we know the column
  573. jb next_formula_var_ex_1
  574. je next_formula_var_ex_2
  575.  
  576. cmp al, 02h
  577. je next_formula_var_ex_3
  578. mov [bx], 'D'
  579. jmp next_formula_var_ex_4
  580.  
  581. next_formula_var_ex_1:
  582. mov [bx], 'A'
  583. jmp next_formula_var_ex_4
  584.  
  585. next_formula_var_ex_2:
  586. mov [bx], 'B'
  587. jmp next_formula_var_ex_4
  588.  
  589. next_formula_var_ex_3:
  590. mov [bx], 'C'
  591.  
  592.  
  593. next_formula_var_ex_4: ;writes column
  594. push cx
  595. mov ah, 40h
  596. mov bx, handler
  597. mov cx, 01h
  598. int 21h
  599. pop cx
  600.  
  601. mov bx, dx
  602. pop ax
  603.  
  604.  
  605. cmp ah, 01h ;depending on the remainder we know the line
  606. jb next_formula_var_ex_5
  607. je next_formula_var_ex_6
  608.  
  609. cmp ah, 02h
  610. je next_formula_var_ex_7
  611. mov [bx], '4'
  612. jmp next_formula_var_ex_8
  613.  
  614. next_formula_var_ex_5:
  615. mov [bx], '1'
  616. jmp next_formula_var_ex_8
  617.  
  618. next_formula_var_ex_6:
  619. mov [bx], '2'
  620. jmp next_formula_var_ex_8
  621.  
  622. next_formula_var_ex_7:
  623. mov [bx], '3'
  624.  
  625. next_formula_var_ex_8: ;writes line
  626. push cx
  627. xor ch, ch
  628. mov bx, handler
  629. mov cl, 01h
  630. mov ah, 40h
  631. int 21h
  632. pop cx
  633.  
  634. cmp ch, 01h ;if the both cells info as been written
  635. je end_of_export
  636.  
  637. inc di ;writes operand
  638. mov bx, dx
  639. mov ah, [di]
  640. mov [bx], ah
  641.  
  642. mov ah, 40h
  643. mov bx, handler
  644. mov cl, 01h
  645. int 21h
  646.  
  647. inc di
  648. inc ch ;too make sure we jump to the end after writing
  649. jmp next_formula_var_ex_0 ;the second cell's info
  650. ;formula end
  651.  
  652.  
  653. write_cell_info: ;writes the first 3 characters of column_number [column, line, ':']
  654. push ax
  655. push bx
  656. mov ah, 40h
  657. mov bx, handler
  658. mov cx, 03h
  659. int 21h
  660.  
  661. pop bx
  662. pop ax
  663. mov cx, 0Ah
  664.  
  665. cmp al, 1000_0000b ;checks if cell value is negative
  666. jb test_tens
  667. mov [bx+3], '-' ;writes '-' in the file
  668. push bx
  669. mov bx, handler
  670. add dx, 03h
  671. mov cx, 01h
  672. push ax
  673. mov ah, 40h
  674. int 21h
  675. pop ax
  676.  
  677. pop bx ;turns the negative value into a positive value so it's
  678. neg al ;processed like a positive number
  679. xor ah, ah
  680. mov cx, 0Ah
  681.  
  682. test_tens: ;checks if number is <10
  683. cmp al, 0Ah
  684. jae test_hundreds
  685. add al, '0' ;if number is <10, it's converted from decimal to ascii
  686. mov [bx+3], al ;put on the vector and then written in the file
  687. push bx
  688. mov dx, bx
  689. mov bx, handler
  690. mov ah, 40h
  691. mov cx, 01h
  692. add dx, 03h
  693. int 21h
  694. jmp pre_next_cell
  695.  
  696.  
  697. test_hundreds: ;checks if number is <100
  698. xor dx, dx
  699. cmp al, 64h
  700. jae hundreds
  701. div cx ;if number is <100, it is divided by 10
  702. mov dx, bx ;the result (al) is converted and written
  703. add al, '0'
  704. mov [bx+3], al
  705. mov al, ah
  706. push ax
  707. push bx
  708. mov bx, handler
  709. mov ah, 40h
  710. mov cx, 01h
  711. add dx, 03h
  712. int 21h
  713.  
  714. pop bx ;the remainder (previously ah) is converted and written
  715. pop ax
  716. add al, '0'
  717. mov [bx+3], al
  718. push bx
  719. mov bx, handler
  720. mov ah, 40h
  721. int 21h
  722. jmp pre_next_cell
  723.  
  724.  
  725. hundreds: ;if number is >100
  726. div cx ;it is divided by 10 twice and the remainder put in the stack
  727. push dx
  728. xor dx, dx
  729. div cx
  730. push dx
  731. mov dx, bx
  732.  
  733. add al, '0' ;the result, as before, is written in the file
  734. mov [bx+3], al
  735. push bx
  736. mov bx, handler
  737. add dx, 03h
  738. mov cx, 01h
  739. mov ah, 40h
  740. int 21h
  741.  
  742. pop bx ;then like before the remainder is poped from the stack
  743. pop ax ;and written in the file twice
  744. add al, '0'
  745. mov [bx+3], al
  746. push bx
  747. mov bx, handler
  748. mov ah, 40h
  749. int 21h
  750.  
  751. pop bx
  752. pop ax
  753. add al, '0'
  754. mov [bx+3], al
  755. push bx
  756. mov bx, handler
  757. mov ah, 40h
  758. int 21h
  759.  
  760.  
  761. pre_next_cell: ;writes ';'
  762. inc dx
  763. mov ah, 40h
  764. int 21h
  765. pop bx
  766. sub dx, 4
  767. jmp test_next_cell
  768.  
  769. end_of_export:
  770. mov bx, handler
  771. call fclose
  772.  
  773. ret
  774. export_spreadsheet endp
  775.  
  776. ;*****************************************************************
  777. ; show_about -
  778. ; descricao: displays information about the creaters
  779. ; input -
  780. ; output -
  781. ; destroi -
  782. ;*****************************************************************
  783.  
  784. show_about proc
  785. call reiniciateVideo
  786.  
  787. mov dh, 01h
  788. mov al, 01h
  789. mov bh, 00h
  790. mov bl, 0Fh
  791. mov dl, 04h
  792.  
  793. mov cx, 13h ;length of string_1
  794. mov bp, offset string_about_1
  795.  
  796. imprime_string_about:
  797. inc dh
  798. inc dh ;next line
  799. push bx
  800.  
  801. xor bh, bh
  802. mov ah, 13h
  803. int 10h
  804.  
  805. pop bx
  806. inc bh ;next string
  807.  
  808. cmp bh, 02h
  809. je about_3
  810. ja end_of_writing_about
  811.  
  812. mov cx, 19h
  813. mov bp, offset string_about_2
  814. jmp imprime_string_about
  815.  
  816. about_3:
  817. mov cx, 17h
  818. mov bp, offset string_about_3
  819. jmp imprime_string_about
  820.  
  821. end_of_writing_about:
  822.  
  823. call createMenuBox
  824.  
  825. keep_checking_about:
  826. xor ax, ax
  827. int 16h
  828.  
  829. cmp al, 0Dh ;check if ENTER was pressed
  830. je about_done
  831. cmp al, 1Bh ;check if ESC was pressed
  832. jne keep_checking_about
  833.  
  834. about_done:
  835. ret
  836. show_about endp
  837.  
  838. ;*****************************************************************
  839. ; hLine -
  840. ; descricao: creates horizontal lines for the matrix
  841. ; input - bx= number of lines ; cx = horizontal margin ; dx = vertical margin ; ax = line lenght
  842. ; output -
  843. ; destroi -
  844. ;*****************************************************************
  845.  
  846. hLine proc
  847.  
  848. n_hlines:
  849. push ax
  850. push cx
  851.  
  852. h_change_pixel:
  853. push ax
  854. mov ah, 0Ch
  855. mov al, 0Fh
  856. int 10h
  857.  
  858. pop ax
  859. dec ax
  860. jz h_done
  861. inc cx
  862. jmp h_change_pixel
  863.  
  864. h_done:
  865. pop cx
  866. pop ax
  867. add dx, 16
  868. dec bx
  869. jnz n_hlines
  870.  
  871. ret
  872. hLine endp
  873.  
  874. ;*****************************************************************
  875. ; vLine -
  876. ; descricao: creates vertical lines for the matrix
  877. ; input - bx= number of lines ; cx = horizontal margin ; dx = vertical margin ; ax = matrix_height
  878. ; output -
  879. ; destroi -
  880. ;*****************************************************************
  881.  
  882. vLine proc
  883.  
  884. n_vlines:
  885. push ax
  886. push dx
  887.  
  888. v_change_pixel:
  889. push ax
  890. mov ah, 0Ch
  891. mov al, 0Fh
  892. int 10h
  893.  
  894. pop ax
  895. dec ax
  896. jz v_done
  897. inc dx
  898. jmp v_change_pixel
  899.  
  900. v_done:
  901. pop dx
  902. pop ax
  903. add cx, 40
  904. dec bx
  905. jnz n_vlines
  906.  
  907. ret
  908. vLine endp
  909.  
  910. ;*****************************************************************
  911. ; createMatrix -
  912. ; descricao: creates horizontal lines for the matrix
  913. ; input - bx= number of lines ; cx = horizontal margin ; dx = vertical margin ; ax = line lenght
  914. ; output -
  915. ; destroi -
  916. ;*****************************************************************
  917.  
  918. createMatrix proc
  919. mov bx, 05h ; se calhar temos de remover isto
  920. mov dx, 28
  921. mov cx, 20
  922. mov ax, 160 ;Matrix Lenght
  923. call hLine
  924.  
  925. mov bx, 05h
  926. mov cx, 20
  927. mov dx, 28
  928. mov ax, 65 ;Matrix Height
  929. call vLine
  930.  
  931. ret
  932. createMatrix endp
  933.  
  934. ;*****************************************************************
  935. ; createMatrix -
  936. ; descricao: creates horizontal lines for the matrix
  937. ; input - bx= number of lines ; cx = horizontal margin ; dx = vertical margin ; ax = line lenght
  938. ; output -
  939. ; destroi -
  940. ;*****************************************************************
  941.  
  942.  
  943. createMenuBox proc
  944. mov bx, 2
  945. mov ax, 40
  946. mov cx, 20
  947. mov dx, 156
  948. call hLine
  949.  
  950. mov bx, 2
  951. mov ax, 17
  952. mov cx, 20
  953. mov dx, 156
  954. call vLine
  955.  
  956. ;writes "Menu" inside the cell
  957. mov al, 1
  958. mov bl, 0Fh
  959. mov cx, 4 ;string lenght
  960. mov dh, 20
  961. mov dl, 3
  962. mov ah, 13h
  963. mov bp, offset string_menu
  964. int 10h
  965.  
  966. ret
  967. createMenuBox endp
  968.  
  969. ;*****************************************************************
  970. ; createFormulaBox -
  971. ; descricao: creates a box where the user will input operations
  972. ; input -
  973. ; output -
  974. ; destroi - ax, bx, cx, dx
  975. ;*****************************************************************
  976.  
  977. createFormulaBox proc ;Provavelmente vamos ter de alterar o
  978. ;tamanho da caixa da formula
  979.  
  980. mov ax, 80 ; formula lenght
  981. mov cx, 20
  982. mov dx, 124
  983. mov bx, 02h
  984. call hLine
  985.  
  986. mov ax, 17 ; formula height + 1
  987. mov cx, 20
  988. mov dx, 124
  989. mov bx, 01h
  990. call vLine
  991.  
  992. mov ax, 17 ; formula height + 1
  993. mov cx, 100
  994. mov dx, 124
  995. mov bx, 01h
  996. call vLine
  997.  
  998. ret
  999. createFormulaBox endp
  1000.  
  1001.  
  1002. ;*****************************************************************
  1003. ; createResultBox -
  1004. ; descricao: creates a box to output the result
  1005. ; input -
  1006. ; output -
  1007. ; destroi - ax, bx, cx, dx
  1008. ;*****************************************************************
  1009.  
  1010. createResultBox proc
  1011.  
  1012. mov ax, 80 ;cell lenght
  1013. mov cx, 140
  1014. mov dx, 124
  1015. mov bx, 02h
  1016. call hLine
  1017.  
  1018. mov ax, 17 ; formula height + 1
  1019. mov cx, 140
  1020. mov dx, 124
  1021. mov bx, 01h
  1022. call vLine
  1023.  
  1024. mov ax, 17 ; formula height + 1
  1025. mov cx, 220
  1026. mov dx, 124
  1027. mov bx, 01h
  1028. call vLine
  1029.  
  1030. ret
  1031. createResultBox endp
  1032.  
  1033. ;*****************************************************************
  1034. ; showLetter -
  1035. ; descricao: prints a letter for each matrix collumn,
  1036. ; also prints "Formula" and "Result"
  1037. ; input -
  1038. ; output -
  1039. ; destroi - ax, bx, cx, dx
  1040. ;*****************************************************************
  1041.  
  1042. showLetters proc
  1043. xor bh, bh
  1044. mov bl, 0Fh
  1045. mov cx, 1
  1046. mov dl, 04
  1047. mov al, 'A'
  1048.  
  1049. new_letter:
  1050. ;set cursor position
  1051. mov dh, 02h
  1052. mov ah, 02h
  1053. int 10h
  1054.  
  1055. ;write char at cursor position
  1056. mov ah, 09h
  1057. int 10h
  1058.  
  1059. inc al
  1060. add dl, 5
  1061. cmp al, 'E'
  1062. jnz new_letter
  1063.  
  1064. ;writes "Formula" above the big cell
  1065. mov al, 1
  1066. mov bl, 0Fh
  1067. mov cx, 7 ;string lenght
  1068. mov dh, 14
  1069. mov dl, 3
  1070. mov ah, 13h
  1071. mov bp, offset string_formula
  1072. int 10h
  1073.  
  1074. ;writes "Result" aove the small cell
  1075. mov dl, 18
  1076. mov cx, 6 ;string lenght deve ser 6
  1077. mov bp, offset string_result
  1078. int 10h
  1079.  
  1080. ret
  1081. showLetters endp
  1082.  
  1083. ;*****************************************************************
  1084. ; showNumbers -
  1085. ; descricao: prints a number for each matrix row
  1086. ; input -
  1087. ; output -
  1088. ; destroi - ax, bx, cx, dx
  1089. ;*****************************************************************
  1090.  
  1091. showNumbers proc
  1092. xor bh, bh
  1093. mov bl, 0Fh
  1094. mov cx, 1
  1095. mov dh, 04h
  1096. mov al, '1'
  1097.  
  1098. new_number:
  1099. ;set cursor position
  1100. mov dl, 01h
  1101. mov ah, 02h
  1102. int 10h
  1103.  
  1104. ;write char at cursor position
  1105. mov ah, 09h
  1106. int 10h
  1107.  
  1108. inc al
  1109. add dh, 2
  1110. cmp al, '5'
  1111. jnz new_number
  1112.  
  1113. ret
  1114. showNumbers endp
  1115.  
  1116. ;*****************************************************************
  1117. ; loadValues -
  1118. ; descricao: prints cells and formula from memory and calculates result and prints it
  1119. ; input - no input needed
  1120. ; output - none
  1121. ; destroi - ax, bx, cx, dx, di, bp
  1122. ;*****************************************************************
  1123.  
  1124. loadValues proc
  1125. mov bp, offset str_buffer_result
  1126. mov di, offset cell
  1127. mov dx, 0403h ;initial cell position
  1128.  
  1129. loadNextNum:
  1130. cmp [di], -128 ;if cell has value -128 it tests the next cell
  1131. je loadNextCell
  1132.  
  1133. mov al, [di]
  1134.  
  1135. xor ah, ah
  1136. cmp al, 00h ;if cell is negative we convert a
  1137. jge signedBitAx ;8bit negative into a 16bit negative
  1138. mov ah, 1111_1111b
  1139. signedBitAx:
  1140. push dx
  1141. push di
  1142. call decimalToASCII ;returns the decimal value in ascii value in a string
  1143. pop di ;pointed to by bp
  1144. pop dx
  1145. mov ax, 1301h ;prints string with value
  1146. mov bx, 0Fh
  1147. int 10h
  1148.  
  1149. loadNextCell: ;loads next value by incrementing the pointer to the
  1150. inc di ;next cell and updates the cell position
  1151. add dh, 02h
  1152.  
  1153. cmp dh, 0Ch
  1154. jne loadNextNum
  1155. add dl, 05h ;next column
  1156. mov dh, 04h
  1157. cmp dl, 17h
  1158. jne loadNextNum
  1159.  
  1160. ;formula start
  1161. mov di, offset formula_var
  1162. mov bx, bp
  1163. cmp [di+1], -1 ;checks if there's a formula loaded
  1164. je end_loading
  1165.  
  1166. xor cx, cx
  1167. load_next_form_value:
  1168. mov [bx], 'A' ;the 'A' is put as an initial value
  1169. inc cx ;and incremented acording to the column on memory
  1170. mov dl, [di]
  1171.  
  1172.  
  1173. cmp dl, 03h
  1174. ja notA
  1175. jmp load_next_form_value_0
  1176.  
  1177. notA:
  1178. inc [bx]
  1179. cmp dl, 07h
  1180. ja notB
  1181. sub dl, 04h
  1182. jmp load_next_form_value_0
  1183.  
  1184. notB:
  1185. inc [bx]
  1186. cmp dl, 0Bh
  1187. jb load_next_form_value_0
  1188. sub dl, 08h
  1189. inc [bx]
  1190. sub dl, 0Ch
  1191.  
  1192.  
  1193. load_next_form_value_0: ;does the same thing as before but for the line
  1194. inc bx
  1195. mov [bx], '1'
  1196. cmp dl, 01h
  1197. ja not2
  1198. jmp load_next_form_value_1
  1199.  
  1200. not2:
  1201. inc [bx]
  1202. cmp dl, 02h
  1203. jb load_next_form_value_1
  1204.  
  1205. not3:
  1206. inc [bx]
  1207. cmp dl, 03h
  1208. jb load_next_form_value_1
  1209. inc [bx]
  1210.  
  1211. load_next_form_value_1:
  1212. inc bx
  1213. cmp cx, 02h ;checks if both cells have been checked
  1214. je end_loading_form
  1215.  
  1216. mov al, [di+1] ;puts operand in string
  1217. mov [bx], al
  1218. inc bx
  1219. add di, 02h
  1220. jmp load_next_form_value
  1221.  
  1222. end_loading_form: ;prints formula
  1223. mov ax, 1301h
  1224. mov bx, 0Fh
  1225. mov cx, 05h
  1226. mov dx, 1003h
  1227. int 10h ;formula end
  1228.  
  1229. mov al, [di-2] ;prepares ah, bh, ch for calculate
  1230. xor ah, ah
  1231. mov bl, [di]
  1232. xor bh, bh
  1233. mov cl, [di-1]
  1234. xor ch, ch
  1235.  
  1236. call calculate
  1237.  
  1238. mov ax, dx
  1239. call writeNumber ;prints result
  1240.  
  1241. end_loading:
  1242. ret
  1243. loadValues endp
  1244.  
  1245.  
  1246. ;*****************************************************************
  1247. ; checkCell - Check and Evaluate Cell
  1248. ; descricao: checks and eveluates what to do with the cell clicked by the user
  1249. ; input - cx;dx = mouse coordenates
  1250. ; output -
  1251. ; destroi -
  1252. ;*****************************************************************
  1253.  
  1254. cANDeCell proc
  1255. ;check if user clicked on the margin
  1256.  
  1257. cmp cx, 20
  1258. jb done_checking
  1259. cmp dx, 28
  1260. jb done_checking
  1261.  
  1262. call checkMenuClicked
  1263. cmp bx, 255
  1264. je done_checking
  1265.  
  1266. ;check if user clicked on a matrix cell
  1267.  
  1268. cmp cx, 3
  1269. ja not_matrix
  1270. cmp dx, 3
  1271. ja not_matrix
  1272. call matrixCellClicked ;the cell clicked is described by its coordenates (cx and dx)
  1273. jmp done_checking
  1274.  
  1275. ;check if user clicked on the formula or result cell
  1276. not_matrix:
  1277. cmp dx, 6
  1278. jnz done_checking ;not formula
  1279. cmp cx, 1
  1280. ja done_checking ;not formula
  1281. call formulaCellClicked
  1282. jmp done_checking
  1283.  
  1284. done_checking:
  1285.  
  1286. ret
  1287. cANDeCell endp
  1288.  
  1289. ;*****************************************************************
  1290. ; checkMenuClicked -
  1291. ; descricao: check if user clicked on MENU
  1292. ; input -
  1293. ; output - bx=0(exit to menu)
  1294. ; destroi -
  1295. ;*****************************************************************
  1296.  
  1297. checkMenuClicked proc
  1298.  
  1299. ;check if user clicked on the margin
  1300.  
  1301. cmp cx, 20
  1302. jb donechecking_menu
  1303. cmp dx, 28
  1304. jb donechecking_menu
  1305.  
  1306. mov ax, cx
  1307. sub ax, 20
  1308.  
  1309. mov bx, 40
  1310. div bl
  1311. xor ah, ah ;sabemos agora a celula das letras
  1312. mov cx, ax ;o resultado esta em cx
  1313.  
  1314. mov ax, dx
  1315. sub ax, 28
  1316.  
  1317. mov bx, 16
  1318. div bl
  1319. xor ah, ah
  1320. mov dx, ax ;coordonates are now stored in cx(x) and dx(y)
  1321.  
  1322. cmp dx, 8 ;esta na linha do menu
  1323. jnz donechecking_menu
  1324. cmp cx, 0
  1325. jnz donechecking_menu
  1326. mov bx, 255
  1327. donechecking_menu:
  1328.  
  1329. ret
  1330. checkMenuClicked endp
  1331.  
  1332. ;*****************************************************************
  1333. ; matrixCellClicked -
  1334. ; descricao: performs operations to get and print the cell value
  1335. ; it also re calculates the result
  1336. ; input -
  1337. ; output -
  1338. ; destroi -
  1339. ;*****************************************************************
  1340.  
  1341. matrixCellClicked proc
  1342. push cx
  1343. push dx
  1344.  
  1345. ;update cursor position and clear cell clicked
  1346.  
  1347. call setCursorPOS
  1348.  
  1349. call clearCell
  1350.  
  1351. pop dx
  1352. pop cx
  1353. push cx
  1354. push dx
  1355.  
  1356. call setCursorPOS
  1357.  
  1358. push dx
  1359.  
  1360. call blinkingCursor
  1361.  
  1362. call getCellValue
  1363.  
  1364. pop dx
  1365.  
  1366. call evaluateInput ;user input value gets stored in bx
  1367.  
  1368. pop dx
  1369. pop cx
  1370. push cx
  1371. push dx
  1372.  
  1373. push bx
  1374. xor bx, bx
  1375. mov al, 4
  1376. mul cl
  1377.  
  1378. mov bl, al
  1379. add bx, dx
  1380. pop cx
  1381. mov cell[bx], cl ;number is now in cl
  1382.  
  1383. cmp cl, -128 ;check if it's a valid number
  1384. pop dx
  1385. pop cx
  1386. jnz mCC_done
  1387. call setCursorPOS
  1388. mov al, ' '
  1389. xor bh, bh
  1390. mov cx, 4
  1391. mov ah, 0Ah
  1392. int 10h
  1393.  
  1394. mCC_done: ;recalculates formula
  1395. mov di, offset formula_var
  1396. mov al, [di]
  1397. inc di
  1398. mov cl, [di]
  1399. inc di
  1400. mov bl, [di]
  1401.  
  1402. xor ah, ah
  1403. xor bh, bh
  1404. xor ch, ch
  1405. cmp cl, -1
  1406. je evaluate_done
  1407.  
  1408. call calculate
  1409.  
  1410. cmp ax, -1
  1411. je invalid_input3
  1412.  
  1413. mov ax, dx
  1414. call writeNumber
  1415. jmp evaluate_done
  1416.  
  1417. invalid_input3:
  1418. call invalidInput
  1419.  
  1420. evaluate_done:
  1421.  
  1422. ret
  1423. matrixCellClicked endp
  1424.  
  1425. ;*****************************************************************
  1426. ; getCellValue -
  1427. ; descricao: read a number from the user (from -127 to 127)
  1428. ; input -
  1429. ; output - cell[cell number] = user input
  1430. ; destroi - ax, bx, cx
  1431. ;*****************************************************************
  1432.  
  1433. getCellValue proc
  1434. xor cx, cx ;contador
  1435.  
  1436. ask_new_input:
  1437. xor ax, ax
  1438. int 16h
  1439. cmp al, 8 ;check if char is backspace
  1440. jnz not_backspace
  1441. cmp cx, 0
  1442. jz ask_new_input
  1443. call writeSpace
  1444. dec cx
  1445. jmp ask_new_input
  1446.  
  1447. not_backspace:
  1448. cmp al, 13
  1449. jnz not_enter
  1450. jmp input_done
  1451.  
  1452. not_enter:
  1453. cmp al, '-'
  1454. jnz not_minus
  1455. cmp cx, 0
  1456. jnz ask_new_input
  1457. jmp accept_input
  1458.  
  1459. not_minus:
  1460. cmp al, '0'
  1461. jb ask_new_input
  1462. cmp al, '9'
  1463. ja ask_new_input
  1464.  
  1465. accept_input:
  1466. mov bx, offset str_buffer
  1467. inc bx
  1468. inc bx
  1469. add bx, cx
  1470. mov [bx], al ;store char in "string" position
  1471. ;write char al = char in ascii
  1472. push ax
  1473. push bx
  1474. push cx
  1475.  
  1476. mov bh, 0
  1477. mov bx, 0Fh
  1478. mov cx, 1
  1479. mov ah, 09h
  1480. int 10h
  1481.  
  1482. pop cx
  1483. pop bx
  1484. pop ax
  1485. ;done
  1486.  
  1487. ;update cursor position dl,dh must already have previous mouse position
  1488.  
  1489. push ax
  1490. push bx
  1491.  
  1492. inc dl
  1493. xor bx, bx
  1494. mov ah, 02h
  1495. int 10h
  1496.  
  1497. pop bx
  1498. pop ax
  1499.  
  1500. ;done
  1501. sub bl, cl
  1502. mov al, [bx]
  1503. inc cx
  1504. cmp al, '-'
  1505. jnz positive_num
  1506. cmp cx, 4
  1507. jz input_done
  1508. jmp ask_new_input
  1509. positive_num:
  1510. cmp cx, 3
  1511. jnz ask_new_input
  1512. input_done:
  1513. mov bx, offset str_buffer
  1514. inc bx
  1515. mov [bx], cl
  1516.  
  1517. ret
  1518. getCellValue endp
  1519.  
  1520. ;*****************************************************************
  1521. ; writeSpace -
  1522. ; descricao: writes a space at cursor position to erase a char
  1523. ; input - dl;dx must have the cursor position
  1524. ; output -
  1525. ; destroi -
  1526. ;*****************************************************************
  1527.  
  1528. writeSpace proc
  1529. push ax
  1530. push bx
  1531. push cx
  1532.  
  1533. dec dl ;update cursor position
  1534. xor bx, bx
  1535. mov ah, 02h
  1536. int 10h
  1537.  
  1538. mov bh, 0 ;write space to "hide" the char
  1539. mov bx, 0Fh
  1540. mov cx, 1
  1541. mov ah, 09h
  1542. mov al, 32
  1543. int 10h
  1544.  
  1545. pop cx
  1546. pop bx
  1547. pop ax
  1548.  
  1549. ret
  1550. writeSpace endp
  1551.  
  1552. ;*****************************************************************
  1553. ; clearCell -
  1554. ; descricao: erases the cell clicked
  1555. ; input -
  1556. ; output -
  1557. ; destroi -
  1558. ;*****************************************************************
  1559.  
  1560. clearCell proc
  1561. push ax
  1562. push bx
  1563. push cx
  1564.  
  1565. mov al, ' '
  1566. xor bh, bh
  1567. mov cx, 4
  1568. mov ah, 0Ah
  1569. int 10h
  1570.  
  1571. pop cx
  1572. pop bx
  1573. pop ax
  1574.  
  1575. ret
  1576. clearCell endp
  1577.  
  1578. ;*****************************************************************
  1579. ; setCursorPOS -
  1580. ; descricao: updates cursor position
  1581. ; input -
  1582. ; output -
  1583. ; destroi -
  1584. ;*****************************************************************
  1585.  
  1586. setCursorPOS proc
  1587. push ax
  1588. push bx
  1589.  
  1590. mov al, 05h ;lenght of a cell (in characters)
  1591. mul cl ;multiply the lenght by its position
  1592. add al, 03h ;sum cell displacement
  1593. mov bl, al ;we now have the x value
  1594.  
  1595. mov al, 02h ;pretty much the same now but using the height
  1596. mul dl
  1597. add al, 04h ;we now have the y value
  1598.  
  1599. mov dl, bl ;from cx
  1600. mov dh, al ;from dx
  1601.  
  1602. xor bx, bx
  1603.  
  1604. mov ah, 02h
  1605. int 10h
  1606.  
  1607. pop bx
  1608. pop ax
  1609.  
  1610. ret
  1611. setCursorPOS endp
  1612.  
  1613. ;*****************************************************************
  1614. ; evaluateInput -
  1615. ; descricao: checks if input is valid
  1616. ; input -
  1617. ; output - BX = number inouted by the user, if invalid bx = -128
  1618. ; destroi -
  1619. ;*****************************************************************
  1620.  
  1621. evaluateInput proc
  1622. xor bx, bx
  1623.  
  1624. mov si, 0
  1625. mov ch, str_buffer[si + 1]
  1626. cmp ch, 0
  1627. jz invalid_input
  1628. mov ch, str_buffer[si + 2] ;in case of ch = '-'
  1629. cmp ch, '-'
  1630. jz negative_num
  1631. jmp input_checked
  1632.  
  1633. negative_num:
  1634. mov ax, 1
  1635.  
  1636. input_checked:
  1637. push ax
  1638. call asciiToDecimal ;number is now in bx
  1639. pop ax
  1640. cmp di, 1
  1641. jz invalid_input
  1642. cmp ax, 1 ;if number is negative (ax = 1) then bx > 127
  1643. jz skip_test
  1644. cmp bx, 127
  1645. ja invalid_input
  1646. jmp valid_input
  1647. skip_test:
  1648. cmp bl, 128 ;else bx < 128
  1649. jb invalid_input
  1650. jmp valid_input
  1651.  
  1652. invalid_input:
  1653. xor bx, bx
  1654. mov bl, -128
  1655.  
  1656. valid_input:
  1657.  
  1658. ret
  1659. evaluateInput endp
  1660.  
  1661. ;*****************************************************************
  1662. ; formulaCellClicked -
  1663. ; descricao:
  1664. ; input - performs operations to get and print a formula
  1665. ; output -
  1666. ; destroi -
  1667. ;*****************************************************************
  1668.  
  1669. formulaCellClicked proc
  1670. ;meter o cursor no sitio certo
  1671. cmp cx, 1
  1672. jnz already_at_start
  1673. dec cx
  1674. already_at_start:
  1675. push cx
  1676. push dx
  1677.  
  1678. call setCursorPOS
  1679.  
  1680. call clearFormula
  1681.  
  1682. pop dx
  1683. pop cx
  1684. push cx
  1685. push dx
  1686.  
  1687. call setCursorPOS
  1688.  
  1689. mov ch, 06h
  1690. mov cl, 0000_1111b
  1691. mov ah, 01h
  1692. int 10h
  1693.  
  1694. call getFormula
  1695.  
  1696. cmp ax, 1 ;check if getFormula returned an error
  1697. jnz evaluateFormula
  1698. pop dx
  1699. pop cx
  1700. call setCursorPOS
  1701. call clearFormula
  1702. mov di, offset formula_var
  1703. mov [di+1], -1
  1704.  
  1705. jmp f_done
  1706. evaluateFormula:
  1707.  
  1708. call evaluateFormulaInput ;ficamos com o valor em decimal no registo ax
  1709.  
  1710. pop dx
  1711. pop cx
  1712. push bx
  1713.  
  1714. call setCursorPOS
  1715.  
  1716. pop bx
  1717. cmp bx, -1
  1718. jz invalid_formula_input
  1719.  
  1720. jmp f_done
  1721.  
  1722. invalid_formula_input:
  1723. xor bx, bx ;para evitar parecer que estamos a clicar no menu
  1724. f_done:
  1725.  
  1726. ret
  1727. formulaCellClicked endp
  1728.  
  1729. ;*****************************************************************
  1730. ; getFormula -
  1731. ; descricao: accepts a formula inputed by the user
  1732. ; input -
  1733. ; output -
  1734. ; destroi -
  1735. ;*****************************************************************
  1736.  
  1737. getFormula proc
  1738. xor cx, cx ;contador
  1739.  
  1740. ask_new_finput:
  1741. xor ax, ax
  1742. int 16h
  1743.  
  1744. cmp al, 8 ;check if char is backspace
  1745. jnz not_fbackspace
  1746. cmp cx, 0
  1747. jz ask_new_finput
  1748. call writeSpace
  1749. dec cx
  1750. jmp ask_new_finput
  1751.  
  1752. not_fbackspace:
  1753. cmp al, 13
  1754. jnz not_fenter
  1755. jmp finput_done
  1756.  
  1757. not_fenter:
  1758. cmp cx, 5
  1759. jz ask_new_finput
  1760. ;check if it's time to get a letter
  1761. cmp cx, 0
  1762. jz f_letter
  1763. cmp cx, 3
  1764. jz f_letter
  1765. cmp cx, 1
  1766. jz f_number
  1767. cmp cx, 4
  1768. jz f_number
  1769. ;now we obtain the operator
  1770. cmp al, '+'
  1771. jz accept_finput
  1772. cmp al, '-'
  1773. jz accept_finput
  1774. cmp al, '*'
  1775. jz accept_finput
  1776. cmp al, '/'
  1777. jz accept_finput
  1778. jmp ask_new_finput
  1779.  
  1780. f_letter:
  1781. cmp al, 'd'
  1782. ja ask_new_finput
  1783. cmp al, 'a'
  1784. jb newt
  1785. sub al, 32 ;tranformar a letra em maiscula
  1786. jmp accept_finput
  1787. newt:
  1788. cmp al, 'D'
  1789. ja ask_new_finput
  1790. cmp al, 'A'
  1791. jb ask_new_finput
  1792. jmp accept_finput
  1793.  
  1794. f_number:
  1795. cmp al, '4'
  1796. ja ask_new_finput
  1797. cmp al, '1'
  1798. jb ask_new_finput
  1799.  
  1800. accept_finput:
  1801. mov bx, offset str_buffer2
  1802. inc bx
  1803. inc bx
  1804. add bx, cx
  1805. mov [bx], al ;store char in "string" position
  1806. ;write char al = char in ascii
  1807. push ax
  1808. push bx
  1809. push cx
  1810.  
  1811. mov bh, 0
  1812. mov bx, 0Fh
  1813. mov cx, 1
  1814. mov ah, 09h
  1815. int 10h
  1816.  
  1817. pop cx
  1818. pop bx
  1819. pop ax
  1820. ;done
  1821.  
  1822. ;update cursor position dl,dh must already have previous mouse position
  1823.  
  1824. push ax
  1825. push bx
  1826.  
  1827. inc dl
  1828. xor bx, bx
  1829. mov ah, 02h
  1830. int 10h
  1831.  
  1832. pop bx
  1833. pop ax
  1834.  
  1835. ;done
  1836. sub bl, cl
  1837. mov al, [bx]
  1838. inc cx
  1839. jmp ask_new_finput
  1840. finput_done:
  1841. mov bx, offset str_buffer2
  1842. inc bx
  1843. mov [bx], cl
  1844.  
  1845. ret
  1846. getFormula endp
  1847.  
  1848. ;*****************************************************************
  1849. ; evaluateInput -
  1850. ; descricao: checks if input is valid, updates result
  1851. ; input -
  1852. ; output - BX = -1 in case of error
  1853. ; destroi -
  1854. ;*****************************************************************
  1855.  
  1856. evaluateFormulaInput proc
  1857. xor bx, bx
  1858. mov bl, str_buffer2[1] ;Antes estava 4
  1859. cmp bl, 5 ;Verificar se o utilizador nao meteu 5 numeros
  1860. jnz invalid_input2 ;Atencao porque se o utilizador meter mais do que 4 numero
  1861.  
  1862. mov si, offset str_buffer2
  1863. add si, 2
  1864.  
  1865. call getCell
  1866. mov ax, bx
  1867. add si, 2
  1868.  
  1869. call getOperand
  1870.  
  1871. inc si
  1872. call getCell
  1873.  
  1874. mov di, offset formula_var
  1875. mov [di], al
  1876. inc di
  1877. mov [di], cl
  1878. inc di
  1879. mov [di], bl
  1880.  
  1881. call calculate
  1882. cmp ax, -1
  1883. jz invalid_input2
  1884.  
  1885. mov ax, dx
  1886.  
  1887. call writeNumber
  1888.  
  1889. xor bx, bx
  1890. jmp fdone
  1891.  
  1892. invalid_input2:
  1893. call invalidInput
  1894.  
  1895. mov bx, -1
  1896. fdone:
  1897.  
  1898. ret
  1899. evaluateFormulaInput endp
  1900.  
  1901. ;*****************************************************************
  1902. ; invalidInput -
  1903. ; descricao: writes error on result box
  1904. ; input - no input needed
  1905. ; output - no output
  1906. ; destroi - ax, bx, cx, dx, bp
  1907. ;*****************************************************************
  1908.  
  1909. invalidInput proc
  1910. mov dx, 06h
  1911. mov cx, 03h
  1912. call setCursorPos
  1913. call clearFormula
  1914. mov ax, 1301h ;print "ERROR" on result box
  1915. mov bx, 000Fh
  1916. mov cx, 05h
  1917. mov bp, offset string_error
  1918. int 10h
  1919.  
  1920. ret
  1921. invalidInput endp
  1922.  
  1923. ;*****************************************************************
  1924. ; clearFormula -
  1925. ; descricao: clears result box
  1926. ; input - no input needed
  1927. ; output - no output
  1928. ; destroi -
  1929. ;*****************************************************************
  1930.  
  1931. clearFormula proc
  1932. push cx
  1933. push di
  1934. pop di
  1935. mov al, ' '
  1936. xor bh, bh
  1937. mov cx, 8
  1938. mov ah, 0Ah
  1939. int 10h
  1940. pop cx
  1941.  
  1942. ret
  1943. clearFormula endp
  1944.  
  1945. ;*****************************************************************
  1946. ; getcell -
  1947. ; descricao: find what's the cell position in the array
  1948. ; input - SI = offset to cell (the memory array)
  1949. ; output - BX = celula
  1950. ; destroi -
  1951. ;*****************************************************************
  1952.  
  1953. getCell proc
  1954. push ax
  1955. xor ax, ax
  1956. mov al, [si]
  1957. sub al, 'A'
  1958. mov bx, 4
  1959. mul bl
  1960. add al, [si+1]
  1961. sub al, '1'
  1962. xor ah, ah
  1963. mov bx, ax
  1964. pop ax
  1965.  
  1966. ret
  1967. getCell endp
  1968.  
  1969.  
  1970. ;*****************************************************************
  1971. ; getcell -
  1972. ; descricao: checks if input is valid, startes checking from the last number
  1973. ; input - SI = offset to the operator (in cell array)
  1974. ; output - CX = operand
  1975. ; destroi -
  1976. ;*****************************************************************
  1977.  
  1978. getOperand proc
  1979. xor cx, cx
  1980. mov cl, [si]
  1981.  
  1982. ret
  1983. getOperand endp
  1984.  
  1985. ;*****************************************************************
  1986. ; calculate -
  1987. ; descricao: checks if input is valid, startes checking from the last number
  1988. ; input - AX = cell1 , BX = cell2 , CX = operator
  1989. ; output - DX = result
  1990. ; destroi -
  1991. ;*****************************************************************
  1992.  
  1993. calculate proc
  1994. mov di, ax
  1995. mov al, cell[di] ;I'm sorry :c
  1996. cmp al, -128
  1997. jz invalid_calc
  1998.  
  1999. mov di, bx
  2000. mov bl, cell[di]
  2001. cmp bl, -128
  2002. jz invalid_calc
  2003.  
  2004. mov dx, -128
  2005. mov remainder, dx
  2006.  
  2007. xor dx, dx
  2008. xor ah, ah ;o numero esta em ax
  2009. xor bh, bh ;o numero esta em bx
  2010.  
  2011. or al, al
  2012. jns al_not_neg
  2013. neg al
  2014. neg ax ;transforming a 8-bit negative number into a 16-bit negative number
  2015. al_not_neg:
  2016. or bl, bl
  2017. jns bl_not_neg
  2018. neg bl
  2019. neg bx
  2020. bl_not_neg:
  2021.  
  2022. ;find operator
  2023. cmp cx, '+'
  2024. jz sum
  2025. cmp cx, '-'
  2026. jz subtract
  2027. cmp cx, '*'
  2028. jz multiply
  2029. cmp cx, '/'
  2030. jz divide
  2031. jmp invalid_calc
  2032.  
  2033. ;dx = 0 here
  2034. sum:
  2035. add ax, bx
  2036. mov dx, ax
  2037. xor ax, ax
  2038. ;js num_neg
  2039. ;jo num_over
  2040. jmp calc_done
  2041.  
  2042. subtract:
  2043. sub ax, bx
  2044. mov dx, ax
  2045. xor ax, ax
  2046. ;js num_neg
  2047. ;jo num_over
  2048. jmp calc_done
  2049.  
  2050. multiply:
  2051. or ax, ax
  2052. jns num_not_neg
  2053. not dx
  2054. num_not_neg:
  2055. imul bx
  2056. cmp dx, 0
  2057. jz cntinue
  2058. cmp dx, 65535
  2059. jnz invalid_calc
  2060. cntinue:
  2061. mov dx, ax
  2062. xor ax, ax
  2063. jmp calc_done
  2064.  
  2065. divide:
  2066. cmp bl, 0
  2067. jz invalid_calc
  2068. or ax, ax
  2069. jns num_not_neg2
  2070. not dx
  2071. num_not_neg2:
  2072. idiv bx
  2073. mov remainder, dx
  2074. mov dx, ax
  2075. xor ax, ax
  2076. jmp calc_done
  2077.  
  2078. invalid_calc:
  2079. mov ax, -1
  2080. jmp calc_done
  2081.  
  2082. calc_done:
  2083. ret
  2084. calculate endp
  2085.  
  2086. ;*****************************************************************
  2087. ; asciiToDecimal -
  2088. ; descricao:
  2089. ; input - ax = 1 if number is negative, ax = 0 if number is positive
  2090. ; output - bx = numero para guardar
  2091. ; destroi -
  2092. ;*****************************************************************
  2093.  
  2094. asciiToDecimal proc
  2095. push ax
  2096. xor di, di
  2097. xor ax, ax
  2098. xor bx, bx
  2099. xor ch, ch ;estes 3 xor provavelmente na sao necessarios,
  2100. mov cl, str_buffer[1] ;mas se eu retirar podem dar erros
  2101. mov si, cx
  2102. mov al, 1
  2103. mov dl, 10
  2104. next_number:
  2105. dec si
  2106. cmp si, 0
  2107. jz last_char
  2108. push ax ;isto deve dar erro porque eu acho que so queria al
  2109. mov dh, str_buffer[si + 2]
  2110. sub dh, '0'
  2111. mul dh
  2112. jo error_AtD
  2113. add bx, ax ;o numero esta no bx
  2114.  
  2115. pop ax
  2116. mul dl
  2117.  
  2118. jmp next_number
  2119. last_char:
  2120. pop cx
  2121. cmp cx, 1 ;verificar se o numero e negativo ou nao
  2122. jnz pos_num
  2123. neg bl
  2124. jmp ascii_dec_done:
  2125.  
  2126. error_AtD:
  2127. pop ax
  2128. pop ax
  2129. mov di, 1
  2130. jmp ascii_dec_done
  2131. pos_num:
  2132. mov dh, str_buffer[2]
  2133. sub dh, '0'
  2134. mul dh
  2135. add bx, ax
  2136.  
  2137. ascii_dec_done:
  2138.  
  2139. ret
  2140. asciiToDecimal endp
  2141.  
  2142.  
  2143. ;*****************************************************************
  2144. ; writeNumber -
  2145. ; descricao: prints number given in ax in the result box
  2146. ; input - ax=number
  2147. ; output - no output
  2148. ; destroi - ad, bx, cx, dx, bp, di
  2149. ;*****************************************************************
  2150.  
  2151. writeNumber proc
  2152. push ax
  2153. mov dx, 06h ;clean-up result
  2154. mov cx, 03h
  2155. call setCursorPOS
  2156. call clearFormula
  2157. pop ax
  2158.  
  2159. mov bp, offset str_buffer_result
  2160. call decimalToASCII ;put result in string
  2161. mov dx, 1012h ;location of where to print the quocient
  2162. jmp quocient
  2163.  
  2164. remainder_print: ;print ";R:" to seperate quocient
  2165. push bp ;and remainder
  2166. mov bp, offset string_remainder
  2167. mov ax, 1301h
  2168. mov bx, 0Fh
  2169. add dx, cx
  2170. mov cx, 03h
  2171. int 10h
  2172. add dx, 03h
  2173.  
  2174. pop bp ;put remainder in string
  2175. push dx
  2176. mov ax, remainder
  2177. call decimalToASCII
  2178. mov remainder, -128 ;remainder is reset
  2179.  
  2180. pop dx ;location of where to print the remainder
  2181. quocient: ;print the quocient and (in case it exists) the remainder
  2182. mov ax, 1301h
  2183. mov bx, 000Fh
  2184. xor ch, ch
  2185. int 10h
  2186.  
  2187. cmp remainder, -128
  2188. jne remainder_print
  2189.  
  2190. ret
  2191. writeNumber endp
  2192.  
  2193. ;*****************************************************************
  2194. ; decimalToASCII -
  2195. ; descricao: puts in string given in bp the ascii value of a number
  2196. ; input - ax=number; bp= string
  2197. ; output - bp=string; cl=number of characters in string
  2198. ; destroi -
  2199. ;*****************************************************************
  2200.  
  2201. decimalToASCII proc
  2202. mov di, bp
  2203. xor bl, bl
  2204. cmp ax, 00h
  2205. jge notNegNumber
  2206. mov [di], '-'
  2207. inc di
  2208. neg ax
  2209. inc bl
  2210. push 00h
  2211.  
  2212. notNegNumber: ;divides number until result=0
  2213. xor dx, dx ;stores result in stack
  2214. mov cx, 0Ah
  2215. div cx
  2216. inc bl
  2217. push dx
  2218.  
  2219. cmp al, 0
  2220. jne notNegNumber
  2221.  
  2222. mov cl, bl
  2223.  
  2224. copyRemainders: ;takes the remainders from the stack
  2225. pop ax ;and puts them on the string pointed
  2226. add al, '0' ;by bp
  2227. mov [di], al
  2228. inc di
  2229. dec bl
  2230.  
  2231. cmp bl, 00h
  2232. jne copyRemainders
  2233.  
  2234. mov [di], 00h
  2235.  
  2236. ret
  2237. decimalToASCII endp
  2238.  
  2239. ;*****************************************************************
  2240. ; importExportFilename -
  2241. ; descricao: reads file name
  2242. ; input - no input needed
  2243. ; output - dx = offset nome do ficheiro
  2244. ; destroi - ax, bx, cx
  2245. ;*****************************************************************
  2246.  
  2247. importExportFilename proc
  2248. mov dx, 06h ;clears possible error and previous filename
  2249. mov cx, 00h
  2250. call setCursorPos
  2251. mov ah, 09h
  2252. mov al, ' '
  2253. mov bx, 000Fh
  2254. mov cx, 0021h
  2255. int 10h
  2256. mov dx, 07h
  2257. mov cx, 00h
  2258. call setCursorPos
  2259. mov cx, 32h
  2260. int 10h
  2261.  
  2262. mov bp, offset string_filename ;prints "Filename:"
  2263. mov ax, 1301h
  2264. mov bx, 0Fh
  2265. mov cx, 08h
  2266. mov dx, 1003h
  2267. int 10h
  2268.  
  2269. mov dx, 07h ;puts cursor and writes "c:\"
  2270. mov cx, 00h
  2271. call setCursorPos
  2272. mov ah, 09h
  2273. mov al, 'c'
  2274. mov bx, 000Fh
  2275. mov cx, 01h
  2276. int 10h
  2277.  
  2278. inc dx ;updates
  2279. mov ah, 02h ;cursor
  2280. int 10h ;position after writing "c"
  2281. mov ah, 09h
  2282. mov al, ':'
  2283. int 10h
  2284.  
  2285. inc dx
  2286. mov ah, 02h
  2287. int 10h
  2288. mov ah, 09h
  2289. mov al, '\'
  2290. int 10h
  2291.  
  2292. inc dx ;updates cursor position
  2293. mov ah, 02h ;after writing "c:\"
  2294. int 10h
  2295.  
  2296. call blinkingCursor
  2297. xor cx, cx
  2298. read_again: ;prints ' ' on previous character
  2299. xor bh, bh
  2300. sub dx, cx
  2301. mov ah, 02h
  2302. int 10h
  2303. mov ah, 09h
  2304. mov al, ' '
  2305. mov bx, 000Fh
  2306. int 10h
  2307.  
  2308. mov bx, offset name_file
  2309. add bx, 03h
  2310. xor cx, cx
  2311. keep_reading:
  2312. push bx
  2313. xor bh, bh
  2314. mov ah, 02h
  2315. int 10h
  2316. pop bx ;reads a character from the user
  2317. mov ah, 00h
  2318. int 16h
  2319. mov [bx], al
  2320. inc bx
  2321. inc cx
  2322.  
  2323. push bx
  2324. push cx
  2325. mov ah, 09h ;writes character read
  2326. mov bx, 0Fh
  2327. mov cx, 01h
  2328. int 10h
  2329. pop cx
  2330. pop bx
  2331.  
  2332. inc dx
  2333. cmp al, 0Dh
  2334. je end_reading
  2335. cmp al, 00h
  2336. je clear_last_char
  2337. cmp al, 08h
  2338. jne keep_reading
  2339. clear_last_char:
  2340. dec cx
  2341. dec dx
  2342. dec bx
  2343. cmp cx, 00h
  2344. je keep_reading
  2345. dec bx
  2346. dec cx ;compensate increment in writeSpace
  2347. call writeSpace
  2348. jmp keep_reading
  2349.  
  2350. end_reading: ;tests extension
  2351. sub bx, 05h
  2352. dec dx
  2353. dec cx
  2354. cmp [bx], '.'
  2355. jne read_again
  2356. inc bx
  2357. cmp [bx], 't'
  2358. jne read_again
  2359. inc bx
  2360. cmp [bx], 'x'
  2361. jne read_again
  2362. inc bx
  2363. cmp [bx], 't'
  2364. jne read_again
  2365. mov [bx+1], 00h
  2366. mov dx, offset name_file
  2367.  
  2368. ret
  2369. importExportFilename endp
  2370.  
  2371. ;*****************************************************************
  2372. ; blinkingCursor -
  2373. ; descricao: cursor starts blinking
  2374. ; input -
  2375. ; output -
  2376. ; destroi -
  2377. ;*****************************************************************
  2378.  
  2379. blinkingCursor proc
  2380. mov ch, 06h
  2381. mov cl, 0000_1111b
  2382. mov ah, 01h
  2383. int 10h
  2384.  
  2385. ret
  2386. blinkingCursor endp
  2387.  
  2388. ;*****************************************************************
  2389. ; keepCheckingMouse -
  2390. ; descricao:
  2391. ; input -
  2392. ; output -
  2393. ; destroi -
  2394. ;*****************************************************************
  2395.  
  2396. keepCheckingMouse proc
  2397. keep_checking:
  2398. mov ax, 03h
  2399. int 33h
  2400. shr cx, 1 ; x/2 - in this mode the value of CX is doubled.
  2401. cmp bx, 1
  2402. jnz keep_checking
  2403.  
  2404. ret
  2405. keepCheckingMouse endp
  2406.  
  2407. ;*****************************************************************
  2408. ; reiniciateVideo -
  2409. ; descricao: mostra a spreadsheet em memoria
  2410. ; input -
  2411. ; output -
  2412. ; destroi -
  2413. ;*****************************************************************
  2414.  
  2415. reiniciateVideo proc
  2416. mov ah, 00h
  2417. mov al, 13h
  2418. int 10h
  2419.  
  2420. ret
  2421. reiniciateVideo endp
  2422.  
  2423. ;*****************************************************************
  2424. ; fopen -
  2425. ; descricao: abre um ficheiro em modo read(al==0), write(al==1) ou append(al==2)
  2426. ; input - dx=offset nome ficheiro
  2427. ; output - ax=file_handle
  2428. ; destroi -
  2429. ;*****************************************************************
  2430.  
  2431. fopen proc
  2432. mov ah, 3Dh
  2433. mov al, 02h
  2434. mov cx, 00h
  2435. int 21h
  2436.  
  2437. ret
  2438. fopen endp
  2439.  
  2440. ;*****************************************************************
  2441. ; fclose -
  2442. ; descricao:
  2443. ; input - bx= file handle
  2444. ; output -
  2445. ; destroi -
  2446. ;*****************************************************************
  2447.  
  2448. fclose proc
  2449. mov ah, 3Eh
  2450. int 21h
  2451.  
  2452. ret
  2453. fclose endp
  2454.  
  2455. ;*****************************************************************
  2456. ; fcreate -
  2457. ; descricao: cria um ficheiro
  2458. ; input - dx=offset nome ficheiro
  2459. ; output - ax=file_handle
  2460. ; destroi -
  2461. ;*****************************************************************
  2462.  
  2463. fcreate proc
  2464. mov ah, 3Ch
  2465. mov cx, 00h
  2466. int 21h
  2467.  
  2468. ret
  2469. fcreate endp
  2470.  
  2471. ;*****************************************************************
  2472. ; save_contents -
  2473. ; descricao: saves contents.bin or creates it
  2474. ; input -
  2475. ; output -
  2476. ; destroi -
  2477. ;*****************************************************************
  2478.  
  2479. save_contents proc
  2480. ;tries to open file
  2481. mov dx, offset name_contents
  2482. call fcreate
  2483.  
  2484. mov bx, ax ;move handle
  2485.  
  2486. mov cx, 19 ;number of bytes
  2487. mov dx, offset cell ;offset to write to
  2488. mov ah, 40h
  2489. int 21h
  2490.  
  2491. ;closes file
  2492. call fclose
  2493.  
  2494. ret
  2495. save_contents endp
  2496.  
  2497. ;*****************************************************************
  2498. ; open_contents -
  2499. ; descricao: opens contents.bin if it exists
  2500. ; input -
  2501. ; output -
  2502. ; destroi - tudo
  2503. ;*****************************************************************
  2504.  
  2505. open_contents proc
  2506. ;tries to open file
  2507. mov dx, offset name_contents
  2508. call fopen
  2509. jc end_open_cont ;in case it doesn't exist yet
  2510.  
  2511. mov bx, ax
  2512.  
  2513. mov cx, 19 ;number of bytes
  2514. mov dx, offset cell ;offset to write to
  2515. mov ah, 3Fh
  2516. int 21h
  2517.  
  2518. call fclose
  2519. end_open_cont:
  2520. ret
  2521. open_contents endp
  2522.  
  2523. ends
  2524.  
  2525. end start ; set entry point and stop the assembler.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement