0

I'm trying to understand how this piece of assembly code makes an 8-bit multiplication. I do know what each line do, but I can't understand how it makes the whole multiplication.

Can you please explain how it makes the multiplication? Thanks in advance

;=========================================================
;routine: mult_soft
;  function: 8-bit unsigned multiplier using
;           shift-and-add algorithm
;  input register:
;     s3: multiplicand
;     s4: multiplier
;  output register:
;     s5: upper byte of product
;     s6: lower byte of product
;  temp register: s2
;=========================================================

load s3, 2                          ;multiplicand
load s4, 2                          ;multiplier
mult_soft:
          load s5, 00               ;clear s5
          load s2, 08               ;initialize loop index
mult_loop:
          sr0  s4                  ;shift lsb to carry
          jump nc, shift_prod      ;lsb is 0
          add s5, s3               ;lsb is 1

shift_prod:     
          sra s5                   ;shift upper byte right,
                                   ;carry to MSB, LSB to carry
          sra s6                   ;shift lower byte right,
                                   ;lsb of s5 to MSB of s6
          sub s2, 01               ;dec loop index
          jump nz, mult_loop       ;repeat until i=0

          ret
user204415
  • 307
  • 1
  • 4
  • 20
  • 4
    using grade school pencil and paper math, do a few binary multiplications. what you should realize is that with base 10 you have to actually multiply a digit against the other parameter then shift and add that into the accumulated result. with binary you are either multiplying the other parameter by zero or by one, which means you either shift and add or dont, turn that into a loop, take your lower parameter on paper shift it right to examine the lsbit, shift the operand left to add it in or not – old_timer Dec 13 '17 at 18:33
  • 3
    See also [wikipedia](https://en.wikipedia.org/wiki/Multiplication_algorithm#Peasant_or_binary_multiplication). _"long multiplication reduces to a nearly trivial operation. For each '1' bit in the multiplier, shift the multiplicand an appropriate amount and then sum the shifted values."_. As old_timer said, it's just elementary school maths in binary. – Jester Dec 13 '17 at 18:35

0 Answers0