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", 0
- string_menu_1 db "- Import Spreadsheet";length=20
- string_menu_2 db "- Show spreadsheet" ;length=18
- string_menu_3 db "- Edit spreadsheet" ;length=18
- string_menu_4 db "- Export spreadsheet";length=20
- string_menu_5 db "- About" ;length=7
- string_menu_6 db "- Exit" ;length=6
- string_formula db "Formula"
- string_result db "Result"
- string_menu db "Menu"
- str_buffer db 7, dup(0)
- num_hcell dw 04h
- matrix_lenght dw 160 ;estava 240 antes
- matrix_height dw 65
- cell db 16 dup(-128)
- ;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
- draw_menu:
- ;iniciate video mode
- mov ah, 00h
- mov al, 13h
- int 10h
- call menu
- keep_checking_menu:
- 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_menu
- call checkMenu
- cmp dh, 01h
- jne draw_menu
- ;saves contents.bin
- call save_contents
- mov ax, 4c00h ; exit to operating system.
- int 21h
- ;*****************************************************************
- ; menu -
- ; descricao: draws menu
- ; input -
- ; output -
- ; destroi -
- ;*****************************************************************
- menu proc
- xor dh, dh
- mov al, 01h
- mov bh, 00h
- mov bl, 0Fh
- mov dl, 02h
- mov cx, 14h;length of string_1
- mov bp, offset string_menu_1
- imprime_string:
- inc dh
- inc dh ;next line
- push bx
- xor bh, bh
- mov ah, 13h
- int 10h
- pop bx
- inc bh ;next string
- cmp bh, 02h
- je menu_2
- cmp bh, 03h
- je menu_3
- cmp bh, 04h
- je menu_4
- cmp bh, 05h
- je menu_5
- ja end_of_menu
- mov cx, 12h
- mov bp, offset string_menu_2
- jmp imprime_string
- menu_2:
- mov bp, offset string_menu_3
- jmp imprime_string
- menu_3:
- mov cx, 14h
- mov bp, offset string_menu_4
- jmp imprime_string
- menu_4:
- mov cx, 07h
- mov bp, offset string_menu_5
- jmp imprime_string
- menu_5:
- mov cx, 06h
- mov bp, offset string_menu_6
- jmp imprime_string
- end_of_menu:
- ret
- menu endp
- ;*****************************************************************
- ; checkMenu -
- ; descricao: checks position clicked on menu window
- ; input -
- ; output -
- ; destroi -
- ;*****************************************************************
- checkMenu proc
- xor dh, dh
- cmp cx, 13h ;left limit x
- jb done_checking_menu
- cmp cx, 178 ;right limit x
- ja done_checking_menu
- cmp dx, 12h ;superior limit y
- jb done_checking_menu
- cmp dx, 6Ch ;inferior limit y
- ja done_checking_menu
- cmp dx, 1Bh ;test import
- ja test_show
- call import_spreadsheet
- jmp done_checking_menu
- test_show:
- cmp dx, 2Bh
- ja test_edit
- call show_spreadsheet
- jmp done_checking_menu
- test_edit:
- cmp dx, 3Bh
- ja test_export
- call edit_spreadsheet
- jmp done_checking_menu
- test_export:
- cmp dx, 4Bh
- ja test_about
- call export_spreadsheet
- jmp done_checking_menu
- test_about:
- cmp dx, 5Bh
- ja definitely_exit
- call show_about
- jmp done_checking_menu
- definitely_exit:
- mov dh, 01h
- done_checking_menu:
- ret
- checkMenu endp
- ;*****************************************************************
- ; import_spreadsheet -
- ; descricao: importa um ficheiro .txt
- ; input -
- ; output -
- ; destroi -
- ;*****************************************************************
- import_spreadsheet proc
- ret
- import_spreadsheet endp
- ;*****************************************************************
- ; show_spreadsheet -
- ; descricao: mostra a spreadsheet em memoria
- ; input -
- ; output -
- ; destroi -
- ;*****************************************************************
- show_spreadsheet proc
- ;reiniciate video mode to clear window
- mov ah, 00h
- mov al, 13h
- int 10h
- ret
- show_spreadsheet endp
- ;*****************************************************************
- ; edit_spreadsheet -
- ; descricao: mostre e permite editar a spreadsheet em memoria
- ; input -
- ; output -
- ; destroi -
- ;*****************************************************************
- edit_spreadsheet proc
- ;reiniciate video mode to clear window
- 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 bx, 1 ;exit to MENU
- jne keep_checking
- ret
- edit_spreadsheet endp
- ;*****************************************************************
- ; export_spreadsheet -
- ; descricao: exporta para um ficheiro .txt a funcao em memoria
- ; input -
- ; output -
- ; destroi -
- ;*****************************************************************
- export_spreadsheet proc
- ret
- export_spreadsheet endp
- ;*****************************************************************
- ; show_about -
- ; descricao: mostra certas informacoes sobre os criadores
- ; input -
- ; output -
- ; destroi -
- ;*****************************************************************
- show_about proc
- ;reiniciate video mode to clear window
- mov ah, 00h
- mov al, 13h
- int 10h
- ret
- show_about endp
- ;*****************************************************************
- ; 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, 156
- call hLine
- mov bx, 2
- mov ax, 17
- mov cx, 20
- mov dx, 156
- call vLine
- ;writes "Menu" inside the cell
- mov al, 1
- mov bl, 0Fh
- mov cx, 4 ;string lenght
- mov dh, 20
- 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
- ;*****************************************************************
- ; checkCell -
- ; descricao: checks and eveluates what to do with the cell clicked by the user
- ; input -
- ; output - bx=0(exit to menu)
- ; 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
- mov bx, 1
- done_checking:
- ret
- cANDeCell endp
- ;*****************************************************************
- ; matrixCellClicked -
- ; descricao: edit clicked cell
- ; input -
- ; output -
- ; destroi -
- ;*****************************************************************
- matrixCellClicked proc
- ;meter o cursor no sitio certo
- push cx
- push dx
- mov al, 05h ;multiplo necessario
- mul cl ;multiplicar a celula pelo multiplo de posicao
- add al, 03h ;somar a constante de deslocamento incial
- mov bl, al ;adquirir a posicao em x do cursor
- mov al, 02h ;a mesma coisa mas para o
- mul dl ;eixo do y
- add al, 04h
- mov dl, bl ;from cx
- mov dh, al ;from dx
- push dx
- xor bx, bx
- mov ah, 02h
- int 10h
- mov ch, 06h
- mov cl, 0000_1111b
- mov ah, 01h
- int 10h
- ;modificacoes comecam aqui
- mov dx, offset str_buffer
- mov ah, 0Ah
- int 21h
- ;se o buffer estiver todo cheio entao o utilizador meteu mal os numeros
- ;falta fazer essa comparacao
- pop dx
- call evaluateInput ;ficamos com o valor em decimal no registo bx
- pop dx
- pop cx
- push bx
- xor bx, bx
- mov al, 4
- mul cl
- mov bl, al
- add bx, dx
- pop cx
- mov cell[bx], cl
- ret
- matrixCellClicked endp
- ;*****************************************************************
- ; evaluateInput -
- ; descricao: checks if input is valid, startes checking from the last number
- ; input - DL,DH = column, row at which to start writing
- ; output -
- ; destroi -
- ;*****************************************************************
- evaluateInput proc
- xor bx, bx
- mov bl, str_buffer[1]
- ;mov buffer[bx + 2], '$'
- cmp bl, 4
- ja invalid_input
- mov si, bx
- check_input:
- dec si
- cmp si, -1
- jz input_checked
- cmp si, 0
- jnz check_char
- cmp ch, '-'
- jz negative_num
- mov ax, 0
- check_char:
- mov ch, str_buffer[si + 2]
- cmp ch, '0'
- jb invalid_input
- cmp ch, '9'
- ja invalid_input
- jmp check_input
- negative_num:
- mov ax, 1
- input_checked:
- ;push ax
- ;mov al, 1
- ;xor bh, bh
- ;mov cx, bx ;string lenght was in bl
- ;mov bl, 0Fh
- ;mov bp, offset str_buffer
- ;add bp, 2
- ;int 10h
- ;pop ax
- call asciiToDecimal ;ficamos com o valor em decimal no registo bx
- invalid_input:
- ret
- evaluateInput endp
- ;*****************************************************************
- ; asciiToDecimal -
- ; descricao:
- ; input - ax = 1 if number is negative, ax = 0 if number is positive
- ; output - bx = numero para guardar
- ; destroi -
- ;*****************************************************************
- asciiToDecimal proc
- push ax
- xor ax, ax
- xor bx, bx
- xor ch, ch
- mov cl, str_buffer[1]
- mov si, cx
- mov al, 1
- mov dl, 10
- next_number:
- dec si
- cmp si, 0
- jz last_char
- push ax ;isto deve dar erro porque eu acho que so queria al
- mov dh, str_buffer[si + 2]
- sub dh, '0'
- mul dh
- add bx, ax ;o numero esta no bx
- pop ax
- mul dl
- jmp next_number
- last_char:
- pop cx
- cmp cx, 1 ;verificar se o numero e negativo ou nao
- jnz pos_num
- neg bx
- jmp ascii_dec_done:
- pos_num:
- ;mul dl ;para meter o ax com o multiplo correcto
- mov dh, str_buffer[2]
- sub dh, '0'
- mul dh
- add bx, ax
- ascii_dec_done:
- ret
- asciiToDecimal 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