; equates
linprt  =      $bdcd     ; linprt=$8e32 for the 128
chrout  =      $ffd2

; demonstration wrapper
        lda    #147      ; clear screen
        jsr    chrout
        ldx    divnum    ; print numerator or dividend
        lda    divnum+1
        jsr    linprt
        lda    #47       ; print "/"
        jsr    chrout
        ldx    divden    ; print denominator or divisor
        lda    divden+1
        jsr    linprt
        lda    #13       ; print RETURN
        jsr    chrout
        jsr    divint    ; divide the numbers
        ldx    answer    ; and print the answer
        lda    answer+1
        jsr    linprt
        lda    #13       ; print RETURN again
        jsr    chrout
        lda    #82       ; "R" indicates Remainder
        jsr    chrout
        ldx    remain    ; print remainder
        lda    remain+1
        jsr    linprt
        rts

; data storage
divnum  .word  3112      ; 3112 will be divided by
divden  .word  550       ; 550
work    .byte  0,0
remain  =      work      ; the remainder will end up in work
answer  .byte  0,0
copyn   .byte  0,0
countr  .byte  0

; 16-BIT DIVISION
divint  jsr    setup     ; set the counter to 16
        jsr    zeros     ; zero out work and answer
        jsr    copynm    ; copy divnum to copyn
divlp   jsr    mvover    ; rotate copyn and work to the left
        jsr    divide    ; the main division routine
        dec    countr    ; count down
        bne    divlp     ; if it's not zero yet, keep going
        rts

; setup, put 16 into countr, represents number of bits in divnum
setup   lda    #16
        sta    countr
        rts

; zeros, zero out work and answer
zeros   lda    #0
        ldy    #3
zloop   sta    work,y
        dey
        bpl    zloop     ; as long as y is zero or higher, loop back
        rts

; copynm, copy divnum to copyn
copynm  lda    divnum
        sta    copyn
        lda    divnum+1
        sta    copyn+1
        rts

; mvover, rotate copyn and work to the left
mvover  asl    copyn
        rol    copyn+1
        rol    work
        rol    work+1
        rts

; divide, main division routine
divide  lda    work+1    ; high byte of work
        cmp    divden+1  ; compare to the divisor
        beq    lookmr    ; look more (check the low byte) if equal
        bcs    subtr     ; work is higher, so subtract
                         ; if we fall through from above, carry is clear
fixans  rol    answer    ; move carry flag into answer
        rol    answer+1  ; high byte, too
        rts
lookmr  lda    work      ; get value in work
        cmp    divden    ; compare to denominator (divisor) low byte
        bcc    fixans    ; if carry is clear, divden is higher, so exit
subtr   jsr    fixans    ; carry is always set at this entry point
        sec              ; carry was changed by fixans, so set it
        lda    work      ; subtract divden from work
        sbc    divden
        sta    work
        lda    work+1
        sbc    divden+1
        sta    work+1
        rts


--  
/Etienne von Wettingfeld

-> ICQ: 2559832 (Authorize)    |  At first there was nothing
-> IRC: Chaos_One on #AmigaNL  |  Then God said 'Let there be light!'
-> Fax: +31 (020) 8835157      |  Then there was still nothing.
-> WWW: www.doomdark.demon.nl  |  But you could see it.


http://www.dds.nl/~nowmoo/

Antwoord per e-mail aan