//---------------------------------------------------------------- // imul_bl.s // // A software multiply routine (to emulate imul %bl) which // computes the signed 16-bit product (in AX) of two 8-bit // signed integers (in AL and BL). The algorithm strategy // is to convert this signed multiplication to the simpler // unsigned case, but keeping track of the product's sign. // The conditional sign-changes that are needed here use a // technique whereby the rule "flip the bits then add one" // is performed using 'xor' to flip the bits, and subtract // minus-one to accomplish the adding of plus-one. // // programmer: ALLAN CRUSE // written on: 12 OCT 2003 //---------------------------------------------------------------- .section .text imul_bl: # # EXPECTS: AL = multiplicand # BL = multiplier # RETURNS: AX = product # pushl %ebx # save working registers pushl %ecx # replace AL by its absolute-value cbw # sign-extend AL to AX xorb %ah, %al # in case AL is negative, subb %ah, %al # flip bits and add one # swap AX with BX (insures BL positive, BH is sign) xchg %ax, %bx # replace AL by its absolute-value cbw # sign-extend AL to AX xorb %ah, %al # in case AL is negative, subb %ah, %al # flip bits and add one # setup sign for the product in register BH xorb %ah, %bh # leave product-sign in BH # multiply the two (nonnegative) values: AL and BL movl $9, %ecx # setup for 8-bit multiplier subb %ah, %ah # clear high result register nxbit8i: rcrw $1, %ax # next multiplier bit to CF jnc noadd8i # skip addition if bit == 0 addb %bl, %ah # else add the multiplicand noadd8i: loop nxbit8i # and shift the result # adjust product in AX to have the proper sign xorb %bh, %al # in case BH is nonzero, xorb %bh, %ah # flip bits in AL and AH subb %bh, %al # and then add one to AX sbbb %bh, %ah # by subtracting minus-one popl %ecx # restore saved registers popl %ebx ret .globl imul_bl