Skip to content

Instantly share code, notes, and snippets.

@jabertuhin
Created March 19, 2019 06:57
Show Gist options
  • Save jabertuhin/ec1e99d17df991b53e89068235bca3da to your computer and use it in GitHub Desktop.
Save jabertuhin/ec1e99d17df991b53e89068235bca3da to your computer and use it in GitHub Desktop.
Simple calculator using assembly language. Can solve these kind of equations - 1. (16/4) + (5*6) + 4 +-2 2. (9*4)/6 3. 4 + 2 + (58*2)/5
include 'emu8086.inc'
.model small
.stack 100h
.data
;declare variables
num1 dw 0
num2 dw 0
.code
mov ax,@data
mov ds,ax
main proc
mov bx,0
mov cx,0
mov dx,0
;taking input
input:
mov ah,1
int 21h
cmp al,13
je jump_there ; After Pressing "ENTER" the calculation will begin
cmp al,'+' ;comparing each character
je pushing
cmp al,'-'
je pushing
cmp al,'*'
je pushing
cmp al,'/'
je pushing
cmp al,'('
je brack1
cmp al,')'
je pushing
cmp al,'0'
je zero
cmp al,'1'
je one
cmp al,'2'
je two
cmp al,'3'
je three
cmp al,'4'
je four
cmp al,'5'
je five
cmp al,'6'
je six
cmp al,'7'
je seven
cmp al,'8'
je eight
cmp al,'9'
je nine
pushing:
pop cx
cmp cx,297 ;comparing the last element in the stack . If it's ')'
je smblPush
push cx
push bx ;the real number will be pushed in the stack
mov ax,al
push ax
mov bx,0
jmp input ;take next input character
zero:
mov ax,bx
mov bl,10
mul bl
add ax,0
mov bx,ax
mov ax,0
jmp input
one:
mov ax,bx
mov bl,10
mul bl
add ax,1
mov bx,ax
mov ax,0
jmp input
two:
mov ax,0
mov ax,bx
mov cl,10
mul cl
add ax,2
mov bx,ax
;mov ax,0
jmp input
three:
mov ax,bx
mov bl,10
mul bl
add ax,3
mov bx,ax
mov ax,0
jmp input
four:
mov ax,bx
mov bl,10
mul bl
add ax,4
mov bx,ax
mov ax,0
jmp input
five:
mov ax,bx
mov bl,10
mul bl
add ax,5
mov bx,ax
jmp input
six:
mov ax,bx
mov bl,10
mul bl
add ax,6
mov bx,ax
mov ax,0
jmp input
seven:
mov ax,bx
mov bl,10
mul bl
add ax,7
mov bx,ax
mov ax,0
jmp input
eight:
mov ax,bx
mov bl,10
mul bl
add ax,8
mov bx,ax
mov ax,0
jmp input
nine:
mov ax,bx
mov bl,10
mul bl
add ax,9
mov bx,ax
mov ax,0
jmp input
brack1: ;this method will only push the symbol
mov ax,al
push ax
jmp input
smblPush:
push cx
mov ax,al
push ax
mov bx,0
jmp input
;end input method
;handling the last number here
jump_there:
pop ax
cmp ax,297
je nai
push ax
push bx
mov bx,0
;next line
mov dl,10
mov ah,2
int 21h
mov dl,13
mov ah,2
int 21h
jmp calculation
;next line
;if the last elemnt of the stack is ')' then no number will be pushed
nai:
push ax
;calculating the equation
calculation:
cmp sp,0FEh
je finish
pop ax
cmp ax,299
je plus
cmp ax,301
je minus
cmp ax,297
je bracketCal
cmp ax,298
je multi
cmp ax,303
je divide
mov num1,ax
jmp calculation
;Printing Stack's element
; print_stack:
; ;next line
; mov dl,10
; mov ah,2
; int 21h
;
; mov dl,13
; mov ah,2
; int 21h
; ;next line
; pop ax
;
; cmp ax,299
; je plus
;
; cmp ax,301
; je minus
;
; cmp ax,298
; je multi
;
; cmp ax,303
; je divide
;
; call print_num
; mov ax,0
; cmp sp,100h
; je finish
;
; jmp print_stack
;end of print_stack method
plus:
pop bx
cmp bx,297
je bracketPlus
add bx,num1
push bx
jmp calculation
minus:
;print "-"
;jmp print_stack
pop bx
cmp bx,297
je bracketMinus
sub bx,num1
push bx
jmp calculation
multi:
;print "*"
;jmp print_stack
pop bx
cmp bx,297
je bracketMulti
mov ax,num1
mul bl
push ax
jmp calculation
divide:
;print "/"
;jmp print_stack
pop bx
cmp bx,297
je bracketDivide
mov ax,bx
mov bl,num1
div bl
mov num1,al
mov ax,0
mov ax,num1
push ax
jmp calculation
bracketCal:
pop bx
pop ax
cmp ax,299
je plus1
cmp ax,301
je minus1
cmp ax,298
je multi1
cmp ax,303
je divide1
plus1:
pop dx
add bx,dx
pop dx ; Don't forget This -> '('
push bx
jmp khotom
minus1:
pop dx
sub dx,bx
pop bx ; Don't forget This -> '('
push dx
jmp khotom
multi1:
pop dx
mov ax,0
mov ax,dx
mul bx
pop dx ; Don't forget This -> '('
push ax
mov ax,0
jmp khotom
divide1:
pop dx
mov ax,dx
;mov bl,num1
div bl
pop dx ; Don't forget This -> '('
mov num2,al
mov ax,0
mov ax,num2
push ax
khotom:
jmp calculation
bracketPlus: ;came from plus procedure
pop bx
pop ax
cmp ax,299
je plus2
cmp ax,301
je minus2
cmp ax,298
je multi2
cmp ax,303
je divide2
plus2:
pop dx
add bx,dx
pop dx ; Don't forget This -> '('
push bx
jmp khotom2
minus2:
pop dx
sub dx,bx
pop bx ; Don't forget This -> '('
push dx
jmp khotom2
multi2:
pop dx
mov ax,0
mov ax,dx
mul bx
pop dx ; Don't forget This -> '('
push ax
mov ax,0
jmp khotom2
divide2:
pop dx
mov ax,dx
;mov bl,num1
div bl
pop dx ; Don't forget This -> '('
mov num2,al
mov ax,0
mov ax,num2
push ax
khotom2:
pop bx
add bx,num1
push bx
jmp calculation
;
bracketMinus:
pop bx
pop ax
cmp ax,299
je plus3
cmp ax,301
je minus3
cmp ax,298
je multi3
cmp ax,303
je divide3
plus3:
pop dx
add bx,dx
pop dx ; Don't forget This -> '('
push bx
jmp khotom3
minus3:
pop dx
sub dx,bx
pop bx ; Don't forget This -> '('
push dx
jmp khotom3
multi3:
pop dx
mov ax,0
mov ax,dx
mul bx
pop dx ; Don't forget This -> '('
push ax
mov ax,0
jmp khotom3
divide3:
pop dx
mov ax,dx
;mov bl,num1
div bl
pop dx ; Don't forget This -> '('
mov num2,al
mov ax,0
mov ax,num2
push ax
khotom3:
pop bx
sub bx,num1
push bx
jmp calculation
bracketMulti:
pop bx
pop ax
cmp ax,299
je plus4
cmp ax,301
je minus4
cmp ax,298
je multi4
cmp ax,303
je divide4
plus4:
pop dx
add bx,dx
pop dx ; Don't forget This -> '('
push bx
jmp khotom4
minus4:
pop dx
sub dx,bx
pop bx ; Don't forget This -> '('
push dx
jmp khotom4
multi4:
pop dx
mov ax,0
mov ax,dx
mul bx
pop dx ; Don't forget This -> '('
push ax
mov ax,0
jmp khotom4
divide4:
pop dx
mov ax,dx
;mov bl,num1
div bl
pop dx ; Don't forget This -> '('
mov num2,al
mov ax,0
mov ax,num2
push ax
khotom4:
pop bx
mov ax,num1
mul bl
push ax
jmp calculation
bracketDivide:
pop bx
pop ax
cmp ax,299
je plus5
cmp ax,301
je minus5
cmp ax,298
je multi5
cmp ax,303
je divide5
plus5:
pop dx
add bx,dx
pop dx ; Don't forget This -> '('
push bx
jmp khotom5
minus5:
pop dx
sub dx,bx
pop bx ; Don't forget This -> '('
push dx
jmp khotom5
multi5:
pop dx
mov ax,0
mov ax,dx
mul bx
pop dx ; Don't forget This -> '('
push ax
mov ax,0
jmp khotom5
divide5:
pop dx
mov ax,dx
;mov bl,num1
div bl
pop dx ; Don't forget This -> '('
mov num2,al
mov ax,0
mov ax,num2
push ax
khotom5:
pop bx
mov ax,bx
mov bl,num1
div bl
mov num1,al
mov ax,0
mov ax,num1
push ax
jmp calculation
finish:
mov dl,10
mov ah,2
int 21h
mov dl,13
mov ah,2
int 21h
print "Answer : "
pop ax
call print_num
mov dl,10
mov ah,2
int 21h
mov dl,13
mov ah,2
int 21h
print "DONE"
mov ah,4ch
int 21h
endp main
;body of fuction/procedure
;bracket_do:
; mov bx,0
; mov cx,0
; mov dx,0
;
; pop bx
;
; pop ax
;
; cmp ax,299
; je plus1
;
; cmp ax,301
; je minus1
;
;
; cmp ax,298
; je multi1
;
; cmp ax,303
; je divide1
;
;
; plus1:
; ;print "+"
; ;jmp print_stack
; pop dx
;
; add bx,dx
;
; pop dx ; Don't forget This -> '('
;
; push bx
;
; jmp khotom
;
;
; minus1:
; ;print "-"
; ;jmp print_stack
;
; pop dx
;
; sub dx,bx
;
; pop bx ; Don't forget This -> '('
;
; push dx
;
; jmp khotom
;
;
; multi1:
;; print "*"
;; jmp print_stack
;
; pop dx
;
; mov ax,dx
;
; mul bl
;
; pop dx ; Don't forget This -> '('
;
; push ax
;
; jmp khotom
;
; divide1:
;; print "/"
;; jmp print_stack
;
;
; pop dx
;
; mov ax,dx
; ;mov bl,num1
;
;
; div bx
;
; pop dx ; Don't forget This -> '('
;
; mov num2,al
; mov ax,0
; mov ax,num2
; push ax
;
;
; khotom:
;
;
; ret
DEFINE_SCAN_NUM
DEFINE_PRINT_STRING
DEFINE_PRINT_NUM
DEFINE_PRINT_NUM_UNS
DEFINE_PTHIS
Calculator_Lab_Project.asm
Displaying Calculator_Lab_Project.asm.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment