home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / ASM / LCFUNCS.ZIP / CALC.ASM next >
Encoding:
Assembly Source File  |  1987-03-21  |  4.9 KB  |  173 lines

  1.        TITLE     CALC      Calculator program using Lattice C library
  2.        page      60,132
  3.  
  4.        include   lattice.mac
  5.        PSEG
  6.  
  7. _MAIN   proc     near              ; you MUST have a procedure _MAIN !!!
  8.  
  9.         _fopen   fname,imode,fp1   ; Open input file    (console)
  10.         _fopen   fname,omode,fp2   ; Open output file   (console)
  11.  
  12. mloop:  _printf  fp2,prompt        ; Print prompt
  13.         mov      si,offset arg1x   ; point to  string
  14.         mov      bx,offset op1     ; operator
  15.         call     getnum            ; read ascii string till break
  16.  
  17.         cmp      ds:byte ptr op1,'q' ; if sQuare or Arctan then don't
  18.         jz       skip1               ; read second number.
  19.         cmp      ds:byte ptr op1,'a'
  20.         jz       skip1
  21.  
  22.         mov      si,offset arg2x   ; point to string, second number
  23.         mov      bx,offset op2
  24.         call     getnum
  25. ;
  26. ; Check to see if first argument is Null, and if it is, use
  27. ; the last result as the first argument, calculator-style.
  28. ;
  29. skip1:  cmp      ds:byte ptr arg1x,0  ; first argument is null
  30.         jz       skip
  31.         _sscanf  arg1x,Dfmt,num1      ; Convert string to double
  32.  
  33. skip:   cmp      ds:byte ptr arg2x,0  ; Second argument is null
  34.         jz       skip2
  35.         _sscanf  arg2x,Dfmt,num2      ; Convert string to double
  36.  
  37. skip2:  mov      al,ds:byte ptr op1   ; What operation to perform?
  38.  
  39.         cmp      al,'+'               ; Add
  40.         jnz      c0
  41.         jmp      plus
  42.  
  43. c0:     cmp      al,'-'               ; Subtract
  44.         jnz      c1
  45.         jmp      minus
  46.  
  47. c1:     cmp      al,'/'               ; Divide
  48.         jnz      c2
  49.         jmp      divide
  50.  
  51. c2:     cmp      al,'*'               ; Multiply
  52.         jnz      c2a
  53.         jmp       mult
  54.  
  55. c2a:    cmp      al,'q'               ; Square root
  56.         jnz      c3
  57.         jmp      root
  58.  
  59. c3:     cmp      al,'^'               ; power
  60.         jnz      c4
  61.         jmp      power
  62.  
  63. c4:     cmp      al,'a'               ; ArcTangent
  64.         jz       atn
  65.         jmp      equals
  66.  
  67. atn:    _atan    num1,num1
  68.         jmp      equals
  69.  
  70. root:   comp     num1,zero           ; square root, Must be >= 0.0
  71.         jge      okroot
  72.         _printf  fp2, error          ; can't get root of negative number!
  73.         jmp      mloop
  74.  
  75. okroot: _sqrt    num1,num1
  76.         jmp      equals
  77.  
  78. power:  _pow     num1,num2,num1
  79.         jmp      equals
  80.  
  81. mult:   _mul     num1,num2,num1
  82.         jmp      equals
  83.  
  84. plus:   _add     num1,num2,num1
  85.         jmp      equals
  86.  
  87. minus:  _sub     num1,num2,num1
  88.         jmp      equals
  89.  
  90. divide: comp    num2,zero            ; Can't divide by zero
  91.         jg      okdiv                ; second arg is > 0.0
  92.         _printf fp2, error           ; second arg = 0, ERROR.
  93.         jmp     mloop
  94. okdiv:  _div    num1,num2,num1       ; Should do error checking here.
  95.  
  96. equals: _printd  fp2,outfmt,num1      ; display result.
  97.  
  98.         jmp      mloop
  99.  
  100. abort:  ret
  101.  
  102. _MAIN  endp
  103.  
  104. ; GETNUM - Read an ascii number from console, read til {+ - / * = }
  105. ;          SI = Address of string
  106. ;          BX = Address of operator
  107.  
  108. getnum  proc     near
  109.         push     bx
  110. get1:   push     si
  111.         mov      ah,1
  112.         int      21h
  113.         pop      si
  114.         cmp      al,'+'       ; Check for arithmetic operators
  115.         jz       getx
  116.         cmp      al,'-'
  117.         jz       getx
  118.         cmp      al,'*'
  119.         jz       getx
  120.         cmp      al,'/'
  121.         jz       getx
  122.         cmp      al,'='
  123.         jz       getx
  124.         cmp      al,'q'
  125.         jz       getx
  126.         cmp      al,'a'
  127.         jz       getx
  128.         cmp      al,'^'
  129.         jz       getx
  130.         cmp      al,'.'       ; Decimal point
  131.         jz       get2
  132.         cmp      al,'0'       ; Must be between 0 and 9
  133.         jb       get1         ; if <0 then ignore
  134.         cmp      al,'9'
  135.         ja       get1         ; if >9 then ignore
  136. get2:   mov      ds:byte ptr [si],al ; store digit
  137.         inc      si
  138.         jmp      get1
  139. getx:   pop      bx
  140.         mov      ds:byte ptr [bx],al  ;Store operator
  141.         mov      ds:byte ptr [si],0
  142.         ret
  143. getnum  endp
  144.  
  145.         ENDPS
  146.  
  147.         DSEG
  148. fp1    dw        ?                  ; file for input
  149. fp2    dw        ?                  ; file for output
  150.  
  151. arg1x  db        20 dup (20h)
  152. op1    db        '+'
  153. arg2x  db        20 dup (20h)
  154. op2    db        '='
  155.  
  156. fname  db        'CON:',0           ; name of console device
  157.  
  158. imode  db        'r',0              ; mode for read-only
  159. omode  db        'w',0              ; mode for write-only
  160.  
  161. Dfmt   db        '%lf',0            ; format for numeric Input
  162. outfmt db        '   %lf',0         ; format for numeric Output
  163.  
  164. prompt db        0Ah,'> ',0
  165. error  db        0AH,'illegal operation ',0
  166.  
  167. num1   dq        ?
  168. num2   dq        ?
  169. num3   dq        ?
  170. zero   dq        0.0e0
  171.        ENDDS                       ; End of data segment
  172.        end
  173.