From fe027ce916dae7f97c64c20478880b328b3fe72e Mon Sep 17 00:00:00 2001 From: cuz Date: Wed, 17 Apr 2002 18:55:21 +0000 Subject: [PATCH] Fixed wrong compares (new code by Piotr Fusik) git-svn-id: svn://svn.cc65.org/cc65/trunk@1243 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- libsrc/runtime/icmp.s | 87 +++++++++++++++++----------------- libsrc/runtime/lcmp.s | 107 ++++++++++++++++++++++-------------------- 2 files changed, 100 insertions(+), 94 deletions(-) diff --git a/libsrc/runtime/icmp.s b/libsrc/runtime/icmp.s index 43e1b2c61..acd5003ab 100644 --- a/libsrc/runtime/icmp.s +++ b/libsrc/runtime/icmp.s @@ -1,43 +1,44 @@ -; -; Ullrich von Bassewitz, 10.12.1998 -; -; Integer compare function - used by the compare operators -; - - .export tosicmp - .importzp sp, sreg - - -tosicmp: - sta sreg - stx sreg+1 ; Save ax - - ldy #$01 - lda (sp),y ; Get high byte - tax - dey - lda (sp),y ; Get low byte - -; Inline incsp2 for better performance - - inc sp ; 5 - bne @L1 ; 3 - inc sp+1 ; (5) -@L1: inc sp ; 5 - bne @L2 ; 3 - inc sp+1 ; (5) - -; Do the compare. - -@L2: cpx sreg+1 ; Compare high byte - bne @L3 - cmp sreg ; Compare low byte - beq @L3 - bcs @L4 - lda #$FF ; Set the N flag -@L3: rts - -@L4: lda #$01 ; Clear the N flag - rts - - +; +; Piotr Fusik, 15.04.2002 +; originally by Ullrich von Bassewitz +; +; Integer compare function - used by the compare operators +; + + .export tosicmp + .importzp sp, sreg + + +tosicmp: + sta sreg + stx sreg+1 ; Save ax + + ldy #$00 + lda (sp),y ; Get low byte + tax + inc sp ; 5 + bne @L1 ; 3 + inc sp+1 ; (5) +@L1: + lda (sp),y ; Get high byte + inc sp ; 5 + bne @L2 ; 3 + inc sp+1 ; (5) + +; Do the compare. + +@L2: sec + sbc sreg+1 ; Compare high byte + bne @L4 + cpx sreg ; Compare low byte + beq @L3 + adc #$FF ; If the C flag is set then clear the N flag + ora #$01 ; else set the N flag +@L3: rts + +@L4: bvc @L3 + eor #$FF ; Fix the N flag if overflow + ora #$01 ; Clear the Z flag + rts + + diff --git a/libsrc/runtime/lcmp.s b/libsrc/runtime/lcmp.s index c9368d525..47394398e 100644 --- a/libsrc/runtime/lcmp.s +++ b/libsrc/runtime/lcmp.s @@ -1,51 +1,56 @@ -; -; Ullrich von Bassewitz, 10.12.1998 -; -; Long int compare function - used by the compare operators -; - - .export toslcmp - .import incsp4 - .importzp sp, sreg, ptr1 - - -toslcmp: - sta ptr1 - stx ptr1+1 ; EAX now in sreg:ptr1 - - ldy #$03 - lda (sp),y - cmp sreg+1 - bne L4 - - dey - lda (sp),y - cmp sreg - bne L1 - - dey - lda (sp),y - cmp ptr1+1 - bne L1 - - dey - lda (sp),y - cmp ptr1 - -L1: php ; Save flags - jsr incsp4 ; Drop TOS - plp ; Restore the flags - beq L2 - bcs L3 - lda #$FF ; Set the N flag -L2: rts - -L3: lda #$01 ; Clear the N flag - rts - -L4: php ; Save flags - jsr incsp4 ; Drop TOS - plp ; Restore flags - rts - - +; +; Piotr Fusik, 15.04.2002 +; originally by Ullrich von Bassewitz +; +; Long int compare function - used by the compare operators +; + + .export toslcmp + .import incsp4 + .importzp sp, sreg, ptr1 + + +toslcmp: + sta ptr1 + stx ptr1+1 ; EAX now in sreg:ptr1 + + ldy #$03 + lda (sp),y + sec + sbc sreg+1 + bne L4 + + dey + lda (sp),y + cmp sreg + bne L1 + + dey + lda (sp),y + cmp ptr1+1 + bne L1 + + dey + lda (sp),y + cmp ptr1 + +L1: php ; Save flags + jsr incsp4 ; Drop TOS + plp ; Restore the flags + beq L2 + bcs L3 + lda #$FF ; Set the N flag +L2: rts + +L3: lda #$01 ; Clear the N flag + rts + +L4: bvc L5 + eor #$FF ; Fix the N flag if overflow + ora #$01 ; Clear the Z flag +L5: php ; Save flags + jsr incsp4 ; Drop TOS + plp ; Restore flags + rts + + -- 2.39.5