Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; multi-segment executable file template.
- data segment
- name_contents db "c:\contents.bin", '$'
- string_formula db "Formula",
- string_result db "Result",
- string_menu db "Menu",
- num_hcell dw 04h
- matrix_lenght dw 160 ;estava 240 antes
- matrix_height dw 65
- cell db -128 dup(16)
- ;formula_lenght 88
- ends
- stack segment
- dw 128 dup(0)
- ends
- code segment
- start:
- ; set segment registers:
- mov ax, data
- mov ds, ax
- mov es, ax
- ;open contents.bin
- call open_contents
- ;iniciate video mode
- mov ah, 00h
- mov al, 13h
- int 10h
- ;draw spreadsheet
- ;call createMatrix
- call createMenuBox
- call createFormulaBox
- call createResultBox
- call showLetters
- call showNumbers
- mov ax, 00h
- int 33h
- ;wait for user input
- keep_checking:
- mov ax, 03h
- int 33h
- shr cx, 1 ; x/2 - in this mode the value of CX is doubled.
- cmp bx, 1
- jnz keep_checking
- call cANDeCell ;check and evaluate cell
- ;Preisamos agora de uma funcao para avaliar a celula que foi clicada
- ;ja temos toda a informacao necessaria
- ;devo meter AL para a dizer se a celula e valida
- ;e AH para dizer o tipo de celula
- cmp bl, 0 ;exit to MENU
- jnz keep_checking
- ;saves contents.bin
- call save_contents
- mov ax, 4c00h ; exit to operating system.
- int 21h
- ;*****************************************************************
- ; hLine -
- ; descricao: creates horizontal lines for the matrix
- ; input - bx= numero de linhas ; cx = horizontal margin ; dx = vertical margin ; ax = line lenght
- ; output -
- ; destroi -
- ;*****************************************************************
- hLine proc
- n_hlines:
- push ax
- push cx
- h_change_pixel:
- push ax
- mov ah, 0Ch
- mov al, 0Fh
- int 10h
- pop ax
- dec ax
- jz h_done
- inc cx
- jmp h_change_pixel
- h_done:
- pop cx
- pop ax
- add dx, 16
- dec bx
- jnz n_hlines
- ret
- hLine endp
- ;*****************************************************************
- ; vLine -
- ; descricao: creates vertical lines for the matrix
- ; input - bx= numero de linhas ; cx = horizontal margin ; dx = vertical margin ; ax = matrix_height
- ; output -
- ; destroi -
- ;*****************************************************************
- vLine proc
- n_vlines:
- push ax
- push dx
- v_change_pixel:
- push ax
- mov ah, 0Ch
- mov al, 0Fh
- int 10h
- pop ax
- dec ax
- jz v_done
- inc dx
- jmp v_change_pixel
- v_done:
- pop dx
- pop ax
- add cx, 40
- dec bx
- jnz n_vlines
- ret
- vLine endp
- ;*****************************************************************
- ; createMatrix -
- ; descricao: creates horizontal lines for the matrix
- ; input - bx= numero de linhas ; cx = horizontal margin ; dx = vertical margin ; ax = line lenght
- ; output -
- ; destroi -
- ;*****************************************************************
- createMatrix proc
- mov bx, 05h ; se calhar temos de remover isto
- mov dx, 28
- mov cx, 20
- mov ax, matrix_lenght
- call hLine
- mov bx, 05h
- mov cx, 20
- mov dx, 28
- mov ax, matrix_height
- call vLine
- ret
- createMatrix endp
- ;*****************************************************************
- ; createMatrix -
- ; descricao: creates horizontal lines for the matrix
- ; input - bx= numero de linhas ; cx = horizontal margin ; dx = vertical margin ; ax = line lenght
- ; output -
- ; destroi -
- ;*****************************************************************
- createMenuBox proc
- mov bx, 2
- mov ax, 40
- mov cx, 20
- mov dx, 164
- call hLine
- mov bx, 2
- mov ax, 17
- mov cx, 20
- mov dx, 164
- call vLine
- ;writes "Menu" inside the cell
- mov al, 1
- mov bl, 0Fh
- mov cx, 4 ;string lenght
- mov dh, 21
- mov dl, 3
- mov ah, 13h
- mov bp, offset string_menu
- int 10h
- ret
- createMenuBox endp
- ;*****************************************************************
- ; createFormulaBox -
- ; descricao: creates a box where the user will input operations
- ; input -
- ; output -
- ; destroi -
- ;*****************************************************************
- createFormulaBox proc ;Provavelmente vamos ter de alterar o
- ;tamanho da caixa da formula
- mov ax, 80 ; formula lenght
- mov cx, 20
- mov dx, 124
- mov bx, 02h
- call hLine
- mov ax, 17 ; formula height + 1
- mov cx, 20
- mov dx, 124
- mov bx, 01h
- call vLine
- mov ax, 17 ; formula height + 1
- mov cx, 100
- mov dx, 124
- mov bx, 01h
- call vLine
- ret
- createFormulaBox endp
- ;*****************************************************************
- ; createResultBox -
- ; descricao: creates a box to output the result
- ; input -
- ; output -
- ; destroi -
- ;*****************************************************************
- createResultBox proc
- mov ax, 80 ;cell lenght
- mov cx, 140
- mov dx, 124
- mov bx, 02h
- call hLine
- mov ax, 17 ; formula height + 1
- mov cx, 140
- mov dx, 124
- mov bx, 01h
- call vLine
- mov ax, 17 ; formula height + 1
- mov cx, 220
- mov dx, 124
- mov bx, 01h
- call vLine
- ret
- createResultBox endp
- ;*****************************************************************
- ; showLetter -
- ; descricao: prints a letter for each matrix collumn,
- ; also prints "Formula" and "Result"
- ; input -
- ; output -
- ; destroi -
- ;*****************************************************************
- showLetters proc
- xor bh, bh
- mov bl, 0Fh
- mov cx, 1
- mov dl, 04
- mov al, 'A'
- new_letter:
- ;set cursor position
- mov dh, 02h
- mov ah, 02h
- int 10h
- ;write char at cursor position
- mov ah, 09h
- int 10h
- inc al
- add dl, 5
- cmp al, 'E'
- jnz new_letter
- ;writes "Formula" above the big cell
- mov al, 1
- mov bl, 0Fh
- mov cx, 7 ;string lenght
- mov dh, 14
- mov dl, 3
- mov ah, 13h
- mov bp, offset string_formula
- int 10h
- ;writes "Result" aove the small cell
- mov dl, 18
- mov cx, 6 ;string lenght deve ser 6
- mov bp, offset string_result
- int 10h
- ret
- showLetters endp
- ;*****************************************************************
- ; showNumbers -
- ; descricao: prints a number for each matrix row
- ; input -
- ; output -
- ; destroi -
- ;*****************************************************************
- showNumbers proc
- xor bh, bh
- mov bl, 0Fh
- mov cx, 1
- mov dh, 04h
- mov al, '1'
- new_number:
- ;set cursor position
- mov dl, 01h
- mov ah, 02h
- int 10h
- ;write char at cursor position
- mov ah, 09h
- int 10h
- inc al
- add dh, 2
- cmp al, '5'
- jnz new_number
- ret
- showNumbers endp
- ;*****************************************************************
- ; matrixCellClicked -
- ; descricao: edit clicked cell
- ; input -
- ; output -
- ; destroi -
- ;*****************************************************************
- matrixCellClicked proc
- xor bx, bx
- ;meter o cursor no sitio certo
- push cx
- push dx
- mov al, 5 ;multiplo necessario
- mul dl ;multiplicar a celula pelo multiplo de posicao
- add al, 3 ;somar a constante de deslocamento incial
- mov dh, al ;adquirir a posicao em x do cursor
- mov al, 2 ;a mesma coisa mas para o
- mul cl ;eixo do y
- add al, 4
- mov dl, al
- mov ah, 02h
- int 10h
- mov al, 'a'
- mov bl, 0Fh
- mov cx, 4
- mov ah, 09h
- int 10h
- pop dx
- pop cx
- ret
- matrixCellClicked endp
- ;*****************************************************************
- ; checkCell -
- ; descricao: checks and eveluates what to do with the cell clicked by the user
- ; input -
- ; output -
- ; destroi -
- ;*****************************************************************
- cANDeCell proc
- xor bx, bx ;nao sei se isto e preciso, tem a ver com um racicionio que ja nao me lembro
- ;check if user clicked on the margin
- cmp cx, 20
- jb done_checking
- cmp dx, 28
- jb done_checking
- mov ax, cx
- sub ax, 20
- mov bx, 40
- div bl
- xor ah, ah ;sabemos agora a celula das letras
- mov cx, ax ;o resultado esta em cx
- mov ax, dx
- sub ax, 28
- mov bx, 16
- div bl
- xor ah, ah
- mov dx, ax ;coordonates are now stored in cx(x) and dx(y)
- ;check if user clicked on a matrix cell
- cmp cx, 3
- ja not_matrix
- cmp dx, 3
- ja not_matrix
- call matrixCellClicked ;a celula clicada foi uma da matriz descrita por cx e dx
- jmp done_checking
- ;check if user clicked on the formula or result cell
- not_matrix:
- cmp dx, 6
- jnz not_fORr
- cmp cx, 1
- ja not_formula
- ;a celula clicada e a celula da formula
- jmp done_checking
- not_formula:
- cmp cx, 3
- jnz not_fORr
- cmp cx, 4
- jnz not_fORr
- ;a celula clicada e a celula do resultado
- jmp done_checking
- not_fORr:
- ;check if used clicked on MENU
- cmp dx, 8 ;esta na linha do menu
- jnz done_checking
- cmp cx, 0
- jnz done_checking
- xor bx, bx
- done_checking:
- ret
- cANDeCell endp
- ;*****************************************************************
- ; fopen -
- ; descricao: abre um ficheiro em modo read(al==0), write(al==1) ou append(al==2)
- ; input - dx=offset nome ficheiro
- ; output - ax=file_handle
- ; destroi -
- ;*****************************************************************
- fopen proc
- mov ah, 3Dh
- mov al, 02h
- mov cx, 00h
- int 21h
- ret
- fopen endp
- ;*****************************************************************
- ; fclose -
- ; descricao:
- ; input - bx= file handle
- ; output -
- ; destroi -
- ;*****************************************************************
- fclose proc
- mov ah, 3Eh
- int 21h
- ret
- fclose endp
- ;*****************************************************************
- ; fcreate -
- ; descricao: cria um ficheiro
- ; input - dx=offset nome ficheiro
- ; output - ax=file_handle
- ; destroi -
- ;*****************************************************************
- fcreate proc
- mov ah, 3Ch
- mov cx, 00h
- int 21h
- ret
- fcreate endp
- ;*****************************************************************
- ; save_contents -
- ; descricao: saves contents.bin or creates it
- ; input -
- ; output -
- ; destroi -
- ;*****************************************************************
- save_contents proc
- ;tries to open file
- mov dx, offset name_contents
- call fopen
- jnc save
- ;in case it doesn't exist, creates it
- call fcreate
- save:
- mov bx, ax ;move handle
- mov cx, 16 ;number of bytes
- mov dx, offset cell ;offset to write to
- mov ah, 40h
- int 21h
- ;closes file
- call fclose
- ret
- save_contents endp
- ;*****************************************************************
- ; open_contents -
- ; descricao: opens contents.bin if it exists
- ; input -
- ; output -
- ; destroi - tudo
- ;*****************************************************************
- open_contents proc
- ;tries to open file
- mov dx, offset name_contents
- call fopen
- jc end_open_cont ;in case it doesn't exist yet
- mov bx, ax
- mov cx, 16 ;number of bytes
- mov dx, offset cell ;offset to write to
- mov ah, 3Fh
- int 21h
- call fclose
- end_open_cont:
- ret
- open_contents endp
- ends
- end start ; set entry point and stop the assembler.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement