From dd411efd1c60e83505a74d57d9ef5065b322b9be Mon Sep 17 00:00:00 2001 From: IrgendwerA8 Date: Thu, 24 May 2018 03:55:40 +0200 Subject: [PATCH 1/5] Adapted div & mod for popptr1. --- libsrc/common/abs.s | 16 ----------- libsrc/runtime/absvaludiv.s | 22 +++++++++++++++ libsrc/runtime/div.s | 40 +++++++++++--------------- libsrc/runtime/mod.s | 41 ++++++++++++--------------- libsrc/runtime/mulax3.s | 1 + libsrc/runtime/{neg.s => negabs.s} | 7 ++++- libsrc/runtime/shelp.s | 30 -------------------- libsrc/runtime/udiv.s | 45 +++++++++++++++--------------- libsrc/runtime/umod.s | 12 ++++---- 9 files changed, 92 insertions(+), 122 deletions(-) delete mode 100644 libsrc/common/abs.s create mode 100644 libsrc/runtime/absvaludiv.s rename libsrc/runtime/{neg.s => negabs.s} (59%) delete mode 100644 libsrc/runtime/shelp.s diff --git a/libsrc/common/abs.s b/libsrc/common/abs.s deleted file mode 100644 index 5daa8a14a..000000000 --- a/libsrc/common/abs.s +++ /dev/null @@ -1,16 +0,0 @@ -; -; Ullrich von Bassewitz, 17.06.1998 -; -; int abs (int x); -; - - .export _abs - .import negax - -_abs: cpx #$00 ; test hi byte - bpl L1 - jmp negax ; Negate if negative -L1: rts - - - diff --git a/libsrc/runtime/absvaludiv.s b/libsrc/runtime/absvaludiv.s new file mode 100644 index 000000000..d23b8c00e --- /dev/null +++ b/libsrc/runtime/absvaludiv.s @@ -0,0 +1,22 @@ +; +; Christian Krueger, 23-May-2018 +; +; CC65 runtime: helper call for mod/div with signed ints +; +; When negating values, we will ignore the possibility here, that one of the +; values is $8000, in which case the negate will fail. + + .export absvaludiv + .import _abs, popax, udiv16 + .importzp ptr1, ptr4 + + +absvaludiv: + jsr _abs + sta ptr4 + stx ptr4+1 ; Save right absolute operand + jsr popax + jsr _abs + sta ptr1 + stx ptr1+1 ; Save left absolute operand + jmp udiv16 diff --git a/libsrc/runtime/div.s b/libsrc/runtime/div.s index cdd340272..54da0c7e4 100644 --- a/libsrc/runtime/div.s +++ b/libsrc/runtime/div.s @@ -1,37 +1,29 @@ ; -; Ullrich von Bassewitz, 07.08.1998 +; Christian Krueger, 24-May-2018 ; ; CC65 runtime: division for signed ints ; ; When negating values, we will ignore the possibility here, that one of the -; values if $8000, in which case the negate will fail. +; values is $8000, in which case the negate will fail. .export tosdiva0, tosdivax - .import popsargs, udiv16, negax - .importzp sreg, tmp1, tmp2 + .import absvaludiv, negax + .importzp sp, ptr1, tmp1 tosdiva0: ldx #0 tosdivax: - jsr popsargs ; Get arguments from stack, adjust sign - jsr udiv16 ; Do the division - ldx sreg+1 ; Load high byte of result - -; Adjust the sign of the result. tmp1 contains the high byte of the left -; operand, tmp2 contains the high byte of the right operand. - - lda tmp1 - eor tmp2 - bpl Pos ; Jump if sign of result positive - -; Result is negative - - lda sreg ; Load low byte of result - jmp negax ; Adjust the sign - -; Result is positive - -Pos: lda sreg + pha ; check if high-bytes indicate + txa ; different sign, so that we + ldy #1 ; negate the result after the operation + eor (sp),y ; eor with lhs high byte + sta tmp1 ; save post negation indicator to tmp1 + pla ; back to entry accu + jsr absvaludiv + ldx ptr1+1 + lda ptr1 + ldy tmp1 ; fetch idicator + bmi negate rts - +negate: jmp negax diff --git a/libsrc/runtime/mod.s b/libsrc/runtime/mod.s index c4331ed85..b3a818fda 100644 --- a/libsrc/runtime/mod.s +++ b/libsrc/runtime/mod.s @@ -1,37 +1,32 @@ ; -; Ullrich von Bassewitz, 07.08.1998 +; Christian Krueger, 24-May-2018 ; ; CC65 runtime: modulo operation for signed ints ; ; When negating values, we will ignore the possibility here, that one of the -; values if $8000, in which case the negate will fail. +; values is $8000, in which case the negate will fail. .export tosmoda0, tosmodax - .import popsargs, udiv16, negax - .importzp ptr1, tmp1 + .import absvaludiv, negax + .importzp sp, sreg, tmp1 tosmoda0: ldx #0 tosmodax: - jsr popsargs ; Get arguments from stack, adjust sign - jsr udiv16 ; Do the division - lda ptr1 ; Load low byte of result - ldx ptr1+1 ; Load high byte of result -; Adjust the sign of the result. tmp1 contains the high byte of the left -; operand, tmp2 contains the high byte of the right operand. The sign of -; the result of the modulo operation is the same as that of the left -; operand - - bit tmp1 - bpl Pos ; Jump if sign of result positive - -; Result is negative - - jmp negax ; Adjust the sign - -; Result is positive - -Pos: rts +; Prepare adjustment of the sign of the result. The sign of the result of the +; modulo operation is the same as that of the left operand. + pha + ldy #1 ; prepare lhs operant hi-byte fetch + lda (sp),y + sta tmp1 ; save post negation indicator to tmp1 + pla ; back to entry accu + jsr absvaludiv + ldx sreg+1 ; remainder to return registers + lda sreg + ldy tmp1 ; fetch idicator + bmi negate + rts +negate: jmp negax diff --git a/libsrc/runtime/mulax3.s b/libsrc/runtime/mulax3.s index 472bc60ec..513ba723e 100644 --- a/libsrc/runtime/mulax3.s +++ b/libsrc/runtime/mulax3.s @@ -3,6 +3,7 @@ ; ; CC65 runtime: Multiply the primary register by 3 ; +; Don't touch the Y-register here, the optimizer relies on it! .export mulax3 .importzp ptr1 diff --git a/libsrc/runtime/neg.s b/libsrc/runtime/negabs.s similarity index 59% rename from libsrc/runtime/neg.s rename to libsrc/runtime/negabs.s index a428fd1e4..5a9ca6e01 100644 --- a/libsrc/runtime/neg.s +++ b/libsrc/runtime/negabs.s @@ -1,11 +1,16 @@ ; ; Ullrich von Bassewitz, 05.08.1998 ; +; int abs (int x); +; and ; CC65 runtime: negation on ints ; .export negax + .export _abs +_abs: cpx #$00 ; test hi byte + bpl L1 ; don't touch if positive negax: clc eor #$FF adc #1 @@ -15,7 +20,7 @@ negax: clc adc #0 tax pla - rts + L1: rts diff --git a/libsrc/runtime/shelp.s b/libsrc/runtime/shelp.s deleted file mode 100644 index b1ebeb798..000000000 --- a/libsrc/runtime/shelp.s +++ /dev/null @@ -1,30 +0,0 @@ -; -; Ullrich von Bassewitz, 07.08.1998 -; -; CC65 runtime: helper stuff for mod/div/mul with signed ints -; - -; When negating values, we will ignore the possibility here, that one of the -; values if $8000, in which case the negate will fail. - - .export popsargs - .import negax, popax - .importzp sreg, tmp1, tmp2, ptr4 - -popsargs: - stx tmp2 ; Remember sign - cpx #0 - bpl L1 - jsr negax ; Negate accumulator -L1: sta ptr4 - stx ptr4+1 ; Save right operand - - jsr popax - stx tmp1 ; Remember sign - cpx #0 - bpl L2 - jsr negax -L2: sta sreg - stx sreg+1 - rts - diff --git a/libsrc/runtime/udiv.s b/libsrc/runtime/udiv.s index 93548d049..4115382c8 100644 --- a/libsrc/runtime/udiv.s +++ b/libsrc/runtime/udiv.s @@ -3,9 +3,10 @@ ; ; CC65 runtime: division for unsigned ints ; +; Don't use tmp1 here, the signed division tunnels data with it! .export tosudiva0, tosudivax, udiv16 - .import popsreg + .import popptr1 .importzp sreg, ptr1, ptr4 @@ -14,50 +15,50 @@ tosudiva0: tosudivax: sta ptr4 stx ptr4+1 ; Save right operand - jsr popsreg ; Get left operand + jsr popptr1 ; Get left operand ; Do the division jsr udiv16 -; Result is in sreg, remainder in ptr1 +; Result is in ptr1, remainder in sreg - lda sreg - ldx sreg+1 + lda ptr1 + ldx ptr1+1 rts ;--------------------------------------------------------------------------- -; 16by16 division. Divide sreg by ptr4. Result is in sreg, remainder in ptr1 +; 16by16 division. Divide ptr1 by ptr4. Result is in ptr1, remainder in sreg ; (see mult-div.s from "The Fridge"). ; This is also the entry point for the signed division udiv16: lda #0 - sta ptr1+1 + sta sreg+1 ldy #16 ldx ptr4+1 beq udiv16by8a -L0: asl sreg - rol sreg+1 - rol a +L0: asl ptr1 rol ptr1+1 + rol a + rol sreg+1 - pha + tax cmp ptr4 - lda ptr1+1 + lda sreg+1 sbc ptr4+1 bcc L1 - sta ptr1+1 - pla + sta sreg+1 + txa sbc ptr4 - pha - inc sreg + tax + inc ptr1 -L1: pla +L1: txa dey bne L0 - sta ptr1 + sta sreg rts @@ -65,18 +66,18 @@ L1: pla ; 16by8 division udiv16by8a: -@L0: asl sreg - rol sreg+1 +@L0: asl ptr1 + rol ptr1+1 rol a bcs @L1 cmp ptr4 bcc @L2 @L1: sbc ptr4 - inc sreg + inc ptr1 @L2: dey bne @L0 - sta ptr1 + sta sreg rts diff --git a/libsrc/runtime/umod.s b/libsrc/runtime/umod.s index 92ebb5f91..5788d569e 100644 --- a/libsrc/runtime/umod.s +++ b/libsrc/runtime/umod.s @@ -5,24 +5,24 @@ ; .export tosumoda0, tosumodax - .import popsreg, udiv16 - .importzp ptr1, ptr4 + .import popptr1, udiv16 + .importzp sreg, ptr4 tosumoda0: ldx #0 tosumodax: sta ptr4 stx ptr4+1 ; Save right operand - jsr popsreg ; Get right operand + jsr popptr1 ; Get right operand ; Do the division jsr udiv16 -; Result is in sreg, remainder in ptr1 +; Result is in ptr1, remainder in sreg - lda ptr1 - ldx ptr1+1 + lda sreg + ldx sreg+1 rts From 9c1cb0801c79e7a87f6b17a667b304624b896e9d Mon Sep 17 00:00:00 2001 From: IrgendwerA8 Date: Thu, 24 May 2018 11:31:43 +0200 Subject: [PATCH 2/5] Changed naming absvaludiv -> absvaludiv16. --- libsrc/runtime/absvaludiv.s | 4 ++-- libsrc/runtime/div.s | 6 +++--- libsrc/runtime/mod.s | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/libsrc/runtime/absvaludiv.s b/libsrc/runtime/absvaludiv.s index d23b8c00e..59c2914b1 100644 --- a/libsrc/runtime/absvaludiv.s +++ b/libsrc/runtime/absvaludiv.s @@ -6,12 +6,12 @@ ; When negating values, we will ignore the possibility here, that one of the ; values is $8000, in which case the negate will fail. - .export absvaludiv + .export absvaludiv16 .import _abs, popax, udiv16 .importzp ptr1, ptr4 -absvaludiv: +absvaludiv16: jsr _abs sta ptr4 stx ptr4+1 ; Save right absolute operand diff --git a/libsrc/runtime/div.s b/libsrc/runtime/div.s index 54da0c7e4..b33d852db 100644 --- a/libsrc/runtime/div.s +++ b/libsrc/runtime/div.s @@ -8,7 +8,7 @@ ; values is $8000, in which case the negate will fail. .export tosdiva0, tosdivax - .import absvaludiv, negax + .import absvaludiv16, negax .importzp sp, ptr1, tmp1 tosdiva0: @@ -20,10 +20,10 @@ tosdivax: eor (sp),y ; eor with lhs high byte sta tmp1 ; save post negation indicator to tmp1 pla ; back to entry accu - jsr absvaludiv + jsr absvaludiv16 ldx ptr1+1 lda ptr1 - ldy tmp1 ; fetch idicator + ldy tmp1 ; fetch indicator bmi negate rts negate: jmp negax diff --git a/libsrc/runtime/mod.s b/libsrc/runtime/mod.s index b3a818fda..eb120377b 100644 --- a/libsrc/runtime/mod.s +++ b/libsrc/runtime/mod.s @@ -8,7 +8,7 @@ ; values is $8000, in which case the negate will fail. .export tosmoda0, tosmodax - .import absvaludiv, negax + .import absvaludiv16, negax .importzp sp, sreg, tmp1 tosmoda0: @@ -23,10 +23,10 @@ tosmodax: lda (sp),y sta tmp1 ; save post negation indicator to tmp1 pla ; back to entry accu - jsr absvaludiv + jsr absvaludiv16 ldx sreg+1 ; remainder to return registers lda sreg - ldy tmp1 ; fetch idicator + ldy tmp1 ; fetch indicator bmi negate rts negate: jmp negax From 71420223bcfed2176edb385dfbd231713b234ca9 Mon Sep 17 00:00:00 2001 From: IrgendwerA8 Date: Thu, 24 May 2018 11:37:17 +0200 Subject: [PATCH 3/5] Changed the name of the file too. --- libsrc/runtime/{absvaludiv.s => absvaludiv16.s} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename libsrc/runtime/{absvaludiv.s => absvaludiv16.s} (100%) diff --git a/libsrc/runtime/absvaludiv.s b/libsrc/runtime/absvaludiv16.s similarity index 100% rename from libsrc/runtime/absvaludiv.s rename to libsrc/runtime/absvaludiv16.s From ba5b580368bc248a1c66ff5026a5d6fb13bac0e1 Mon Sep 17 00:00:00 2001 From: IrgendwerA8 Date: Fri, 25 May 2018 16:10:16 +0200 Subject: [PATCH 4/5] Fixed first letter of comments (should be upper case). --- libsrc/runtime/div.s | 14 +++++++------- libsrc/runtime/mod.s | 10 +++++----- libsrc/runtime/negabs.s | 4 ++-- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/libsrc/runtime/div.s b/libsrc/runtime/div.s index b33d852db..a67799fdf 100644 --- a/libsrc/runtime/div.s +++ b/libsrc/runtime/div.s @@ -14,16 +14,16 @@ tosdiva0: ldx #0 tosdivax: - pha ; check if high-bytes indicate - txa ; different sign, so that we - ldy #1 ; negate the result after the operation - eor (sp),y ; eor with lhs high byte - sta tmp1 ; save post negation indicator to tmp1 - pla ; back to entry accu + pha ; Check if high-bytes indicate + txa ; different sign, so that we have to + ldy #1 ; negate the result after the operation. + eor (sp),y ; Eor with lhs high byte + sta tmp1 ; Save post negation indicator to tmp1 + pla ; Back to entry accu jsr absvaludiv16 ldx ptr1+1 lda ptr1 - ldy tmp1 ; fetch indicator + ldy tmp1 ; Fetch indicator bmi negate rts negate: jmp negax diff --git a/libsrc/runtime/mod.s b/libsrc/runtime/mod.s index eb120377b..0706ba315 100644 --- a/libsrc/runtime/mod.s +++ b/libsrc/runtime/mod.s @@ -19,14 +19,14 @@ tosmodax: ; modulo operation is the same as that of the left operand. pha - ldy #1 ; prepare lhs operant hi-byte fetch + ldy #1 ; Prepare lhs operant hi-byte fetch lda (sp),y - sta tmp1 ; save post negation indicator to tmp1 - pla ; back to entry accu + sta tmp1 ; Save post negation indicator to tmp1 + pla ; Back to entry accu jsr absvaludiv16 - ldx sreg+1 ; remainder to return registers + ldx sreg+1 ; Remainder to return registers lda sreg - ldy tmp1 ; fetch indicator + ldy tmp1 ; Fetch indicator bmi negate rts negate: jmp negax diff --git a/libsrc/runtime/negabs.s b/libsrc/runtime/negabs.s index 5a9ca6e01..ef660f1da 100644 --- a/libsrc/runtime/negabs.s +++ b/libsrc/runtime/negabs.s @@ -9,8 +9,8 @@ .export negax .export _abs -_abs: cpx #$00 ; test hi byte - bpl L1 ; don't touch if positive +_abs: cpx #$00 ; Test hi byte + bpl L1 ; Don't touch if positive negax: clc eor #$FF adc #1 From 8a5d1b967473c9acd07dde67778e2cfc1ac17aa1 Mon Sep 17 00:00:00 2001 From: IrgendwerA8 Date: Fri, 25 May 2018 23:18:26 +0200 Subject: [PATCH 5/5] Reestablished entry state of signed operation and optimized that (again). --- libsrc/runtime/absvaludiv16.s | 22 -------------------- libsrc/runtime/div.s | 38 ++++++++++++++++++++-------------- libsrc/runtime/mod.s | 39 ++++++++++++++++++++--------------- libsrc/runtime/shelp.s | 29 ++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 54 deletions(-) delete mode 100644 libsrc/runtime/absvaludiv16.s create mode 100644 libsrc/runtime/shelp.s diff --git a/libsrc/runtime/absvaludiv16.s b/libsrc/runtime/absvaludiv16.s deleted file mode 100644 index 59c2914b1..000000000 --- a/libsrc/runtime/absvaludiv16.s +++ /dev/null @@ -1,22 +0,0 @@ -; -; Christian Krueger, 23-May-2018 -; -; CC65 runtime: helper call for mod/div with signed ints -; -; When negating values, we will ignore the possibility here, that one of the -; values is $8000, in which case the negate will fail. - - .export absvaludiv16 - .import _abs, popax, udiv16 - .importzp ptr1, ptr4 - - -absvaludiv16: - jsr _abs - sta ptr4 - stx ptr4+1 ; Save right absolute operand - jsr popax - jsr _abs - sta ptr1 - stx ptr1+1 ; Save left absolute operand - jmp udiv16 diff --git a/libsrc/runtime/div.s b/libsrc/runtime/div.s index a67799fdf..e10ebc57d 100644 --- a/libsrc/runtime/div.s +++ b/libsrc/runtime/div.s @@ -1,5 +1,5 @@ ; -; Christian Krueger, 24-May-2018 +; Ullrich von Bassewitz, 07.08.1998 ; ; CC65 runtime: division for signed ints ; @@ -8,22 +8,30 @@ ; values is $8000, in which case the negate will fail. .export tosdiva0, tosdivax - .import absvaludiv16, negax - .importzp sp, ptr1, tmp1 + .import popsargsudiv16, negax + .importzp ptr1, tmp1, tmp2 tosdiva0: ldx #0 tosdivax: - pha ; Check if high-bytes indicate - txa ; different sign, so that we have to - ldy #1 ; negate the result after the operation. - eor (sp),y ; Eor with lhs high byte - sta tmp1 ; Save post negation indicator to tmp1 - pla ; Back to entry accu - jsr absvaludiv16 - ldx ptr1+1 - lda ptr1 - ldy tmp1 ; Fetch indicator - bmi negate + jsr popsargsudiv16 ; Get arguments from stack, adjust sign + ; and do the division + ldx ptr1+1 ; Load high byte of result + +; Adjust the sign of the result. tmp1 contains the high byte of the left +; operand, tmp2 contains the high byte of the right operand. + + lda tmp1 + eor tmp2 + bpl Pos ; Jump if sign of result positive + +; Result is negative + + lda ptr1 ; Load low byte of result + jmp negax ; Adjust the sign + +; Result is positive + +Pos: lda ptr1 ; Load low byte of result rts -negate: jmp negax + diff --git a/libsrc/runtime/mod.s b/libsrc/runtime/mod.s index 0706ba315..58e740575 100644 --- a/libsrc/runtime/mod.s +++ b/libsrc/runtime/mod.s @@ -1,5 +1,5 @@ ; -; Christian Krueger, 24-May-2018 +; Ullrich von Bassewitz, 07.08.1998 ; ; CC65 runtime: modulo operation for signed ints ; @@ -8,25 +8,30 @@ ; values is $8000, in which case the negate will fail. .export tosmoda0, tosmodax - .import absvaludiv16, negax - .importzp sp, sreg, tmp1 + .import popsargsudiv16, negax + .importzp sreg, tmp1 tosmoda0: ldx #0 tosmodax: + jsr popsargsudiv16 ; Get arguments from stack, adjust sign + ; and do the division + lda sreg ; Load low byte of result + ldx sreg+1 ; Load high byte of result -; Prepare adjustment of the sign of the result. The sign of the result of the -; modulo operation is the same as that of the left operand. +; Adjust the sign of the result. tmp1 contains the high byte of the left +; operand, tmp2 contains the high byte of the right operand. The sign of +; the result of the modulo operation is the same as that of the left +; operand + + bit tmp1 + bpl Pos ; Jump if sign of result positive + +; Result is negative + + jmp negax ; Adjust the sign + +; Result is positive + +Pos: rts - pha - ldy #1 ; Prepare lhs operant hi-byte fetch - lda (sp),y - sta tmp1 ; Save post negation indicator to tmp1 - pla ; Back to entry accu - jsr absvaludiv16 - ldx sreg+1 ; Remainder to return registers - lda sreg - ldy tmp1 ; Fetch indicator - bmi negate - rts -negate: jmp negax diff --git a/libsrc/runtime/shelp.s b/libsrc/runtime/shelp.s new file mode 100644 index 000000000..9762dbf44 --- /dev/null +++ b/libsrc/runtime/shelp.s @@ -0,0 +1,29 @@ +; +; Ullrich von Bassewitz, 07.08.1998 +; +; CC65 runtime: helper stuff for mod/div with signed ints +; + +; When negating values, we will ignore the possibility here, that one of the +; values is $8000, in which case the negate will fail. + + .export popsargsudiv16 + .import negax, popax, udiv16 + .importzp tmp1, tmp2, ptr1, ptr4 + +popsargsudiv16: + stx tmp2 ; Remember sign + cpx #0 + bpl L1 + jsr negax ; Negate accumulator +L1: sta ptr4 + stx ptr4+1 ; Save right operand + + jsr popax + stx tmp1 ; Remember sign + cpx #0 + bpl L2 + jsr negax +L2: sta ptr1 + stx ptr1+1 + jmp udiv16 ; Call the division