Difference between revisions of "Programming:Integer Division"
From CPCWiki - THE Amstrad CPC encyclopedia!
PulkoMandy (Talk | contribs) m |
(→16bit division) |
||
Line 16: | Line 16: | ||
clcd161 rl c | clcd161 rl c | ||
rla | rla | ||
− | + | adc hl,hl | |
− | + | ||
sbc hl,de | sbc hl,de | ||
jr nc,clcd162 | jr nc,clcd162 |
Revision as of 03:25, 16 September 2006
16bit division
Input: BC=Value1, DE=Value2
Output: HL=Value1/Value2, DE=Value1 MOD Value2
Destroyed: AF,BC
clcd16 ld a,e or d ld hl,0 ret z ld a,b ld b,16 clcd161 rl c rla adc hl,hl sbc hl,de jr nc,clcd162 add hl,de clcd162 ccf djnz clcd161 ex de,hl rl c rla ld h,a ld l,c ret
24bit division
Input: A,BC=Value1, DE=Value2
Output: HL=Value1/Value2, DE=Value1 MOD Value2
Destroyed: AF,BC,IX,IYL
clcdiv db #dd:ld l,e db #dd:ld h,d ;IX=Value2 ld e,a ;E,BC=Value1(Counter) ld hl,0 db #dd:ld a,l db #dd:or h ret z ld d,l ;D,HL=CalcVar db #fd:ld l,24 ;IYL=Counter clcdiv1 rl c rl b rl e rl l rl h rl d ld a,l db #dd:sub l ld l,a ld a,h db #dd:sbc h ld h,a ld a,d sbc 0 ld d,a ;D,HL=D,HL-IX jr nc,clcdiv2 ld a,l db #dd:add l ld l,a ld a,h db #dd:adc h ld h,a ld a,d adc 0 ld d,a scf clcdiv2 ccf db #fd:dec l jr nz,clcdiv1 ex de,hl ;DE=Value1 MOD Value2 rl c rl b ld l,c ld h,b ;HL=Value1 DIV Value2 ret
32bit division
Input: IY,BC=Value1, IX=Value2
Output: IY,BC=Value1/Value2, HL=Value1 MOD Value2
Destroyed: AF,DE,IY
clcd32c db 0 clcd32 ld hl,0 db #dd:ld a,l db #dd:or h ret z ;IY,BC=Value1(Counter) ld de,0 ;DE,HL=CalcVar ld a,32 ;Set Counter to 32 clcd321 ld (clcd32c),a rl c rl b db #fd:ld a,l:rla:db #fd:ld l,a db #fd:ld a,h:rla:db #fd:ld h,a rl l rl h rl e rl d ld a,l db #dd:sub l ld l,a ld a,h db #dd:sbc h ld h,a ld a,e sbc 0 ld e,a ld a,d sbc 0 ld d,a jr nc,clcd322 ld a,l db #dd:add l ld l,a ld a,h db #dd:adc h ld h,a ld a,e adc 0 ld e,a ld a,d adc 0 ld d,a scf clcd322 ccf ld a,(clcd32c) dec a jr nz,clcd321 ;HL=Value1 MOD Value2 rl c rl b db #fd:ld a,l:rla:db #fd:ld l,a db #fd:ld a,h:rla:db #fd:ld h,a ret ;IY,BC=Value1 DIV Value2