Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
84
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", '$'                        
  5.     string_formula db "Formula",
  6.     string_result db "Result",
  7.     string_menu db "Menu",
  8.     num_hcell dw 04h
  9.     matrix_lenght dw 160 ;estava 240 antes
  10.     matrix_height dw 65
  11.     cell db -128 dup(16)
  12.     ;formula_lenght 88
  13. ends
  14.  
  15. stack segment
  16.     dw   128  dup(0)
  17. ends
  18.  
  19. code segment
  20. start:
  21. ; set segment registers:
  22.     mov ax, data
  23.     mov ds, ax
  24.     mov es, ax
  25.    
  26.     ;open contents.bin
  27.         call open_contents
  28.    
  29.     ;iniciate video mode
  30.         mov ah, 00h
  31.         mov al, 13h
  32.         int 10h
  33.    
  34.     ;draw spreadsheet    
  35.         ;call createMatrix
  36.        
  37.         call createMenuBox
  38.        
  39.         call createFormulaBox
  40.        
  41.         call createResultBox
  42.        
  43.         call showLetters
  44.        
  45.         call showNumbers
  46.        
  47.         mov ax, 00h
  48.         int 33h
  49.        
  50.     ;wait for user input                              
  51.     keep_checking:
  52.         mov ax, 03h
  53.         int 33h
  54.         shr cx, 1       ; x/2 - in this mode the value of CX is doubled.
  55.         cmp bx, 1
  56.         jnz keep_checking
  57.        
  58.         call cANDeCell  ;check and evaluate cell
  59.        
  60.         ;Preisamos agora de uma funcao para avaliar a celula que foi clicada
  61.         ;ja temos toda a informacao necessaria
  62.         ;devo meter AL para a dizer se a celula e valida
  63.         ;e AH para dizer o tipo de celula
  64.        
  65.         cmp bl, 0     ;exit to MENU
  66.         jnz keep_checking      
  67.    
  68.     ;saves contents.bin
  69.         call save_contents  
  70.    
  71.     mov ax, 4c00h ; exit to operating system.
  72.     int 21h
  73.  
  74.    
  75. ;*****************************************************************
  76. ; hLine -
  77. ; descricao: creates horizontal lines for the matrix
  78. ; input - bx= numero de linhas ; cx = horizontal margin ; dx = vertical margin ; ax = line lenght
  79. ; output -
  80. ; destroi -
  81. ;*****************************************************************        
  82.    
  83.     hLine proc
  84.            
  85.         n_hlines:
  86.             push ax
  87.             push cx
  88.            
  89.             h_change_pixel:
  90.             push ax
  91.             mov ah, 0Ch
  92.             mov al, 0Fh
  93.             int 10h
  94.            
  95.             pop ax
  96.             dec ax
  97.             jz h_done
  98.             inc cx
  99.             jmp h_change_pixel
  100.        
  101.         h_done:
  102.         pop cx
  103.         pop ax
  104.         add dx, 16
  105.         dec bx
  106.         jnz n_hlines
  107.        
  108.         ret
  109.     hLine endp
  110.    
  111.  
  112. ;*****************************************************************
  113. ; vLine -
  114. ; descricao: creates vertical lines for the matrix
  115. ; input - bx= numero de linhas ; cx = horizontal margin ; dx = vertical margin ; ax = matrix_height
  116. ; output -
  117. ; destroi -
  118. ;*****************************************************************        
  119.    
  120.     vLine proc
  121.        
  122.         n_vlines:
  123.             push ax
  124.             push dx
  125.            
  126.             v_change_pixel:
  127.             push ax
  128.             mov ah, 0Ch
  129.             mov al, 0Fh
  130.             int 10h
  131.            
  132.             pop ax
  133.             dec ax
  134.             jz v_done
  135.             inc dx
  136.             jmp v_change_pixel
  137.            
  138.         v_done:
  139.         pop dx
  140.         pop ax
  141.         add cx, 40
  142.         dec bx
  143.         jnz n_vlines
  144.                
  145.         ret
  146.     vLine endp
  147.    
  148.    
  149.    
  150. ;*****************************************************************
  151. ; createMatrix -
  152. ; descricao: creates horizontal lines for the matrix
  153. ; input - bx= numero de linhas ; cx = horizontal margin ; dx = vertical margin ; ax = line lenght
  154. ; output -
  155. ; destroi -
  156. ;*****************************************************************        
  157.  
  158.   createMatrix proc
  159.     mov bx, 05h ; se calhar temos de remover isto
  160.     mov dx, 28
  161.     mov cx, 20
  162.     mov ax, matrix_lenght
  163.     call hLine
  164.    
  165.     mov bx, 05h
  166.     mov cx, 20
  167.     mov dx, 28
  168.     mov ax, matrix_height
  169.     call vLine
  170.    
  171.     ret
  172.  
  173.   createMatrix endp
  174.  
  175. ;*****************************************************************
  176. ; createMatrix -
  177. ; descricao: creates horizontal lines for the matrix
  178. ; input - bx= numero de linhas ; cx = horizontal margin ; dx = vertical margin ; ax = line lenght
  179. ; output -
  180. ; destroi -
  181. ;*****************************************************************        
  182.  
  183.  
  184.   createMenuBox proc
  185.    
  186.     mov bx, 2
  187.     mov ax, 40
  188.     mov cx, 20
  189.     mov dx, 164
  190.     call hLine
  191.    
  192.     mov bx, 2
  193.     mov ax, 17
  194.     mov cx, 20
  195.     mov dx, 164                              
  196.     call vLine
  197.    
  198.     ;writes "Menu" inside the cell
  199.         mov al, 1
  200.         mov bl, 0Fh
  201.         mov cx, 4    ;string lenght
  202.         mov dh, 21
  203.         mov dl, 3
  204.         mov ah, 13h
  205.         mov bp, offset string_menu
  206.         int 10h
  207.  
  208.   ret
  209.    
  210.   createMenuBox endp
  211.    
  212. ;*****************************************************************
  213. ; createFormulaBox -
  214. ; descricao: creates a box where the user will input operations
  215. ; input -
  216. ; output -
  217. ; destroi -
  218. ;*****************************************************************
  219.  
  220.     createFormulaBox proc ;Provavelmente vamos ter de alterar o
  221.                           ;tamanho da caixa da formula
  222.            
  223.         mov ax, 80 ; formula lenght
  224.         mov cx, 20
  225.         mov dx, 124
  226.         mov bx, 02h
  227.         call hLine
  228.        
  229.         mov ax, 17 ; formula height + 1
  230.         mov cx, 20
  231.         mov dx, 124
  232.         mov bx, 01h
  233.         call vLine
  234.        
  235.         mov ax, 17 ; formula height + 1
  236.         mov cx, 100
  237.         mov dx, 124
  238.         mov bx, 01h
  239.         call vLine
  240.        
  241.     ret    
  242.        
  243.     createFormulaBox endp
  244.    
  245.  
  246. ;*****************************************************************
  247. ; createResultBox -
  248. ; descricao: creates a box to output the result
  249. ; input -
  250. ; output -
  251. ; destroi -
  252. ;*****************************************************************
  253.  
  254.     createResultBox proc
  255.        
  256.         mov ax, 80 ;cell lenght
  257.         mov cx, 140
  258.         mov dx, 124
  259.         mov bx, 02h
  260.         call hLine
  261.        
  262.         mov ax, 17 ; formula height + 1
  263.         mov cx, 140
  264.         mov dx, 124
  265.         mov bx, 01h
  266.         call vLine
  267.        
  268.         mov ax, 17 ; formula height + 1
  269.         mov cx, 220
  270.         mov dx, 124
  271.         mov bx, 01h
  272.         call vLine
  273.    
  274.     ret
  275.        
  276.     createResultBox endp
  277.    
  278. ;*****************************************************************
  279. ; showLetter -
  280. ; descricao: prints a letter for each matrix collumn,
  281. ;            also prints "Formula" and "Result"
  282. ; input -
  283. ; output -
  284. ; destroi -
  285. ;*****************************************************************
  286.  
  287.     showLetters proc
  288.        
  289.         xor bh, bh
  290.         mov bl, 0Fh
  291.         mov cx, 1
  292.         mov dl, 04
  293.         mov al, 'A'
  294.        
  295.         new_letter:
  296.             ;set cursor position
  297.             mov dh, 02h
  298.             mov ah, 02h
  299.             int 10h
  300.            
  301.             ;write char at cursor position
  302.             mov ah, 09h
  303.             int 10h
  304.            
  305.             inc al
  306.             add dl, 5
  307.             cmp al, 'E'
  308.             jnz new_letter
  309.        
  310.         ;writes "Formula" above the big cell
  311.         mov al, 1
  312.         mov bl, 0Fh
  313.         mov cx, 7    ;string lenght
  314.         mov dh, 14
  315.         mov dl, 3
  316.         mov ah, 13h
  317.         mov bp, offset string_formula
  318.         int 10h
  319.        
  320.         ;writes "Result" aove the small cell
  321.         mov dl, 18
  322.         mov cx, 6   ;string lenght deve ser 6
  323.         mov bp, offset string_result    
  324.         int 10h  
  325.    
  326.     ret
  327.        
  328.     showLetters endp
  329.    
  330. ;*****************************************************************
  331. ; showNumbers -
  332. ; descricao: prints a number for each matrix row
  333. ; input -
  334. ; output -
  335. ; destroi -
  336. ;*****************************************************************    
  337.  
  338.     showNumbers proc
  339.                    
  340.         xor bh, bh
  341.         mov bl, 0Fh
  342.         mov cx, 1
  343.         mov dh, 04h
  344.         mov al, '1'
  345.        
  346.         new_number:
  347.         ;set cursor position
  348.         mov dl, 01h
  349.         mov ah, 02h
  350.         int 10h
  351.        
  352.         ;write char at cursor position
  353.         mov ah, 09h
  354.         int 10h
  355.        
  356.         inc al
  357.         add dh, 2
  358.         cmp al, '5'
  359.         jnz new_number                    
  360.    
  361.     ret
  362.        
  363.     showNumbers endp
  364.  
  365. ;*****************************************************************
  366. ; matrixCellClicked -
  367. ; descricao: edit clicked cell
  368. ; input -
  369. ; output -
  370. ; destroi -
  371. ;*****************************************************************        
  372.    
  373.     matrixCellClicked proc
  374.        
  375.         xor bx, bx
  376.        
  377.         ;meter o cursor no sitio certo
  378.         push cx
  379.         push dx
  380.        
  381.         mov al, 5   ;multiplo necessario
  382.         mul dl     ;multiplicar a celula pelo multiplo de posicao
  383.         add al, 3  ;somar a constante de deslocamento incial
  384.         mov dh, al ;adquirir a posicao em x do cursor
  385.        
  386.         mov al, 2   ;a mesma coisa mas para o
  387.         mul cl     ;eixo do y
  388.         add al, 4
  389.         mov dl, al
  390.        
  391.         mov ah, 02h
  392.         int 10h
  393.        
  394.         mov al, 'a'
  395.         mov bl, 0Fh
  396.         mov cx, 4
  397.         mov ah, 09h
  398.         int 10h
  399.        
  400.         pop dx
  401.         pop cx
  402.        
  403.          
  404.     ret    
  405.     matrixCellClicked endp
  406.  
  407.  
  408.  
  409. ;*****************************************************************
  410. ; checkCell -
  411. ; descricao: checks and eveluates what to do with the cell clicked by the user
  412. ; input -
  413. ; output -
  414. ; destroi -
  415. ;*****************************************************************        
  416.    
  417.     cANDeCell proc
  418.        
  419.         xor bx, bx ;nao sei se isto e preciso, tem a ver com um racicionio que ja nao me lembro
  420.        
  421.         ;check if user clicked on the margin
  422.        
  423.         cmp cx, 20
  424.         jb done_checking
  425.         cmp dx, 28
  426.         jb done_checking
  427.        
  428.         mov ax, cx
  429.         sub ax, 20
  430.        
  431.         mov bx, 40
  432.         div bl
  433.         xor ah, ah ;sabemos agora a celula das letras
  434.         mov cx, ax ;o resultado esta em cx
  435.        
  436.         mov ax, dx
  437.         sub ax, 28
  438.        
  439.         mov bx, 16
  440.         div bl
  441.         xor ah, ah
  442.         mov dx, ax ;coordonates are now stored in cx(x) and dx(y)
  443.        
  444.         ;check if user clicked on a matrix cell
  445.        
  446.         cmp cx, 3
  447.         ja not_matrix
  448.         cmp dx, 3
  449.         ja not_matrix
  450.         call matrixCellClicked    ;a celula clicada foi uma da matriz descrita por cx e dx
  451.         jmp done_checking
  452.        
  453.         ;check if user clicked on the formula or result cell
  454.         not_matrix:
  455.         cmp dx, 6
  456.         jnz not_fORr
  457.         cmp cx, 1
  458.         ja not_formula
  459.         ;a celula clicada e a celula da formula
  460.         jmp done_checking
  461.        
  462.         not_formula:
  463.         cmp cx, 3
  464.         jnz not_fORr
  465.         cmp cx, 4
  466.         jnz not_fORr
  467.         ;a celula clicada e a celula do resultado
  468.         jmp done_checking
  469.        
  470.         not_fORr:
  471.         ;check if used clicked on MENU
  472.         cmp dx, 8 ;esta na linha do menu
  473.         jnz done_checking
  474.         cmp cx, 0
  475.         jnz done_checking
  476.         xor bx, bx        
  477.         done_checking:
  478.        
  479.     ret
  480.     cANDeCell endp
  481.  
  482. ;*****************************************************************
  483. ; fopen -
  484. ; descricao: abre um ficheiro em modo read(al==0), write(al==1) ou append(al==2)
  485. ; input - dx=offset nome ficheiro
  486. ; output - ax=file_handle
  487. ; destroi -
  488. ;*****************************************************************    
  489.  
  490.     fopen proc
  491.         mov ah, 3Dh
  492.         mov al, 02h
  493.         mov cx, 00h
  494.         int 21h      
  495.        
  496.         ret
  497.     fopen endp
  498.  
  499. ;*****************************************************************
  500. ; fclose -
  501. ; descricao:
  502. ; input - bx= file handle
  503. ; output -
  504. ; destroi -
  505. ;*****************************************************************    
  506.  
  507.     fclose proc
  508.         mov ah, 3Eh
  509.         int 21h      
  510.        
  511.         ret
  512.     fclose endp
  513.    
  514. ;*****************************************************************
  515. ; fcreate -
  516. ; descricao: cria um ficheiro
  517. ; input - dx=offset nome ficheiro
  518. ; output - ax=file_handle
  519. ; destroi -
  520. ;*****************************************************************        
  521.  
  522.     fcreate proc
  523.         mov ah, 3Ch
  524.         mov cx, 00h
  525.         int 21h      
  526.  
  527.         ret    
  528.     fcreate endp
  529.    
  530. ;*****************************************************************
  531. ; save_contents -
  532. ; descricao: saves contents.bin or creates it
  533. ; input -
  534. ; output -
  535. ; destroi -
  536. ;*****************************************************************    
  537.  
  538.     save_contents proc
  539.         ;tries to open file
  540.         mov dx, offset name_contents
  541.         call fopen
  542.         jnc save
  543.        
  544.         ;in case it doesn't exist, creates it
  545.         call fcreate
  546.        
  547.         save:
  548.         mov bx, ax ;move handle
  549.        
  550.         mov cx, 16 ;number of bytes
  551.         mov dx, offset cell ;offset to write to
  552.         mov ah, 40h
  553.         int 21h
  554.                    
  555.         ;closes file                  
  556.         call fclose
  557.        
  558.         ret
  559.     save_contents endp
  560.  
  561. ;*****************************************************************
  562. ; open_contents -
  563. ; descricao: opens contents.bin if it exists
  564. ; input -
  565. ; output -
  566. ; destroi - tudo
  567. ;*****************************************************************    
  568.    
  569.     open_contents proc
  570.         ;tries to open file
  571.         mov dx, offset name_contents
  572.         call fopen
  573.         jc end_open_cont ;in case it doesn't exist yet
  574.        
  575.         mov bx, ax
  576.        
  577.         mov cx, 16 ;number of bytes
  578.         mov dx, offset cell ;offset to write to
  579.         mov ah, 3Fh
  580.         int 21h  
  581.        
  582.         call fclose
  583.         end_open_cont:
  584.         ret
  585.     open_contents endp
  586.    
  587. ends
  588.  
  589. end start ; set entry point and stop the assembler.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement