+++ /dev/null
-;
-; 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
;
-; Christian Krueger, 24-May-2018
+; Ullrich von Bassewitz, 07.08.1998
;
; CC65 runtime: division for signed ints
;
; 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
+
;
-; Christian Krueger, 24-May-2018
+; Ullrich von Bassewitz, 07.08.1998
;
; CC65 runtime: modulo operation for signed ints
;
; 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
+
+; 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 absvaludiv16
- ldx sreg+1 ; Remainder to return registers
- lda sreg
- ldy tmp1 ; Fetch indicator
- bmi negate
- rts
-negate: jmp negax
--- /dev/null
+;
+; 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