Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;Calculator V1.5 By cmk20
- ;The functions resetTerminal, newLine, print, and divide we writen by SopaXorzTaker (Library link: https://gist.github.com/SopaXorzTaker/26fcc318bdc3781727ba748ccb3194ff)
- ;The dividing function is currently bugged... not sure why
- ;
- ; Initialize the Stack Pointer
- mov sp, 0x300
- mov bx, 0 ;The term port
- jmp main
- ; Resets the terminal to look pretty
- resetTerminal:
- push ax
- push cx
- mov ax, 0x1000
- send 0, ax
- mov ax, 0x208A
- send 0, ax
- mov ax, 0x3000
- mov cx, 24
- .loop:
- send 0, ax
- sub cx, 1
- cmp cx, 0
- jne .loop
- pop cx
- pop ax
- ret
- ; Scrolls the text down.
- newLine:
- send bx, 0x3000
- send bx, 0x1000
- ret
- ; Prints a byte (not word!) string.
- ; DX - the starting word of the string.
- print:
- push ax
- push bx
- push cx
- .loop:
- mov ax, [dx]
- test ax, ax
- jz .end
- mov bx, ax
- mov cx, ax
- shr bx, 8
- test bx, bx
- jz .end
- send 0, bx
- and cx, 0xFF
- test cx, cx
- jz .end
- send 0, cx
- add dx, 1
- jmp .loop
- .end:
- pop ax
- pop bx
- pop cx
- ret
- ; Prints a number in decimal.
- ; AX - the number to print.
- printInt:
- push ax
- push bx
- push cx
- push dx
- push ex
- mov bx, 0
- mov cx, 0
- mov dx, 0
- mov ex, 0
- .k10:
- cmp ax, 10000
- jl .k
- sub ax, 10000
- add ex, 1
- jmp .k10
- .k:
- cmp ax, 1000
- jl .h
- sub ax, 1000
- add dx, 1
- jmp .k
- .h:
- cmp ax, 100
- jl .t
- sub ax, 100
- add cx, 1
- jmp .h
- .t:
- cmp ax, 10
- jl .end
- sub ax, 10
- add bx, 1
- jmp .t
- .end:
- test ex, ex
- jnz .digits
- test dx, dx
- jnz .skip1
- test cx, cx
- jnz .skip2
- test bx, bx
- jnz .skip3
- jmp .skip4
- .digits:
- mov ex, [ex+decimals]
- send 0, ex
- .skip1:
- mov ex, [dx+decimals]
- send 0, ex
- .skip2:
- mov ex, [cx+decimals]
- send 0, ex
- .skip3:
- mov ex, [bx+decimals]
- send 0, ex
- .skip4:
- mov ex, [ax+decimals]
- send 0, ex
- pop ex
- pop dx
- pop cx
- pop bx
- pop ax
- ret
- ; Divide a whole number. In: AX - dividend, BX - divisor
- ; Out: AX - quotient, BX - remainder
- divide:
- push cx
- mov cx, 0
- test bx, bx
- jz .end
- .loop:
- cmp ax, bx
- jl .end
- sub ax, bx
- add cx, 1
- jmp .loop
- .end:
- mov bx, ax
- mov ax, cx
- pop cx
- ret
- ;Multiply a number "ax" by "cx"
- multiply:
- push bx
- mov bx, ax ;Store ax's starting value
- loop:
- cmp cx, 1
- je .end ;when cx is 1 the multiplication is compleate
- add ax, bx ;Add ax to it's starting state
- sub cx, 1
- jmp loop
- .end:
- pop bx
- ret
- ;Imput a number and save it to ax
- inputNum:
- push cx
- mov ax, 0
- send bx, 0x3E ;Print a ">"
- .loop:
- mov cx, 10 ;Set multiplyer to 10
- recv dx, bx ;Recv the char
- cmp dx, 10 ;If the char is ENTER
- je .end ;End the input
- sub dx, 48 ;Convert the ascii to a number
- call multiply ;Multiply the current number value in ax by ten
- add ax, dx ;Add the newly inputed number to ax
- push ax
- mov ax, dx
- call printInt ;Print the newly inputed number
- pop ax
- jmp .loop
- .end:
- pop cx
- ret
- ;Resets the display for the fist screen (It just displays some strings)
- mainMenuReset:
- mov ax, 0 ;Reset the registers (for a clean start if the program loops)
- mov bx, 0
- mov cx, 0
- mov dx, 0
- mov ex, 0
- call resetTerminal
- mov dx, info
- call print
- send bx, 0x1041
- mov dx, op1
- call print
- send bx, 0x1061
- mov dx, op2
- call print
- send bx, 0x1081
- mov dx, op3
- call print
- send bx, 0x10A1
- mov dx, op4
- call print
- send bx, 0x10E1
- mov dx, op5
- call print
- send bx, 0x1101
- ret
- main:
- call mainMenuReset ;Reset the menu
- call inputNum ;Get the option
- cmp ax, 1 ;Then select a option
- je adding
- cmp ax, 2
- je subtracting
- cmp ax, 3
- je multiplying
- cmp ax, 4
- je dividing
- jmp main ;If the number is not 1 thru 4 loop back to main
- ;Adding menu
- adding:
- call resetTerminal ;Reset the term and display some text
- mov dx, op1
- call print
- send bx, 0x1041
- mov dx, mOp1
- call print
- send bx, 0x1061
- call inputNum ;Get the first number
- mov ex, ax ;Store it in ex temporarily
- send bx, 0x1081
- mov dx, mOp2
- call print
- send bx, 0x10A1
- call inputNum ;Get the second number
- add ex, ax ;Add them together
- send bx, 0x10E1
- mov dx, answer
- call print
- send bx, 0x1161
- mov ax, ex ;Move the answer to ax then print it
- call printInt
- send bx, 0x12E1
- mov dx, exit
- call print
- recv dx, 0 ;Wait for a button press then go back to main
- jmp main
- ;Same as adding except we use sub instead of add
- subtracting:
- call resetTerminal
- mov dx, op2
- call print
- send bx, 0x1041
- mov dx, mOp1
- call print
- send bx, 0x1061
- call inputNum
- mov ex, ax
- send bx, 0x1081
- mov dx, mOp2
- call print
- send bx, 0x10A1
- call inputNum
- sub ex, ax
- send bx, 0x10E1
- mov dx, answer
- call print
- send bx, 0x1161
- mov ax, ex
- call printInt
- send bx, 0x12E1
- mov dx, exit
- call print
- recv dx, 0
- jmp main
- ;Similar to adding however we need to move some registers around to use the multiply function properly
- multiplying:
- call resetTerminal
- mov dx, op3
- call print
- send bx, 0x1041
- mov dx, mOp1
- call print
- send bx, 0x1061
- call inputNum
- mov cx, ax ;Put input in cx instead of ex
- send bx, 0x1081
- mov dx, mOp2
- call print
- send bx, 0x10A1
- call inputNum
- xchg ax, cx ;Exchange ex and cx's values to make sure you are multpling number 1 buy number 2
- call multiply
- mov ex, ax ;Put the result in ex
- send bx, 0x10E1
- mov dx, answer
- call print
- send bx, 0x1161
- mov ax, ex
- call printInt ;Print it
- send bx, 0x12E1
- mov dx, exit
- call print
- recv dx, 0
- jmp main
- ;Similar to multiply but it's broken :P
- dividing:
- call resetTerminal
- mov dx, op4
- call print
- send bx, 0x1041
- mov dx, mOp1
- call print
- send bx, 0x1061
- call inputNum
- mov cx, ax ;Store the first number in cx, again
- send bx, 0x1081
- mov dx, mOp2
- call print
- send bx, 0x10A1
- call inputNum
- mov bx, cx ;Put the fist number in bx (Needed for the divide function)
- xchg ax, bx ;Exchnage the values
- call divide
- mov cx, bx ;Put the remainder in cx (The quotient is already in ax)
- mov bx, 0
- send bx, 0x10E1
- mov dx, answer
- call print
- send bx, 0x1101
- call printInt ;Print The quotient
- send bx, 0x1121
- mov dx, dividingAns
- call print
- send bx, 0x1161
- mov ax, cx
- call printInt ;Print the remainder
- send bx, 0x12E1
- mov dx, exit
- call print
- recv dx, 0
- jmp main
- info: db "Calculator V1.5 By Cmk20", 0
- op1: db "1 - Addition", 0
- op2: db "2 - Subtraction", 0
- op3: db "3 - Multiplication", 0
- op4: db "4 - Division (Broken :P)", 0
- op5: db "Enter 1, 2, 3, Or 4:", 0
- exit: db "Press Any Key To Exit", 0
- answer: db "The Answer Is:", 0
- dividingAns: db "The Remainder Is:", 0
- mOp1: db "Enter First Number:", 0
- mOp2: db "Enter Second Number:", 0
- decimals: dw "0123456789ABCDEF"
Advertisement
Add Comment
Please, Sign In to add comment