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