Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; multi-segment executable file template.
  2.  
  3. data segment
  4.     name_contents db "c:\contents.bin", 0  
  5.    
  6.     string_menu_1 db "- Import Spreadsheet";length=20
  7.     string_menu_2 db "- Show spreadsheet"  ;length=18
  8.     string_menu_3 db "- Edit spreadsheet"  ;length=18
  9.     string_menu_4 db "- Export spreadsheet";length=20
  10.     string_menu_5 db "- About"             ;length=7
  11.     string_menu_6 db "- Exit"              ;length=6
  12.                              
  13.     string_formula db "Formula",
  14.     string_result db "Result",
  15.     string_menu db "Menu",
  16.     num_hcell dw 04h
  17.     matrix_lenght dw 160 ;estava 240 antes
  18.     matrix_height dw 65
  19.     cell db 16 dup(-128)
  20.     ;formula_lenght 88
  21. ends
  22.  
  23. stack segment
  24.     dw   128  dup(0)
  25. ends
  26.  
  27. code segment
  28. start:
  29. ; set segment registers:
  30.     mov ax, data
  31.     mov ds, ax
  32.     mov es, ax
  33.    
  34.     ;open contents.bin
  35.         call open_contents
  36.        
  37.     draw_menu:
  38.     ;iniciate video mode
  39.         mov ah, 00h
  40.         mov al, 13h
  41.         int 10h
  42.        
  43.         call menu
  44.          
  45.         keep_checking_menu:
  46.             mov ax, 03h
  47.             int 33h
  48.             shr cx, 1  ; x/2 - in this mode the value of CX is doubled.
  49.             cmp bx, 1
  50.             jnz keep_checking_menu
  51.        
  52.         call checkMenu        
  53.        
  54.         cmp dh, 01h
  55.         jne draw_menu    
  56.  
  57.        
  58.     ;saves contents.bin
  59.         call save_contents  
  60.    
  61.     mov ax, 4c00h ; exit to operating system.
  62.     int 21h
  63.  
  64. ;*****************************************************************
  65. ; menu -
  66. ; descricao: draws menu
  67. ; input -
  68. ; output -
  69. ; destroi -
  70. ;*****************************************************************    
  71.    
  72.     menu proc      
  73.         xor dh, dh
  74.         mov al, 01h
  75.         mov bh, 00h
  76.         mov bl, 0Fh
  77.         mov dl, 02h
  78.        
  79.         mov cx, 14h;length of string_1
  80.         mov bp, offset string_menu_1
  81.        
  82.         imprime_string:
  83.             inc dh
  84.             inc dh ;next line
  85.             push bx
  86.            
  87.             xor bh, bh      
  88.             mov ah, 13h
  89.             int 10h
  90.            
  91.             pop bx    
  92.             inc bh ;next string
  93.                        
  94.         cmp bh, 02h
  95.         je menu_2
  96.            
  97.         cmp bh, 03h
  98.         je menu_3
  99.            
  100.         cmp bh, 04h
  101.         je menu_4
  102.            
  103.         cmp bh, 05h
  104.         je menu_5
  105.         ja end_of_menu
  106.        
  107.         mov cx, 12h
  108.         mov bp, offset string_menu_2
  109.         jmp imprime_string    
  110.        
  111.         menu_2:
  112.         mov bp, offset string_menu_3
  113.         jmp imprime_string
  114.        
  115.         menu_3:
  116.         mov cx, 14h
  117.         mov bp, offset string_menu_4
  118.         jmp imprime_string
  119.        
  120.         menu_4:
  121.         mov cx, 07h
  122.         mov bp, offset string_menu_5      
  123.         jmp imprime_string
  124.        
  125.         menu_5:
  126.         mov cx, 06h
  127.         mov bp, offset string_menu_6      
  128.         jmp imprime_string
  129.        
  130.         end_of_menu:
  131.         ret
  132.     menu endp
  133.  
  134. ;*****************************************************************
  135. ; checkMenu -
  136. ; descricao: checks position clicked on menu window
  137. ; input -
  138. ; output -
  139. ; destroi -
  140. ;*****************************************************************    
  141.        
  142.         checkMenu proc
  143.             xor dh, dh
  144.            
  145.             cmp cx, 13h          ;left limit  x
  146.             jb done_checking_menu
  147.             cmp cx, 178          ;right limit x
  148.             ja done_checking_menu
  149.             cmp dx, 12h          ;superior limit y
  150.             jb done_checking_menu
  151.             cmp dx, 6Ch          ;inferior limit y
  152.             ja done_checking_menu
  153.            
  154.             cmp dx, 1Bh          ;test import
  155.             ja test_show
  156.             call import_spreadsheet
  157.             jmp done_checking_menu
  158.            
  159.             test_show:
  160.             cmp dx, 2Bh
  161.             ja test_edit
  162.             call show_spreadsheet
  163.             jmp done_checking_menu
  164.            
  165.             test_edit:
  166.             cmp dx, 3Bh
  167.             ja test_export
  168.             call edit_spreadsheet
  169.             jmp done_checking_menu
  170.            
  171.             test_export:          
  172.             cmp dx, 4Bh
  173.             ja test_about
  174.             call export_spreadsheet
  175.             jmp done_checking_menu
  176.            
  177.             test_about:
  178.             cmp dx, 5Bh
  179.             ja definitely_exit
  180.             call show_about
  181.             jmp done_checking_menu
  182.            
  183.             definitely_exit:
  184.             mov dh, 01h
  185.            
  186.             done_checking_menu:
  187.             ret
  188.         checkMenu endp
  189.  
  190. ;*****************************************************************
  191. ; import_spreadsheet -
  192. ; descricao: importa um ficheiro .txt
  193. ; input -
  194. ; output -
  195. ; destroi -
  196. ;*****************************************************************  
  197.      
  198.         import_spreadsheet proc
  199.             ret
  200.         import_spreadsheet endp
  201.  
  202. ;*****************************************************************
  203. ; show_spreadsheet -
  204. ; descricao: mostra a spreadsheet em memoria
  205. ; input -
  206. ; output -
  207. ; destroi -
  208. ;*****************************************************************        
  209.        
  210.         show_spreadsheet proc
  211.             ;reiniciate video mode to clear window
  212.             mov ah, 00h
  213.             mov al, 13h
  214.             int 10h
  215.            
  216.             ret
  217.         show_spreadsheet endp
  218.  
  219. ;*****************************************************************
  220. ; edit_spreadsheet -
  221. ; descricao: mostre e permite editar a spreadsheet em memoria
  222. ; input -
  223. ; output -
  224. ; destroi -
  225. ;*****************************************************************        
  226.        
  227.         edit_spreadsheet proc
  228.             ;reiniciate video mode to clear window
  229.             mov ah, 00h
  230.             mov al, 13h
  231.             int 10h
  232.                  
  233.             ;draw spreadsheet    
  234.             call createMatrix
  235.            
  236.             call createMenuBox
  237.            
  238.             call createFormulaBox
  239.            
  240.             call createResultBox
  241.            
  242.             call showLetters
  243.            
  244.             call showNumbers
  245.            
  246.             mov ax, 00h
  247.             int 33h
  248.            
  249.             ;wait for user input                              
  250.             keep_checking:
  251.                 mov ax, 03h
  252.                 int 33h
  253.                 shr cx, 1       ; x/2 - in this mode the value of CX is doubled.
  254.                 cmp bx, 1
  255.                 jnz keep_checking
  256.            
  257.             call cANDeCell  ;check and evaluate cell
  258.            
  259.             ;Preisamos agora de uma funcao para avaliar a celula que foi clicada
  260.             ;ja temos toda a informacao necessaria
  261.             ;devo meter AL para a dizer se a celula e valida
  262.             ;e AH para dizer o tipo de celula
  263.            
  264.             cmp bl, 0     ;exit to MENU
  265.             jne keep_checking
  266.              
  267.             ret
  268.         edit_spreadsheet endp
  269.        
  270. ;*****************************************************************
  271. ; export_spreadsheet -
  272. ; descricao: exporta para um ficheiro .txt a funcao em memoria
  273. ; input -
  274. ; output -
  275. ; destroi -
  276. ;*****************************************************************        
  277.        
  278.         export_spreadsheet proc
  279.             ret
  280.         export_spreadsheet endp
  281.        
  282. ;*****************************************************************
  283. ; show_about -
  284. ; descricao: mostra certas informacoes sobre os criadores
  285. ; input -
  286. ; output -
  287. ; destroi -
  288. ;*****************************************************************        
  289.        
  290.         show_about proc
  291.             ;reiniciate video mode to clear window
  292.             mov ah, 00h
  293.             mov al, 13h
  294.             int 10h
  295.            
  296.             ret
  297.         show_about endp
  298.            
  299. ;*****************************************************************
  300. ; hLine -
  301. ; descricao: creates horizontal lines for the matrix
  302. ; input - bx= numero de linhas ; cx = horizontal margin ; dx = vertical margin ; ax = line lenght
  303. ; output -
  304. ; destroi -
  305. ;*****************************************************************        
  306.    
  307.     hLine proc
  308.            
  309.         n_hlines:
  310.             push ax
  311.             push cx
  312.            
  313.             h_change_pixel:
  314.             push ax
  315.             mov ah, 0Ch
  316.             mov al, 0Fh
  317.             int 10h
  318.            
  319.             pop ax
  320.             dec ax
  321.             jz h_done
  322.             inc cx
  323.             jmp h_change_pixel
  324.        
  325.         h_done:
  326.         pop cx
  327.         pop ax
  328.         add dx, 16
  329.         dec bx
  330.         jnz n_hlines
  331.        
  332.         ret
  333.     hLine endp
  334.    
  335.  
  336. ;*****************************************************************
  337. ; vLine -
  338. ; descricao: creates vertical lines for the matrix
  339. ; input - bx= numero de linhas ; cx = horizontal margin ; dx = vertical margin ; ax = matrix_height
  340. ; output -
  341. ; destroi -
  342. ;*****************************************************************        
  343.    
  344.     vLine proc
  345.        
  346.         n_vlines:
  347.             push ax
  348.             push dx
  349.            
  350.             v_change_pixel:
  351.             push ax
  352.             mov ah, 0Ch
  353.             mov al, 0Fh
  354.             int 10h
  355.            
  356.             pop ax
  357.             dec ax
  358.             jz v_done
  359.             inc dx
  360.             jmp v_change_pixel
  361.            
  362.         v_done:
  363.         pop dx
  364.         pop ax
  365.         add cx, 40
  366.         dec bx
  367.         jnz n_vlines
  368.                
  369.         ret
  370.     vLine endp
  371.    
  372.    
  373.    
  374. ;*****************************************************************
  375. ; createMatrix -
  376. ; descricao: creates horizontal lines for the matrix
  377. ; input - bx= numero de linhas ; cx = horizontal margin ; dx = vertical margin ; ax = line lenght
  378. ; output -
  379. ; destroi -
  380. ;*****************************************************************        
  381.  
  382.   createMatrix proc
  383.     mov bx, 05h ; se calhar temos de remover isto
  384.     mov dx, 28
  385.     mov cx, 20
  386.     mov ax, matrix_lenght
  387.     call hLine
  388.    
  389.     mov bx, 05h
  390.     mov cx, 20
  391.     mov dx, 28
  392.     mov ax, matrix_height
  393.     call vLine
  394.    
  395.     ret
  396.  
  397.   createMatrix endp
  398.  
  399. ;*****************************************************************
  400. ; createMatrix -
  401. ; descricao: creates horizontal lines for the matrix
  402. ; input - bx= numero de linhas ; cx = horizontal margin ; dx = vertical margin ; ax = line lenght
  403. ; output -
  404. ; destroi -
  405. ;*****************************************************************        
  406.  
  407.  
  408.   createMenuBox proc
  409.    
  410.     mov bx, 2
  411.     mov ax, 40
  412.     mov cx, 20
  413.     mov dx, 164
  414.     call hLine
  415.    
  416.     mov bx, 2
  417.     mov ax, 17
  418.     mov cx, 20
  419.     mov dx, 164                              
  420.     call vLine
  421.    
  422.     ;writes "Menu" inside the cell
  423.         mov al, 1
  424.         mov bl, 0Fh
  425.         mov cx, 4    ;string lenght
  426.         mov dh, 21
  427.         mov dl, 3
  428.         mov ah, 13h
  429.         mov bp, offset string_menu
  430.         int 10h
  431.  
  432.   ret
  433.    
  434.   createMenuBox endp
  435.    
  436. ;*****************************************************************
  437. ; createFormulaBox -
  438. ; descricao: creates a box where the user will input operations
  439. ; input -
  440. ; output -
  441. ; destroi -
  442. ;*****************************************************************
  443.  
  444.     createFormulaBox proc ;Provavelmente vamos ter de alterar o
  445.                           ;tamanho da caixa da formula
  446.            
  447.         mov ax, 80 ; formula lenght
  448.         mov cx, 20
  449.         mov dx, 124
  450.         mov bx, 02h
  451.         call hLine
  452.        
  453.         mov ax, 17 ; formula height + 1
  454.         mov cx, 20
  455.         mov dx, 124
  456.         mov bx, 01h
  457.         call vLine
  458.        
  459.         mov ax, 17 ; formula height + 1
  460.         mov cx, 100
  461.         mov dx, 124
  462.         mov bx, 01h
  463.         call vLine
  464.        
  465.     ret    
  466.        
  467.     createFormulaBox endp
  468.    
  469.  
  470. ;*****************************************************************
  471. ; createResultBox -
  472. ; descricao: creates a box to output the result
  473. ; input -
  474. ; output -
  475. ; destroi -
  476. ;*****************************************************************
  477.  
  478.     createResultBox proc
  479.        
  480.         mov ax, 80 ;cell lenght
  481.         mov cx, 140
  482.         mov dx, 124
  483.         mov bx, 02h
  484.         call hLine
  485.        
  486.         mov ax, 17 ; formula height + 1
  487.         mov cx, 140
  488.         mov dx, 124
  489.         mov bx, 01h
  490.         call vLine
  491.        
  492.         mov ax, 17 ; formula height + 1
  493.         mov cx, 220
  494.         mov dx, 124
  495.         mov bx, 01h
  496.         call vLine
  497.    
  498.     ret
  499.        
  500.     createResultBox endp
  501.    
  502. ;*****************************************************************
  503. ; showLetter -
  504. ; descricao: prints a letter for each matrix collumn,
  505. ;            also prints "Formula" and "Result"
  506. ; input -
  507. ; output -
  508. ; destroi -
  509. ;*****************************************************************
  510.  
  511.     showLetters proc
  512.        
  513.         xor bh, bh
  514.         mov bl, 0Fh
  515.         mov cx, 1
  516.         mov dl, 04
  517.         mov al, 'A'
  518.        
  519.         new_letter:
  520.             ;set cursor position
  521.             mov dh, 02h
  522.             mov ah, 02h
  523.             int 10h
  524.            
  525.             ;write char at cursor position
  526.             mov ah, 09h
  527.             int 10h
  528.            
  529.             inc al
  530.             add dl, 5
  531.             cmp al, 'E'
  532.             jnz new_letter
  533.        
  534.         ;writes "Formula" above the big cell
  535.         mov al, 1
  536.         mov bl, 0Fh
  537.         mov cx, 7    ;string lenght
  538.         mov dh, 14
  539.         mov dl, 3
  540.         mov ah, 13h
  541.         mov bp, offset string_formula
  542.         int 10h
  543.        
  544.         ;writes "Result" aove the small cell
  545.         mov dl, 18
  546.         mov cx, 6   ;string lenght deve ser 6
  547.         mov bp, offset string_result    
  548.         int 10h  
  549.    
  550.     ret
  551.        
  552.     showLetters endp
  553.    
  554. ;*****************************************************************
  555. ; showNumbers -
  556. ; descricao: prints a number for each matrix row
  557. ; input -
  558. ; output -
  559. ; destroi -
  560. ;*****************************************************************    
  561.  
  562.     showNumbers proc
  563.                    
  564.         xor bh, bh
  565.         mov bl, 0Fh
  566.         mov cx, 1
  567.         mov dh, 04h
  568.         mov al, '1'
  569.        
  570.         new_number:
  571.         ;set cursor position
  572.         mov dl, 01h
  573.         mov ah, 02h
  574.         int 10h
  575.        
  576.         ;write char at cursor position
  577.         mov ah, 09h
  578.         int 10h
  579.        
  580.         inc al
  581.         add dh, 2
  582.         cmp al, '5'
  583.         jnz new_number                    
  584.    
  585.     ret
  586.        
  587.     showNumbers endp
  588.  
  589. ;*****************************************************************
  590. ; matrixCellClicked -
  591. ; descricao: edit clicked cell
  592. ; input -
  593. ; output -
  594. ; destroi -
  595. ;*****************************************************************        
  596.    
  597.     matrixCellClicked proc
  598.        
  599.         xor bx, bx
  600.        
  601.         ;meter o cursor no sitio certo
  602.         push cx
  603.         push dx
  604.        
  605.         mov al, 05h   ;multiplo necessario
  606.         mul dl     ;multiplicar a celula pelo multiplo de posicao
  607.         add al, 03h  ;somar a constante de deslocamento incial
  608.         mov dl, al ;adquirir a posicao em x do cursor
  609.        
  610.         mov al, 02h   ;a mesma coisa mas para o
  611.         mul cl     ;eixo do y
  612.         add al, 04h
  613.         mov dh, al
  614.        
  615.         mov ah, 02h
  616.         int 10h
  617.        
  618.         mov ch, 06h
  619.         mov cl, 0000_1111b
  620.         mov ah, 01h
  621.         int 10h
  622.        
  623.         mov ah,1
  624.         int 21h
  625.        
  626.         pop dx
  627.         pop cx
  628.        
  629.          
  630.     ret    
  631.     matrixCellClicked endp
  632.  
  633.  
  634.  
  635. ;*****************************************************************
  636. ; checkCell -
  637. ; descricao: checks and eveluates what to do with the cell clicked by the user
  638. ; input -
  639. ; output - bx=0(exit to menu)
  640. ; destroi -
  641. ;*****************************************************************        
  642.    
  643.     cANDeCell proc
  644.        
  645.         xor bx, bx ;nao sei se isto e preciso, tem a ver com um racicionio que ja nao me lembro
  646.        
  647.         ;check if user clicked on the margin
  648.        
  649.         cmp cx, 20
  650.         jb done_checking
  651.         cmp dx, 28
  652.         jb done_checking
  653.        
  654.         mov ax, cx
  655.         sub ax, 20
  656.        
  657.         mov bx, 40
  658.         div bl
  659.         xor ah, ah ;sabemos agora a celula das letras
  660.         mov cx, ax ;o resultado esta em cx
  661.        
  662.         mov ax, dx
  663.         sub ax, 28
  664.        
  665.         mov bx, 16
  666.         div bl
  667.         xor ah, ah
  668.         mov dx, ax ;coordonates are now stored in cx(x) and dx(y)
  669.        
  670.         ;check if user clicked on a matrix cell
  671.        
  672.         cmp cx, 3
  673.         ja not_matrix
  674.         cmp dx, 3
  675.         ja not_matrix
  676.         call matrixCellClicked    ;a celula clicada foi uma da matriz descrita por cx e dx
  677.         jmp done_checking
  678.        
  679.         ;check if user clicked on the formula or result cell
  680.         not_matrix:
  681.         cmp dx, 6
  682.         jnz not_fORr
  683.         cmp cx, 1
  684.         ja not_formula
  685.         ;a celula clicada e a celula da formula
  686.         jmp done_checking
  687.        
  688.         not_formula:
  689.         cmp cx, 3
  690.         jnz not_fORr
  691.         cmp cx, 4
  692.         jnz not_fORr
  693.         ;a celula clicada e a celula do resultado
  694.         jmp done_checking
  695.        
  696.         not_fORr:
  697.         ;check if used clicked on MENU
  698.         cmp dx, 8 ;esta na linha do menu
  699.         jnz done_checking
  700.         cmp cx, 0
  701.         jnz done_checking
  702.         xor bx, bx        
  703.         done_checking:
  704.        
  705.     ret
  706.     cANDeCell endp
  707.  
  708. ;*****************************************************************
  709. ; fopen -
  710. ; descricao: abre um ficheiro em modo read(al==0), write(al==1) ou append(al==2)
  711. ; input - dx=offset nome ficheiro
  712. ; output - ax=file_handle
  713. ; destroi -
  714. ;*****************************************************************    
  715.  
  716.     fopen proc
  717.         mov ah, 3Dh
  718.         mov al, 02h
  719.         mov cx, 00h
  720.         int 21h      
  721.        
  722.         ret
  723.     fopen endp
  724.  
  725. ;*****************************************************************
  726. ; fclose -
  727. ; descricao:
  728. ; input - bx= file handle
  729. ; output -
  730. ; destroi -
  731. ;*****************************************************************    
  732.  
  733.     fclose proc
  734.         mov ah, 3Eh
  735.         int 21h      
  736.        
  737.         ret
  738.     fclose endp
  739.    
  740. ;*****************************************************************
  741. ; fcreate -
  742. ; descricao: cria um ficheiro
  743. ; input - dx=offset nome ficheiro
  744. ; output - ax=file_handle
  745. ; destroi -
  746. ;*****************************************************************        
  747.  
  748.     fcreate proc
  749.         mov ah, 3Ch
  750.         mov cx, 00h
  751.         int 21h      
  752.  
  753.         ret    
  754.     fcreate endp
  755.    
  756. ;*****************************************************************
  757. ; save_contents -
  758. ; descricao: saves contents.bin or creates it
  759. ; input -
  760. ; output -
  761. ; destroi -
  762. ;*****************************************************************    
  763.  
  764.     save_contents proc
  765.         ;tries to open file
  766.         mov dx, offset name_contents
  767.         call fopen
  768.         jnc save
  769.        
  770.         ;in case it doesn't exist, creates it
  771.         call fcreate
  772.        
  773.         save:
  774.         mov bx, ax ;move handle
  775.        
  776.         mov cx, 16 ;number of bytes
  777.         mov dx, offset cell ;offset to write to
  778.         mov ah, 40h
  779.         int 21h
  780.                    
  781.         ;closes file                  
  782.         call fclose
  783.        
  784.         ret
  785.     save_contents endp
  786.  
  787. ;*****************************************************************
  788. ; open_contents -
  789. ; descricao: opens contents.bin if it exists
  790. ; input -
  791. ; output -
  792. ; destroi - tudo
  793. ;*****************************************************************    
  794.    
  795.     open_contents proc
  796.         ;tries to open file
  797.         mov dx, offset name_contents
  798.         call fopen
  799.         jc end_open_cont ;in case it doesn't exist yet
  800.        
  801.         mov bx, ax
  802.        
  803.         mov cx, 16 ;number of bytes
  804.         mov dx, offset cell ;offset to write to
  805.         mov ah, 3Fh
  806.         int 21h  
  807.        
  808.         call fclose
  809.         end_open_cont:
  810.         ret
  811.     open_contents endp
  812.    
  813. ends
  814.  
  815. end start ; set entry point and stop the assembler.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement