home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / cpm / utils / asmutl / asmlib.lbr / SMATH.AZM / SMATH.ASM
Encoding:
Assembly Source File  |  1991-06-25  |  1.4 KB  |  53 lines

  1. ;----------------------------------------------------------------
  2. ;         This is a module in the ASMLIB library.
  3. ;
  4. ; This module is responsible for signed arithmetic. A signed
  5. ; number is represented in 16 bits as 2's complement so that
  6. ; numbers of up to +/- 32k are possible. Supported operations
  7. ; are....
  8. ;
  9. ; sgnadd    HL = DE + HL    Signed add.
  10. ; sgnsub    HL = DE - HL    Signed subtract.
  11. ; comp2s    HL = 2'complement of DE
  12. ;
  13. ;            Written        R.C.H.        19/9/83
  14. ;            Last Update    R.C.H.        19/9/83
  15. ;----------------------------------------------------------------
  16. ;
  17.     name    'sgnmath'
  18.     public    sgnadd,sgnsub,comp2s
  19.     maclib    z80
  20. ;
  21. sgnadd:
  22.     dad    d            ; HL = HL + DE
  23.     ret
  24. ;
  25. ; Subtraction is the SAME as adding the twos complement so we simply take
  26. ; the twos complement of HL then jump to the adder.
  27. ;
  28. sgnsub:
  29.     push    d
  30.     xchg                ; Put HL into DE
  31.     call    comp2s            ; HL becomes 2' DE
  32.     pop    d            ; Restore original DE
  33.     jr    sgnadd
  34. ;
  35. ; The twos complement is performed by complementing all the bits in the
  36. ; input number in DE then adding 1.
  37. ;
  38. comp2s:
  39.     push    psw
  40.     mov    a,d
  41.     cma                ; Complement
  42.     mov    h,a            ; Copy to result
  43.     mov    a,e
  44.     cma
  45.     mov    l,a            ; Copy low vlue to result
  46.     inx    h            ; Bump to make 2's complement
  47.     pop    psw            ; Restore the only affected register
  48.     ret
  49.  
  50.     end
  51.  
  52.  
  53.