;;Term Project Konyk Serhiy .MODEL SMALL ;.586 EXTRN GetDec:NEAR EXTRN PutDec:NEAR EXTRN PutHex:NEAR INCLUDE PCMac.INC .STACK 100h .DATA A DW ? Msg1 DB ' CALCULATOR ', 13,10, '$' Msg2 DB 'Choose an operation, (or # to enter number): ', '$' Msg3 DB 'No such op. (? for help)',13,10,'$' Prompt1 DB 10,13,'Enter a number: ','$' Helpmsg DB 10, 13, '#-to enter two numbers' DB 10, 13, '$-to display a stack' DB 10, 13, '+-to add two numbers' DB 10, 13, '--to subtract two numbers' DB 10, 13, '/-to divide two numbers' DB 10, 13, '*-to multiply two numbers' DB 10, 13, '%-to divide with remaider' DB 10, 13, 'r-to squareroot thenumber' DB 10, 13, 'h-to convert a number to hexidecimal' DB 10, 13, '!-factorial' DB 10, 13, 'f-recurcive factorial' DB 10, 13, '>-to determine which number is bigger' DB 10, 13, '<-to determine which number is smaller' DB 10,13,'$' Newline DB 10,13,'$' .CODE CALC PROC mov ax, @data mov ds, ax _PutStr Msg1 START: _PutStr Newline _PutStr Msg2 _GetCh push ax _PutStr Newline pop cx cmp cl,'#' je DOENTER cmp cl, '?' je DOMENU cmp cl,'+' jne NOTADD pop AX pop BX call DOADDI jmp DONE NOTADD: cmp cl,':' je DODISPLAY cmp cl,'-' je DOSUB cmp cl,'*' je DOMUL cmp cl,'/' je DODIV cmp cl,'r' je DOSQR cmp cl,'!' je DOFAC cmp cl,'f' je DOREC cmp cl,'%' je DOREM cmp cl,'h' je DOHEX cmp cl,'<' je DOSMALL cmp cl,'>' je DOBIG cmp cl,'q' je DOQUIT _PutStr Msg3 jmp start DOMENU: call HELP jmp START DODISPLAY: call DODISPL jmp START DOENTER: call ENTER jmp DONE DOSUB: pop BX pop AX call DOSUBI jmp DONE DOMUL: pop BX pop AX call DOMULI jmp DONE DODIV: pop AX pop BX call DODIVI jmp DONE DOHEX: pop AX call DOHEXI jmp DONE DOREM: pop AX pop BX call DOREMI jmp DONE DOSQR: pop AX call DOSQRI jmp DONE DOFAC: pop AX call DOFAC1 jmp DONE DOREC: pop AX call DOFAC2 jmp DONE DOSMALL: pop AX pop BX call DOMIN jmp DONE DOBIG: pop AX pop BX call DOMAX jmp DONE DONE: push AX call PutDec ;;Output the number. _PutStr Newline jmp Start DOQUIT: mov al, 0 mov ah, 4ch int 21h CALC ENDP ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;SUBPROGRAMS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; HELP PROC _PutStr Helpmsg ;;Outputs the list of operations ret HELP ENDP ENTER PROC _PutStr Prompt1 ;;Receives first number call GetDec ret ENTER ENDP DOADDI PROC add AX,BX ;;Add ret DOADDI ENDP DOSUBI PROC sub AX,BX ;;Subtract ret DOSUBI ENDP DOMULI PROC imul BX ;;Multiply ret DOMULI ENDP DODIVI PROC cwd idiv BX ;;Divede ret DODIVI ENDP DOREMI PROC cwd idiv BX ;;Divede with a remainder mov AX,DX ret DOREMI ENDP DOHEXI PROC call PutHex ;;Conver to a hexidecimal ret DOHEXI ENDP DODISPL PROC REPEAT: pop AX call PutDec _PutStr Newline ;;Display the stack jmp REPEAT ret DODISPL ENDP DOFAC1 PROC mov CX,AX mov AX,1 cmp CX,1 jle FINISH ;;Regular Factorial NEXT: mul CX dec CX jg NEXT FINISH: ret DOFAC1 ENDP DOFAC2 PROC cmp AX,1 jg GO mov AX,1 ret GO: ;;Recurcive Factorial push AX dec AX call DOFAC2 pop CX mul CX ret DOFAC2 ENDP DOMAX PROC cmp AX,BX jge DO1 ;;Bigger Number DO1: ret DOMAX ENDP DOMIN PROC cmp AX,BX ;;Smaller Number jle DON2 mov AX,BX DON2: ret DOMIN ENDP DOSQRI PROC mov dx,0 mov ax,bx mov bx,1 loop_start: div bx add ax,bx ror ax,1 xchg ax,bx mov A,ax ;;Square root sub ax,bx jnc positive_diff neg ax positive_diff: mov dx,0 cmp ax,3 jnb loop_start mov ax,A ret DOSQRI ENDP END CALC