1
0
mirror of https://github.com/cc65/cc65.git synced 2024-06-07 07:29:33 +00:00

Adapted div & mod for popptr1.

This commit is contained in:
IrgendwerA8 2018-05-24 03:55:40 +02:00
parent 746e3d1454
commit dd411efd1c
9 changed files with 92 additions and 122 deletions

View File

@ -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

View File

@ -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

View File

@ -1,37 +1,29 @@
; ;
; Ullrich von Bassewitz, 07.08.1998 ; Christian Krueger, 24-May-2018
; ;
; CC65 runtime: division for signed ints ; CC65 runtime: division for signed ints
; ;
; When negating values, we will ignore the possibility here, that one of the ; 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 .export tosdiva0, tosdivax
.import popsargs, udiv16, negax .import absvaludiv, negax
.importzp sreg, tmp1, tmp2 .importzp sp, ptr1, tmp1
tosdiva0: tosdiva0:
ldx #0 ldx #0
tosdivax: tosdivax:
jsr popsargs ; Get arguments from stack, adjust sign pha ; check if high-bytes indicate
jsr udiv16 ; Do the division txa ; different sign, so that we
ldx sreg+1 ; Load high byte of result ldy #1 ; negate the result after the operation
eor (sp),y ; eor with lhs high byte
; Adjust the sign of the result. tmp1 contains the high byte of the left sta tmp1 ; save post negation indicator to tmp1
; operand, tmp2 contains the high byte of the right operand. pla ; back to entry accu
jsr absvaludiv
lda tmp1 ldx ptr1+1
eor tmp2 lda ptr1
bpl Pos ; Jump if sign of result positive ldy tmp1 ; fetch idicator
bmi negate
; Result is negative
lda sreg ; Load low byte of result
jmp negax ; Adjust the sign
; Result is positive
Pos: lda sreg
rts rts
negate: jmp negax

View File

@ -1,37 +1,32 @@
; ;
; Ullrich von Bassewitz, 07.08.1998 ; Christian Krueger, 24-May-2018
; ;
; CC65 runtime: modulo operation for signed ints ; CC65 runtime: modulo operation for signed ints
; ;
; When negating values, we will ignore the possibility here, that one of the ; 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 .export tosmoda0, tosmodax
.import popsargs, udiv16, negax .import absvaludiv, negax
.importzp ptr1, tmp1 .importzp sp, sreg, tmp1
tosmoda0: tosmoda0:
ldx #0 ldx #0
tosmodax: 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 ; Prepare adjustment of the sign of the result. The sign of the result of the
; operand, tmp2 contains the high byte of the right operand. The sign of ; modulo operation is the same as that of the left operand.
; 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 absvaludiv
ldx sreg+1 ; remainder to return registers
lda sreg
ldy tmp1 ; fetch idicator
bmi negate
rts
negate: jmp negax

View File

@ -3,6 +3,7 @@
; ;
; CC65 runtime: Multiply the primary register by 3 ; CC65 runtime: Multiply the primary register by 3
; ;
; Don't touch the Y-register here, the optimizer relies on it!
.export mulax3 .export mulax3
.importzp ptr1 .importzp ptr1

View File

@ -1,11 +1,16 @@
; ;
; Ullrich von Bassewitz, 05.08.1998 ; Ullrich von Bassewitz, 05.08.1998
; ;
; int abs (int x);
; and
; CC65 runtime: negation on ints ; CC65 runtime: negation on ints
; ;
.export negax .export negax
.export _abs
_abs: cpx #$00 ; test hi byte
bpl L1 ; don't touch if positive
negax: clc negax: clc
eor #$FF eor #$FF
adc #1 adc #1
@ -15,7 +20,7 @@ negax: clc
adc #0 adc #0
tax tax
pla pla
rts L1: rts

View File

@ -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

View File

@ -3,9 +3,10 @@
; ;
; CC65 runtime: division for unsigned ints ; CC65 runtime: division for unsigned ints
; ;
; Don't use tmp1 here, the signed division tunnels data with it!
.export tosudiva0, tosudivax, udiv16 .export tosudiva0, tosudivax, udiv16
.import popsreg .import popptr1
.importzp sreg, ptr1, ptr4 .importzp sreg, ptr1, ptr4
@ -14,50 +15,50 @@ tosudiva0:
tosudivax: tosudivax:
sta ptr4 sta ptr4
stx ptr4+1 ; Save right operand stx ptr4+1 ; Save right operand
jsr popsreg ; Get left operand jsr popptr1 ; Get left operand
; Do the division ; Do the division
jsr udiv16 jsr udiv16
; Result is in sreg, remainder in ptr1 ; Result is in ptr1, remainder in sreg
lda sreg lda ptr1
ldx sreg+1 ldx ptr1+1
rts 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"). ; (see mult-div.s from "The Fridge").
; This is also the entry point for the signed division ; This is also the entry point for the signed division
udiv16: lda #0 udiv16: lda #0
sta ptr1+1 sta sreg+1
ldy #16 ldy #16
ldx ptr4+1 ldx ptr4+1
beq udiv16by8a beq udiv16by8a
L0: asl sreg L0: asl ptr1
rol sreg+1
rol a
rol ptr1+1 rol ptr1+1
rol a
rol sreg+1
pha tax
cmp ptr4 cmp ptr4
lda ptr1+1 lda sreg+1
sbc ptr4+1 sbc ptr4+1
bcc L1 bcc L1
sta ptr1+1 sta sreg+1
pla txa
sbc ptr4 sbc ptr4
pha tax
inc sreg inc ptr1
L1: pla L1: txa
dey dey
bne L0 bne L0
sta ptr1 sta sreg
rts rts
@ -65,18 +66,18 @@ L1: pla
; 16by8 division ; 16by8 division
udiv16by8a: udiv16by8a:
@L0: asl sreg @L0: asl ptr1
rol sreg+1 rol ptr1+1
rol a rol a
bcs @L1 bcs @L1
cmp ptr4 cmp ptr4
bcc @L2 bcc @L2
@L1: sbc ptr4 @L1: sbc ptr4
inc sreg inc ptr1
@L2: dey @L2: dey
bne @L0 bne @L0
sta ptr1 sta sreg
rts rts

View File

@ -5,24 +5,24 @@
; ;
.export tosumoda0, tosumodax .export tosumoda0, tosumodax
.import popsreg, udiv16 .import popptr1, udiv16
.importzp ptr1, ptr4 .importzp sreg, ptr4
tosumoda0: tosumoda0:
ldx #0 ldx #0
tosumodax: tosumodax:
sta ptr4 sta ptr4
stx ptr4+1 ; Save right operand stx ptr4+1 ; Save right operand
jsr popsreg ; Get right operand jsr popptr1 ; Get right operand
; Do the division ; Do the division
jsr udiv16 jsr udiv16
; Result is in sreg, remainder in ptr1 ; Result is in ptr1, remainder in sreg
lda ptr1 lda sreg
ldx ptr1+1 ldx sreg+1
rts rts