Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
67
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.     string_formula db "Formula", 0
  6.     string_result db "Result", 0
  7.     string_menu db "Menu", 0
  8.     num_hcell dw 04h
  9.     matrix_lenght dw 200 ;estava 240 antes
  10.     matrix_height dw 64
  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.     keep_checking:
  51.         mov ax, 03h
  52.         int 33h
  53.         shr cx, 1       ; x/2 - in this mode the value of CX is doubled.
  54.         cmp bx, 1
  55.         jnz keep_checking
  56.         call checkCell      
  57.    
  58.     ;saves contents.bin
  59.         ;call save_contents  
  60.    
  61.     mov ax, 4c00h ; exit to operating system.
  62.     int 21h
  63.  
  64.    
  65. ;*****************************************************************
  66. ; hLine -
  67. ; descricao: creates horizontal lines for the matrix
  68. ; input - bx= numero de linhas ; cx = horizontal margin ; dx = vertical margin ; ax = line lenght
  69. ; output -
  70. ; destroi -
  71. ;*****************************************************************        
  72.    
  73.     hLine proc
  74.            
  75.         n_hlines:
  76.             push ax
  77.             push cx
  78.            
  79.             h_change_pixel:
  80.             push ax
  81.             mov ah, 0Ch
  82.             mov al, 0Fh
  83.             int 10h
  84.            
  85.             pop ax
  86.             dec ax
  87.             jz h_done
  88.             inc cx
  89.             jmp h_change_pixel
  90.        
  91.         h_done:
  92.         pop cx
  93.         pop ax
  94.         add dx, 16
  95.         dec bx
  96.         jnz n_hlines
  97.        
  98.         ret
  99.     hLine endp
  100.    
  101.  
  102. ;*****************************************************************
  103. ; vLine -
  104. ; descricao: creates vertical lines for the matrix
  105. ; input - bx= numero de linhas ; cx = horizontal margin ; dx = vertical margin ; ax = matrix_height
  106. ; output -
  107. ; destroi -
  108. ;*****************************************************************        
  109.    
  110.     vLine proc
  111.        
  112.         n_vlines:
  113.             push ax
  114.             push dx
  115.            
  116.             v_change_pixel:
  117.             push ax
  118.             mov ah, 0Ch
  119.             mov al, 0Fh
  120.             int 10h
  121.            
  122.             pop ax
  123.             dec ax
  124.             jz v_done
  125.             inc dx
  126.             jmp v_change_pixel
  127.            
  128.         v_done:
  129.         pop dx
  130.         pop ax
  131.         add cx, 50
  132.         dec bx
  133.         jnz n_vlines
  134.                
  135.         ret
  136.     vLine endp
  137.    
  138.    
  139.    
  140. ;*****************************************************************
  141. ; createMatrix -
  142. ; descricao: creates horizontal lines for the matrix
  143. ; input - bx= numero de linhas ; cx = horizontal margin ; dx = vertical margin ; ax = line lenght
  144. ; output -
  145. ; destroi -
  146. ;*****************************************************************        
  147.  
  148.   createMatrix proc
  149.     mov bx, 05h ; se calhar temos de remover isto
  150.     mov dx, 23
  151.     mov cx, 23
  152.     mov ax, matrix_lenght
  153.     call hLine
  154.    
  155.     mov bx, 05h
  156.     mov cx, 23
  157.     mov dx, 23
  158.     mov ax, matrix_height
  159.     call vLine
  160.    
  161.     ret
  162.  
  163.   createMatrix endp
  164.  
  165. ;*****************************************************************
  166. ; createMatrix -
  167. ; descricao: creates horizontal lines for the matrix
  168. ; input - bx= numero de linhas ; cx = horizontal margin ; dx = vertical margin ; ax = line lenght
  169. ; output -
  170. ; destroi -
  171. ;*****************************************************************        
  172.  
  173.  
  174.   createMenuBox proc
  175.    
  176.     mov bx, 2
  177.     mov ax, 50
  178.     mov cx, 23
  179.     mov dx, 164
  180.     call hLine
  181.    
  182.     mov bx, 2
  183.     mov ax, 16
  184.     mov cx, 23
  185.     mov dx, 164                              
  186.     call vLine
  187.    
  188.     ;writes "Menu" inside the cell
  189.         mov al, 1
  190.         mov bl, 0Fh
  191.         mov cx, 5    ;string lenght
  192.         mov dh, 21
  193.         mov dl, 4
  194.         mov ah, 13h
  195.         mov bp, offset string_menu
  196.         int 10h
  197.  
  198.   ret
  199.    
  200.   createMenuBox endp
  201.    
  202. ;*****************************************************************
  203. ; createFormulaBox -
  204. ; descricao: creates a box where the user will input operations
  205. ; input -
  206. ; output -
  207. ; destroi -
  208. ;*****************************************************************
  209.  
  210.     createFormulaBox proc ;Provavelmente vamos ter de alterar o
  211.                           ;tamanho da caixa da formula
  212.            
  213.         mov ax, 120 ; formula lenght
  214.         mov cx, 23
  215.         mov dx, 140
  216.         mov bx, 02h
  217.         call hLine
  218.        
  219.         mov ax, 16 ; formula height
  220.         mov cx, 23
  221.         mov dx, 140
  222.         mov bx, 01h
  223.         call vLine
  224.        
  225.         mov ax, 16 ; formula height
  226.         mov cx, 143
  227.         mov dx, 140
  228.         mov bx, 01h
  229.         call vLine
  230.        
  231.     ret    
  232.        
  233.     createFormulaBox endp
  234.    
  235.  
  236. ;*****************************************************************
  237. ; createResultBox -
  238. ; descricao: creates a box to output the result
  239. ; input -
  240. ; output -
  241. ; destroi -
  242. ;*****************************************************************
  243.  
  244.     createResultBox proc
  245.        
  246.         mov ax, 60 ;cell lenght
  247.         mov cx, 200
  248.         mov dx, 140
  249.         mov bx, 02h
  250.         call hLine
  251.        
  252.         mov ax, 16 ; formula height
  253.         mov cx, 200
  254.         mov dx, 140
  255.         mov bx, 01h
  256.         call vLine
  257.        
  258.         mov ax, 16 ; formula height
  259.         mov cx, 260
  260.         mov dx, 140
  261.         mov bx, 01h
  262.         call vLine
  263.    
  264.     ret
  265.        
  266.     createResultBox endp
  267.    
  268. ;*****************************************************************
  269. ; showLetter -
  270. ; descricao: prints a letter for each matrix collumn,
  271. ;            also prints "Formula" and "Result"
  272. ; input -
  273. ; output -
  274. ; destroi -
  275. ;*****************************************************************
  276.  
  277.     showLetters proc
  278.        
  279.         xor bh, bh
  280.         mov bl, 0Fh
  281.         mov cx, 1
  282.         mov dl, 06
  283.         mov al, 'A'
  284.        
  285.         new_letter:
  286.             ;set cursor position
  287.             mov dh, 01h
  288.             mov ah, 02h
  289.             int 10h
  290.            
  291.             ;write char at cursor position
  292.             mov ah, 09h
  293.             int 10h
  294.            
  295.             inc al
  296.             add dl, 6
  297.             cmp al, 'E'
  298.             jnz new_letter
  299.        
  300.         ;writes "Formula" above the big cell
  301.         mov al, 1
  302.         mov bl, 0Fh
  303.         mov cx, 8    ;string lenght
  304.         mov dh, 16
  305.         mov dl, 3
  306.         mov ah, 13h
  307.         mov bp, offset string_formula
  308.         int 10h
  309.        
  310.         ;writes "Result" aove the small cell
  311.         mov dl, 25
  312.         mov cx, 7   ;string lenght
  313.         mov bp, offset string_result    
  314.         int 10h  
  315.    
  316.     ret
  317.        
  318.     showLetters endp
  319.    
  320. ;*****************************************************************
  321. ; showNumbers -
  322. ; descricao: prints a number for each matrix row
  323. ; input -
  324. ; output -
  325. ; destroi -
  326. ;*****************************************************************    
  327.  
  328.     showNumbers proc
  329.                    
  330.         xor bh, bh
  331.         mov bl, 0Fh
  332.         mov cx, 1
  333.         mov dh, 03h
  334.         mov al, '1'
  335.        
  336.         new_number:
  337.         ;set cursor position
  338.         mov dl, 01h
  339.         mov ah, 02h
  340.         int 10h
  341.        
  342.         ;write char at cursor position
  343.         mov ah, 09h
  344.         int 10h
  345.        
  346.         inc al
  347.         add dh, 2
  348.         cmp al, '5'
  349.         jnz new_number                    
  350.    
  351.     ret
  352.        
  353.     showNumbers endp
  354.  
  355. ;*****************************************************************
  356. ; checkCell -
  357. ; descricao: prints a number for each matrix row
  358. ; input -
  359. ; output -
  360. ; destroi -
  361. ;*****************************************************************        
  362.    
  363.     checkCell proc
  364.        
  365.         mov ax, cx
  366.         sub ax, 23 ;margin
  367.        
  368.         mov bx, 50
  369.         div bl
  370.         xor ah, ah     ;sabemos agora a celula das letras
  371.         mov cx, ax ;o resultado esta em cx
  372.        
  373.         mov ax, dx
  374.         sub ax, 23
  375.        
  376.         mov bx, 16
  377.         div bl
  378.         xor ah, ah
  379.         mov dx, ax ;sabemos agora a celula dos numeros    
  380.        
  381.     ret
  382.     checkCell endp
  383.  
  384. ;*****************************************************************
  385. ; fopen -
  386. ; descricao: abre um ficheiro em modo read(al==0), write(al==1) ou append(al==2)
  387. ; input - dx=offset nome ficheiro
  388. ; output - ax=file_handle
  389. ; destroi -
  390. ;*****************************************************************    
  391.  
  392.     fopen proc
  393.         mov ah, 3Dh
  394.         mov al, 02h
  395.         mov cx, 00h
  396.         int 21h      
  397.        
  398.         ret
  399.     fopen endp
  400.  
  401. ;*****************************************************************
  402. ; fclose -
  403. ; descricao:
  404. ; input - bx= file handle
  405. ; output -
  406. ; destroi -
  407. ;*****************************************************************    
  408.  
  409.     fclose proc
  410.         mov ah, 3Eh
  411.         int 21h      
  412.        
  413.         ret
  414.     fclose endp
  415.    
  416. ;*****************************************************************
  417. ; fcreate -
  418. ; descricao: cria um ficheiro
  419. ; input - dx=offset nome ficheiro
  420. ; output - ax=file_handle
  421. ; destroi -
  422. ;*****************************************************************        
  423.  
  424.     fcreate proc
  425.         mov ah, 3Ch
  426.         mov cx, 00h
  427.         int 21h      
  428.  
  429.         ret    
  430.     fcreate endp
  431.    
  432. ;*****************************************************************
  433. ; save_contents -
  434. ; descricao: saves contents.bin or creates it
  435. ; input -
  436. ; output -
  437. ; destroi -
  438. ;*****************************************************************    
  439.  
  440.     save_contents proc
  441.         ;tries to open file
  442.         mov dx, offset name_contents
  443.         call fopen
  444.         jnc save
  445.        
  446.         ;in case it doesn't exist, creates it
  447.         call fcreate
  448.        
  449.         save:
  450.         mov bx, ax ;move handle
  451.        
  452.         mov cx, 0 ;number of bytes
  453.         mov dx, 0 ;offset to write to
  454.         mov ah, 40h
  455.         int 21h
  456.                    
  457.         ;closes file                  
  458.         call fclose
  459.        
  460.         ret
  461.     save_contents endp
  462.  
  463. ;*****************************************************************
  464. ; open_contents -
  465. ; descricao: opens contents.bin if it exists
  466. ; input -
  467. ; output -
  468. ; destroi -
  469. ;*****************************************************************    
  470.    
  471.     open_contents proc
  472.         ;tries to open file
  473.         mov dx, offset name_contents
  474.         call fopen
  475.         jc end_open_cont ;in case it doesn't exist yet
  476.        
  477.         mov bx, ax
  478.        
  479.         mov cx, 0 ;number of bytes
  480.         mov dx, 0 ;offset to write to
  481.         mov ah, 3Fh  
  482.        
  483.         call fclose
  484.         end_open_cont:
  485.         ret
  486.     open_contents endp
  487.    
  488. ends
  489.  
  490. end start ; set entry point and stop the assembler.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement