user204415
user204415

Reputation: 317

Understanding Multiplication in Assembly

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

Upvotes: 0

Views: 621

Answers (0)

Related Questions