News:

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

Multiplication question

Started by theZeroFlag, October 03, 2008, 03:18:46 PM

Previous topic - Next topic

theZeroFlag

Actually my issue is not with multiplication, but rather the way it is stored afterwards

when you use imul to multiply word values it stores the result in the dx:ax register pair

this is because the value could be up to dword long

all this makes great sense considering extended registers have not been around forever

my question is, what is the best way to get dx:ax into say the ebx register

i tried some indirect pointing and I got really confused

this is what i have so far

      mov ax, intOperand1
      imul intOperand2


any help is greatly appreciated!

I have changed the topic. hutch.

Mirno

This should do what you want (I think)

movzx ax,  ebx
movzx dx,  edx
shl   edx, 16
or    ebx, edx


Mirno

theZeroFlag

is there anyway to directly access the memory like

      mov [EBX], ax
      mov [EBX+2], dx

or something like that? I dont think that is the exact syntax

Mark Jones

Hello, experiment with this:


    mov BYTE PTR [memory offset],al
    mov WORD PTR [memory offset],ax
    mov DWORD PTR [memory offset],eax
    mov QWORD PTR [memory offset],rax  ; 64-bit processors only of course
    ; and
    mov al,BYTE PTR [memory offset]
    mov ax,WORD PTR [memory offset]
    mov eax,DWORD PTR [memory offset]
    mov rax,QWORD PTR [memory offset]


The MOV instruction can also take a [base + index * scale] syntax, search here for examples. Only REG<--REG, MEM<--REG, or REG<--MEM transfers are allowed in i86 architecture, MEM<--MEM is not supported. Have fun. :U
"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

theZeroFlag

i tried this and i am still getting some crazy results

      mov ax, intOperand1
      imul intOperand2
      mov WORD PTR [ebx], ax
      mov WORD PTR [ebx+2], dx

i am going to whip out the debugger i guess

dsouza123

Moves dx:ax into ebx, dx into hi word, ax into lo word (aka bx).


mov   ax, intOperand1
imul  intOperand2
movzx ebx, dx  ; mov   bx, dx  would also work, the hi word of ebx is going to be overwritten on the next instruction
shl   ebx, 16
mov   bx, ax

raymond

Since you seem to be working in 32-bit mode anyway (you do intend to use the EBX register), you should think FIRST to keep the size of your intOperand1 and intOperand2 variables as 32-bit instead of 16-bit, and then perform all operations in 32-bit.

If the latter is impossible for whatever reason, you should still perform your multiplication in 32-bit registers. The mnemonics of all the following instructions are based on the assumption that you need to perform a SIGNED multiplication.

movsx  eax,intOperand1
movsx  edx,intOperand2
imul   edx
mov    ebx,eax  ;assuming you absolutely need the result in EBX


Another way you could do it with 16-bit variables and you need your answer in EBX could be:

movsx  eax,intOperand1
movsx  ebx,intOperand2
imul   ebx,eax


As you can see, there are many ways to "skin a cat".
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com