From 8a17ba9b999c25bd976393595151bf255f04bd5a Mon Sep 17 00:00:00 2001 From: MaartenBrock Date: Thu, 29 May 2008 11:47:22 +0000 Subject: [PATCH] * device/lib/gbz80/div.s, * device/lib/z80/div.s: fixed bug 1904314 by applying a modified part of patch 1597883, Thanks Robert Ramey git-svn-id: https://sdcc.svn.sourceforge.net/svnroot/sdcc/trunk/sdcc@5187 4a8a32a2-be11-0410-ad9d-d568d2c75423 --- ChangeLog | 6 ++++++ device/lib/gbz80/div.s | 38 ++++++++++++++++++++------------------ device/lib/z80/div.s | 38 +++++++++++++++++--------------------- 3 files changed, 43 insertions(+), 39 deletions(-) diff --git a/ChangeLog b/ChangeLog index d3b63e1b..29ca2e1d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2008-05-29 Maarten Brock + + * device/lib/gbz80/div.s, + * device/lib/z80/div.s: fixed bug 1904314 by applying a modified part of + patch 1597883, Thanks Robert Ramey + 2008-05-29 Maarten Brock * src/SDCCast.c (createIvalType, createIvalStruct, createIvalArray, diff --git a/device/lib/gbz80/div.s b/device/lib/gbz80/div.s index d2ac308d..defe1877 100644 --- a/device/lib/gbz80/div.s +++ b/device/lib/gbz80/div.s @@ -204,9 +204,10 @@ __moduint_rrx_hds:: ;; if signs are different ;; Remainder has same sign as dividend ld a,b ; Get high byte of dividend - ld (.srem),a ; Save as sign of remainder + push af ; Save as sign of remainder xor d ; Xor with high byte of divisor - ld (.squot),a ; Save sign of quotient + push af ; Save sign of quotient + ;; Take absolute value of divisor bit 7,d jr Z,.chkde ; Jump if divisor is positive @@ -229,9 +230,9 @@ __moduint_rrx_hds:: ;; Divide absolute values .dodiv: call .divu16 - ret C ; Exit if divide by zero + jr C,.exit ; Exit if divide by zero ;; Negate quotient if it is negative - ld a,(.squot) + pop af ; recover sign of quotient and #0x80 jr Z,.dorem ; Jump if quotient is positive sub a ; Substract quotient from 0 @@ -242,7 +243,7 @@ __moduint_rrx_hds:: ld b,a .dorem: ;; Negate remainder if it is negative - ld a,(.srem) + pop af ; recover sign of remainder and #0x80 ret Z ; Return if remainder is positive sub a ; Substract remainder from 0 @@ -252,6 +253,10 @@ __moduint_rrx_hds:: sub d ld d,a ret +.exit: + pop af + pop af + ret .divu8:: .modu8:: @@ -284,7 +289,7 @@ __moduint_rrx_hds:: ;; HL holds remainder ;; Do a 32-bit left shift, shifting carry to L, L to H, ;; H to C, C to B - ld (.dcnt),a + push af ; save number of bits remaining rl l ; Carry (next bit of quotient) to bit 0 rl h ; Shift remaining bytes rl c @@ -303,14 +308,20 @@ __moduint_rrx_hds:: ; next bit of quotient) jr C,.drop ; Jump if remainder is >= dividend pop bc ; Otherwise, restore remainder + pop af ; recover # bits remaining, carry flag destroyed + dec a + or a ; restore (clear) the carry flag + jr NZ,.dvloop jr .nodrop .drop: inc sp inc sp -.nodrop: - ld a,(.dcnt) - dec a ; DEC does not affect carry flag + pop af ; recover # bits remaining, carry flag destroyed + dec a + scf ; restore (set) the carry flag jr NZ,.dvloop + jr .nodrop +.nodrop: ;; Shift last carry bit into quotient ld d,b ; DE = remainder ld e,c @@ -320,12 +331,3 @@ __moduint_rrx_hds:: ld b,h ; B = high byte of quotient or a ; Clear carry, valid result ret - - .area _BSS - -.srem: - .ds 0x01 ; Sign of quotient -.squot: - .ds 0x01 ; Sign of remainder -.dcnt: - .ds 0x01 ; Counter for division diff --git a/device/lib/z80/div.s b/device/lib/z80/div.s index 19e47457..e20c31b2 100644 --- a/device/lib/z80/div.s +++ b/device/lib/z80/div.s @@ -208,12 +208,13 @@ __moduint_rrx_hds:: ;; if signs are different ;; Remainder has same sign as dividend ld a,b ; Get high byte of dividend - ld (.srem),a ; Save as sign of remainder + push af ; Save as sign of remainder xor d ; Xor with high byte of divisor - ld (.squot),a ; Save sign of quotient + push af ; Save sign of quotient + ;; Take absolute value of divisor bit 7,d - jp Z,.chkde ; Jump if divisor is positive + jr Z,.chkde ; Jump if divisor is positive sub a ; Substract divisor from 0 sub e ld e,a @@ -223,7 +224,7 @@ __moduint_rrx_hds:: ;; Take absolute value of dividend .chkde: bit 7,b - jp Z,.dodiv ; Jump if dividend is positive + jr Z,.dodiv ; Jump if dividend is positive sub a ; Substract dividend from 0 sub c ld c,a @@ -233,11 +234,11 @@ __moduint_rrx_hds:: ;; Divide absolute values .dodiv: call .divu16 - ret C ; Exit if divide by zero + jr C,.exit ; Exit if divide by zero ;; Negate quotient if it is negative - ld a,(.squot) + pop af ; recover sign of quotient and #0x80 - jp Z,.dorem ; Jump if quotient is positive + jr Z,.dorem ; Jump if quotient is positive sub a ; Substract quotient from 0 sub c ld c,a @@ -246,7 +247,7 @@ __moduint_rrx_hds:: ld b,a .dorem: ;; Negate remainder if it is negative - ld a,(.srem) + pop af ; recover sign of remainder and #0x80 ret Z ; Return if remainder is positive sub a ; Substract remainder from 0 @@ -256,6 +257,10 @@ __moduint_rrx_hds:: sub d ld d,a ret +.exit: + pop af + pop af + ret .divu8:: .modu8:: @@ -268,7 +273,7 @@ __moduint_rrx_hds:: ;; Check for division by zero ld a,e or d - jp NZ,.divide ; Branch if divisor is non-zero + jr NZ,.divide ; Branch if divisor is non-zero ld bc,#0x00 ; Divide by zero error ld d,b ld e,c @@ -280,7 +285,7 @@ __moduint_rrx_hds:: ; ld h,b ; H = high byte of dividend/quotient ; ld bc,#0x00 ; BC = remainder or a ; Clear carry to start - ex af,af + ex af,af' ld a,#16 ; 16 bits in dividend .dvloop: ;; Shift next bit of quotient into bit 0 of dividend @@ -308,9 +313,9 @@ __moduint_rrx_hds:: ccf ; Complement borrow so 1 indicates a ; successful substraction (this is the ; next bit of quotient) - jp C,.drop ; Jump if remainder is >= dividend + jr C,.drop ; Jump if remainder is >= dividend pop hl ; Otherwise, restore remainder - jp .nodrop + jr .nodrop .drop: inc sp inc sp @@ -328,12 +333,3 @@ __moduint_rrx_hds:: ; ld b,h ; B = high byte of quotient or a ; Clear carry, valid result ret - - .area _BSS - -.srem: - .ds 0x01 ; Sign of quotient -.squot: - .ds 0x01 ; Sign of remainder -.dcnt: - .ds 0x01 ; Counter for division -- 2.30.2