From b072f5d042705ea014f33fc7f6c49aad59b8f419 Mon Sep 17 00:00:00 2001 From: Stephen Heumann Date: Fri, 5 Feb 2021 18:11:37 -0600 Subject: [PATCH] Implement lldiv and imaxdiv functions. These do 64-bit division and return a struct with the quotient and remainder. --- stdlib.asm | 34 +++++++++++++++++ stdlib.macros | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+) diff --git a/stdlib.asm b/stdlib.asm index edd8c8a..af6d175 100644 --- a/stdlib.asm +++ b/stdlib.asm @@ -534,6 +534,40 @@ lb1 lla addr,div_t return the result div_t ds 8 end +**************************************************************** +* +* lldiv_t lldiv(long long n, long long d) +* +* Inputs: +* n - numerator +* d - denominator +* +* Outputs: +* lldiv_t - contains result & remainder +* +**************************************************************** +* +lldiv start +imaxdiv entry +addr equ 1 + + csubroutine (8:n,8:d),4 + phb use local addressing + phk + plb + ph8 n do the divide + ph8 d + jsl ~CDIV8 + pl8 lldiv_t + pl8 lldiv_t+8 + lla addr,lldiv_t return the result + plb + + creturn 4:addr + +lldiv_t ds 16 + end + **************************************************************** * * void qsort(base, count, size, compar) diff --git a/stdlib.macros b/stdlib.macros index e786465..6467427 100644 --- a/stdlib.macros +++ b/stdlib.macros @@ -585,3 +585,107 @@ DC I4'&DCB' ~RESTM MEND + macro +&l ph8 &n1 + lclc &c +&l anop +&c amid &n1,1,1 + aif s:longa=1,.a + rep #%00100000 +.a + aif "&c"="#",.d + aif "&c"="[",.b + aif "&c"<>"{",.c +&c amid &n1,l:&n1,1 + aif "&c"<>"}",.g +&n1 amid &n1,2,l:&n1-2 +&n1 setc (&n1) +.b + ldy #6 +~&SYSCNT lda &n1,y + pha + dey + dey + bpl ~&SYSCNT + ago .e +.c + ldx #6 +~&SYSCNT lda &n1,x + pha + dex + dex + bpl ~&SYSCNT + ago .e +.d +&n1 amid &n1,2,l:&n1-1 + bra ~b&SYSCNT +~a&SYSCNT dc i8"&n1" +~b&SYSCNT ldx #6 +~c&SYSCNT lda ~a&SYSCNT,x + pha + dex + dex + bpl ~c&SYSCNT +.e + aif s:longa=1,.f + sep #%00100000 +.f + mexit +.g + mnote "Missing closing '}'",16 + mend + macro +&l pl8 &n1 + lclc &c +&l anop + aif s:longa=1,.a + rep #%00100000 +.a +&c amid &n1,1,1 + aif "&c"<>"{",.b +&c amid &n1,l:&n1,1 + aif "&c"<>"}",.f +&n1 amid &n1,2,l:&n1-2 + pla + sta (&n1) + ldy #2 + pla + sta (&n1),y + ldy #4 + pla + sta (&n1),y + ldy #6 + pla + sta (&n1),y + ago .d +.b + aif "&c"<>"[",.c + pla + sta &n1 + ldy #2 + pla + sta &n1,y + ldy #4 + pla + sta &n1,y + ldy #6 + pla + sta &n1,y + ago .d +.c + pla + sta &n1 + pla + sta &n1+2 + pla + sta &n1+4 + pla + sta &n1+6 +.d + aif s:longa=1,.e + sep #%00100000 +.e + mexit +.f + mnote "Missing closing '}'",16 + mend