News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Trying to understand binary multiplication

Started by unktehi, February 25, 2009, 10:19:11 PM

Previous topic - Next topic

unktehi

One of the slides on the powerpoint presentation presents a formula for multiplying ax x 26.  But I'm not understanding how it works.  Here are the instructions, with what I thought should happen in the line underneath:

I had a question about slide 25.  Here are the instructions that were given (with what I understand is happening) but it looks like somehow I'm confused with one of the instructions.  Can you please help me understand?

mov ax,2
This moves the value of 2 into the ax register.

mov dx, ax
This moves the value of ax (2) into dx.  When the value is moved does the value of ax remain as 2?

shl dx, 4
This multiplies dx (2) by 2 with exponent 4 (16).  So wouldn't the value of dx be 2 x 16 or 32?

push dx
Save dx (32?) for later.  Once dx is pushed to the stack does the value of dx assume 0?

mov dx, ax
This moves the value of ax into dx.  Assuming ax is still 2, dx then becomes 2.

shl dx, 3
This multiplies dx (2) by 2 exponent 3 (8).  So wouldn't the value of dx become 2 x 8 or 16?

shl ax, 1
This multiplies ax (still 2?) by 2 exponent 1 (2).  So wouldn't the new value of ax be 2 x 2 or 4?

pop dx
This pops the 'pushed' value of dx (32) back into dx. Doesn't this replace the other dx value of 16 from two steps above?

add ax, dx
add ax (4) + dx (32?) = 36?

Which part am I not understanding correctly?

Thanks for your help!

raymond

Either you misplaced the "pop dx" instruction or it was misplaced in the tutorial.

First, forget what is the content of AX. Assume it is N. The aim is to multiply the content of AX by 26 without using any multiplication instruction.

mov dx,ax   ;make a copy of it, DX = N
shl dx,4    ;multiply it by 16, DX  = N x 16
push dx     ;save it for later
mov dx,ax   ;make another copy of AX, DX = N
shl dx,3    ;multiply it by 8,  DX = N x 8
shl ax,1    ;multiply it by 2, AX = N x 2
add ax,dx   ;AX = (N x 2) + (N x 8) = N x 10
pop dx      ;DX = N x 16
add ax,dx   ;AX = (N x 10) + (N x 16) = N x 26


Such a procedure would give you a correct result IF and only IF the result of the multiplication is not expected to exceed the range of the register used. In this case, it should not exceed 65535. Otherwise, the result is invalid. For example, if you start with AX=2521, the final result in AX would be 10!!!
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

unktehi

Ahh yes, I did omit the add ax,dx instruction in my typing and it looks like I left that out as well when I was trying to figure out how this all worked.  But I think I got it now.

Thanks for the help!

Mark Jones

Quote from: unktehi on February 25, 2009, 10:19:11 PM
mov dx, ax
This moves the value of ax (2) into dx.  When the value is moved does the value of ax remain as 2?

MOV is not a literal "move", think of it as a "copy." MOV only updates the destination operand.

Quote
shl dx, 4
This multiplies dx (2) by 2 with exponent 4 (16).  So wouldn't the value of dx be 2 x 16 or 32?

Think of SHL DX,4 as "multiply DX by 2 to the 4th power." It is DX * 2^4, which is DX * 16.

Quote
push dx
Save dx (32?) for later.  Once dx is pushed to the stack does the value of dx assume 0?

No. Most operations only modify the destination operand. This is by design.
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

unktehi

Mark,
  Thanks for clarifying those questions for me.  That makes sense.