Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; multi-segment executable file template.
- data segment
- menu db "Please enter your Choice",0ah,0dh,"Add ->'+'",0ah,0dh,"Sub ->'-'",0ah,0dh,"Mul ->'*'",0ah,0dh,"div ->'/'",0ah,0dh,"Mod ->'%'",0ah,0dh,"Pow ->'^'",0ah,0dh,"Exit ->'x'",0ah,0dh,"$"
- menuLength dw menuLength - menu
- userSellection db ?
- tempNum db 3 dup (?)
- tempNumLength dw tempNumLength - tempNum
- firstNumMSG db 0ah,0dh,"Please enter the First Number: $"
- firstNumMSGLen dw firstNumMSGlen - firstNumMSG
- secendNumMSG db 0ah,0dh,"Please enter the Secend Number: $"
- secendNumMSGLen dw firstNumMSGlen - firstNumMSG
- resultMsg db 0ah,0dh,"Result: $"
- resultMsgLen dw resultMsgLen - resultMsg
- firstNum db ?
- secendNum db ?
- result db ?
- ends
- stack segment
- dw 128 dup(0)
- ends
- code segment
- start:
- mov ax, data
- mov ds, ax
- mov es, ax
- restartCalc:
- mov result, 0
- mov userSellection, 0
- mov firstNum, 0
- mov secendNum, 0
- ; print menu -----
- mov dx, offset (menu)
- mov ah, 9
- int 21h
- ;input from user ----
- mov ah, 1
- int 21h
- mov userSellection, al
- cmp al, 'x' ; end and not get 2 more numbers if option is x
- je end
- ;input for numbers - first num
- mov bp, 2
- mov si, offset(firstNum)
- ;print first request for input
- mov dx, offset (firstNumMSG)
- mov ah, 9
- int 21h
- num2:
- mov bx, 0
- mov cx, tempNumLength ;set cx for max len of number
- dec bp ; flag for current number input(2 for the first number, 1 for the secend number)
- NextNum:
- mov ah, 1
- int 21h
- cmp al, 13
- je compNum
- mov tempNum[bx], al
- inc bx
- loop NextNum
- compNum: ;turning the digits in temp to a number stored in cuurnt input state sellected by dh
- mov cx, bx
- mov bx, 0
- mov dl, 10
- nextDig:
- mov ah,0
- mov al, [si]
- mul dl
- mov [si],al
- sub tempnum[bx] , 48
- mov al, tempnum[bx]
- add [si], al
- inc bx
- loop nextDig
- cmp bp, 0
- je menuSellection:
- mov si, offset(secendNum);moving to the secend num
- ;print secend request for input
- mov dx, offset (secendNumMSG)
- mov ah, 9
- int 21h
- jmp num2
- menuSellection: ;checks what option was selected, if option not valid returns to start
- mov al,userSellection
- cmp al, '+'
- je addition
- cmp al, '-'
- je substruction
- cmp al, '*'
- je multiplication
- cmp al, '/'
- je divide
- cmp al, '%'
- je mod
- cmp al, '^'
- je pow
- jmp restartCalc
- ;calcules for each option
- addition:
- mov al, firstNum
- add al, secendNum
- mov result, al
- jmp showResult
- substruction:
- mov al, FirstNum
- sub al, secendNum
- mov result,al
- jmp showResult
- multiplication:
- mov al, firstNum
- mov ah,0
- mul secendNum
- mov result, al
- jmp showResult
- divide:
- mov al, firstNum
- mov ah, 0
- div secendNum
- mov result,al
- jmp showResult
- mod:
- mov al, firstNum
- mov ah, 0
- div secendNum
- mov result,ah
- jmp showResult
- pow:
- mov cl, secendNum
- mov ch, 0
- mov al, firstNum
- mov ah, 0
- dec cl
- nextPow:
- mul firstNum
- loop nextPow
- mov result, al
- jmp showResult
- ;prints the result in the correct length
- showResult:
- mov dx, offset (resultMsg)
- mov ah, 9
- int 21h
- mov al, result
- mov ah, 0
- mov dl,10
- div dl
- add ah,48
- mov tempNum[2],ah
- mov ah,0
- div dl
- add ah,48
- add al,48
- mov tempNum[1],ah
- mov tempNum[0],al
- mov ah,result ;finding length of result
- cmp ah, 10
- jb oneDigit
- cmp Ah, 100
- jb twoDigit
- jmp threeDigit
- oneDigit: ;printing setting set accordingly
- mov cx, 1
- mov bx, 2
- jmp printNum
- twoDigit:
- mov cx, 2
- mov bx, 1
- jmp printNum
- threeDigit:
- mov cx,3
- mov bx,0
- jmp printNum
- printNum: ;printing acoording to length.
- mov ah,2
- mov dl,tempNum[bx]
- int 21h
- inc bx
- loop printNum
- mov cx,4
- printSpaces:;prints spaces beetween runs of calculator
- mov ah, 2
- mov dl, 13
- int 21h
- mov ah, 2
- mov dl, 10
- int 21h
- loop printSpaces
- jmp restartCalc
- end:
- mov ax, 4c00h ; exit to operating system.
- int 21h
- ends
- end start
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement