SlideServe
Menu
Préférences
© SlideServe · v2026

Emulating Multiplication of Unsigned Integers in Software

chiaramonte avatar
11 diapositives · 146 Views · Jan 02, 2020

Learn how to perform multiplication of unsigned integers in software when hardware support is absent. Detailed instructions and examples provided for emulating multiplication in base ten and binary. Includes code snippets and insights for implementing emulation of binary multiplication.

Emulating Multiplication of Unsigned Integers in Software - slide 1
01 / 11Emulating Multiplication of Unsigned Integers in Software
1 / 17

Télécharger la présentation

Emulating Multiplication of Unsigned Integers in Software
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Emulating ‘MUL’ How multiplication of unsigned integers can be performed in software

  2. Hardware support absent? • The earliest microprocessors (from Intel, Zilog, and others) did not implement the multiplication instructions – that operation would have had to be done in software • Even with our Core-2 Quad processor, there is an ultimate limit on the sizes of integers that can be multiplied using the processor’s built-in ‘multiply’ instructions

  3. Multiplying with Base Ten • Here’s how we learn to multiply multi-digit integers using ordinary ‘decimal’ notation 8765  multiplicand x 4321  multiplier ------------------ 8765  partial product 17530  partial product 26295  partial product + 35060  partial product ------------------ 37873565  product

  4. Analogy with Base Ten • When multiplying multi-digit integers using ‘binary’ notation, we apply the same idea 1001  multiplicand (=9) x 1011  multiplier (=11) ------------------ 1001  partial product 1001  partial product 0000  partial product + 1001  partial product ------------------ 1100011  product (=99)

  5. Some observations… • With ‘binary’ multiplication of two N-digit values, the product can require 2N-digits • Each ‘partial product’ is either zero or is equal to the value of the ‘multiplicand’ • Succeeding partial products are ‘shifted’ • So if we want to ‘emulate’ multiplication of unsigned binary integers using software, we must implement these observations

  6. 8-bit case • The smallest case to consider is using the ‘mul’ instruction to compute the product of 8-bit values, say in registers AL and BL .section .data number1: .byte 100 number2: .byte 200 product: .word 0 .section .text mov number1, %al mov number2, %bl mul %bl mov %ax, product

  7. Doing it by hand 100 = 0x64 = 01100100 (binary) 01100100 AL 200 = 0xC8 = 11001000 (binary) 11001000 BL 00000000 (BL x 0) 00000000 (BL x 0) 11001000 (BL x 1) 00000000 (BL x 0) 00000000 (BL x 0) 11001000 (BL x 1) 11001000 (BL x 1) + 00000000 (BL x 0) ------------------------------------- 0100111000100000 20000 = 0x4E20 = 0100111000100000 (binary) 01001110 00100000 AX

  8. Using x86 instructions .section .text softmulb: push %rcx # save caller’s count-register sub %ah, %ah # zero-extend AL to (CF:AH:AL) mov $9, %rcx # number of bits in (CF:AH) nxbit8: rcr $1, %ax # next multiplier-bit to Carry-Flag jnc noadd # skip addition if CF-bit is zero add %bl, %ah # else add the multiplicand noadd8: loop nxbit8 # go back to shift in next CF-bit sub %ah, %cl # set CF-bit if 8-bits exceeded pop %rcx # recover caller’s count-register ret

  9. Visual depiction ROR $1, %AX CF AH AL 0 00000000 multiplier 17-bit value gets ‘rotated’ 1-place to the right BL multiplicand then multiplicand is added to AH (unless CF =0)

  10. Exhaustive testing • We can insert ‘inline assembly language’ in a C++ program to construct a loop that checks our software multiply operation against every case of the CPU’s ‘mul’ • Our test-program is names ‘multest.cpp’ • You can compile it like this: $ g++ multest.cpp softmulb.s -o multest

  11. In-class exercises • Can you write a ‘software’ emulation for the CPU’s 16-bit multiply operation? mul %bx • Can you write a ‘software’ emulation for the CPU’s 32-bit multiply operation? mul %ebx