home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / ASM / BCDASM.ZIP / BCDASM / SRC / BCDCMP.ASM < prev    next >
Encoding:
Assembly Source File  |  1997-06-03  |  1.8 KB  |  64 lines

  1.     title    BCDASM -- Copyright 1997, Morten Elling
  2.     subttl    Compare two packed signed BCD numbers
  3.  
  4.     include    model.inc
  5.     include    modelt.inc
  6.     include    bcd.ash
  7.  
  8.     @CODESEG
  9.  
  10. ;//////////////////////////////////////////////////////////////////////
  11. ;//    Name    bcdCmp
  12. ;//    Desc    Compare two packed signed BCD numbers.
  13. ;//
  14. ;//
  15. ;//    Entry    Passed args
  16. ;//    Exit    Accumulator (and cf,zf,sf flags) set according to the
  17. ;//        (string) comparison: BCD1st - BCD2nd
  18. ;//        Acc = -1:  1st < 2nd
  19. ;//        Acc =  0:  1st = 2nd
  20. ;//        Acc = +1:  1st > 2nd
  21. ;//
  22. ;//    Note    Unlike bcdSub, this procedure returns the result based
  23. ;//        on a string comparison of the operands.
  24.  
  25. bcdCmp    proc
  26. arg    BCD1st    :dataptr, \    ; Addr of 1st BCD
  27.     BCD2nd    :dataptr, \    ; Addr of 2nd BCD
  28.     BCDsz    :@uint        ; Byte size of each BCD
  29. @uses    ds,es,rsi,rdi,rcx
  30. ;.
  31.     mov   rcx, [BCDsz]
  32.     @LDS  rsi, [BCD1st]
  33.     @LES  rdi, [BCD2nd]
  34.     dec   rcx        ; All but sign byte
  35.     add   rsi, rcx        ; Point to
  36.     add   rdi, rcx        ;   sign bytes
  37.     mov   al, [rsi]        ; Get top byte of 1st BCD
  38.     mov   ah, @ES [rdi]    ;   and of 2nd BCD
  39.     and   rax, 8080h    ; Isolate sign bits
  40.     jz sh @@same_sign    ; Result zero if both positive
  41.     cmp   ah, al        ; Are signs identical?
  42.     jnz sh @@ret_sign    ; No, skip string compare
  43.     xchg  rsi, rdi        ; Yes, swap pointers
  44. @@same_sign:            ;   since both are negative
  45.     dec   rsi        ; Point to byte before sign
  46.     dec   rdi        ;   in 2nd number, too
  47.     std            ; Auto-decrement index reg.s
  48.     repe  cmpsb        ; Compare high-to-low
  49.     cld            ; Clear direction flag
  50.     mov   al, 0        ; Assume [rsi] = [rdi]
  51.     jz sh @@ret        ; If so, exit
  52. @@ret_sign:
  53.     mov   al,-1        ; Assume [rsi] < [rdi]
  54.     jc sh @@ret        ; Guessed right
  55.     mov   al, 1        ; Else must be [rsi] > [rdi]
  56. @@ret:    ife @isUse32        ; Sign-extend in accumulator
  57.     cbw
  58.     else
  59.     movsx rax, al
  60.     endif
  61.     RET            ; Return acc and cf,zf,sf
  62. bcdCmp    endp
  63.  
  64.     END