//---------------------------------------------------------------- // softdivb.s // // This file uses shift/rotate instructions to emulate with // software the Pentium's hardware divide operation for the // case when the 8-bit divisor is held in the BL register. // // programmer: ALLAN CRUSE // written on: 12 OCT 2003 // correction: 15 MAR 2006 -- divide-overflow test revised // revised on: 15 MAR 2009 -- for x86_64 Linux environment //---------------------------------------------------------------- .section .text softdivb: # # EXPECTS: AX = dididend # BL = divisor # RETURNS: AL = quotient # AH = remainder # provided ( AH < BL ) # OTHERWISE: divide-overflow exception; # cmpb %bl, %ah # test: quotient overflow? jb okdiv8 # no, proceed with division int $0 # else simulate exception 0 okdiv8: push %rcx # preserve register's value mov $8, %rcx # setup for 8-bit divisor nxbit8: shl $1, %ax # double the dividend jc dosub8 cmp %bl, %ah # can divisor be subtracted? jc nosub8 # skip subtract if bit == 1 dosub8: inc %al # record this division sub %bl, %ah # else subtrace the divisor nosub8: loop nxbit8 # and shift the result pop %rcx # restore saved register ret .global softdivb