### Author Topic: Storing multiplication value from two 16bit registers to a 32bit register  (Read 3784 times)

#### AndrejM

• Jr. Member
• Posts: 9
##### Storing multiplication value from two 16bit registers to a 32bit register
« on: February 19, 2011, 12:35:20 AM »
Small example:
Code: [Select]
`mov bx, 10dmov ax, 10dmul bx ; multiplied by ax, stored in dx:ax`
How mul works is explained in both the pcasm and the Step by Step books. But I don't see how I can move this result into a single 32bit register (e.g. ecx).

What's the trick I need to do?

#### Frank Kotler

• NASM Developer
• Hero Member
• Posts: 2429
• Country:
##### Re: Storing multiplication value from two 16bit registers to a 32bit register
« Reply #1 on: February 19, 2011, 02:29:00 AM »
Well...

Code: [Select]
`mov cx, dxshl ecx, 16mov cx, ax`
or maybe...

Code: [Select]
`push dxpush axpop ecx`
But, as a general rule, you'd want to stick with one size of register. For signed integers, or if your values are of a magnitude that it doesn't matter...

Code: [Select]
`imul cx, ax, bx; orimul ecx, eax, ebx`
Simplest thing, if you're doing 32-bit code, is to use 32-bit registers...

Code: [Select]
`mov eax, 10mov ebx, 10mul ebx`
"mul" will set the carry flag if the result overflows [e]ax and there's anything but zero in [e]dx. You could use this to determine if you need to bother with [e]dx at al... if that applies to what you're trying to do...

Best,
Frank

#### AndrejM

• Jr. Member
• Posts: 9
##### Re: Storing multiplication value from two 16bit registers to a 32bit register
« Reply #2 on: February 19, 2011, 03:06:46 AM »
Thanks, Frank.

The example was in the book, so I was left wondering how to do it. Otherwise I'd just use 32bit registers and call it a day.

P.S. Is it just me or is the font size for code snippets on this forum extremely small?

#### AndrejM

• Jr. Member
• Posts: 9
##### Re: Storing multiplication value from two 16bit registers to a 32bit register
« Reply #3 on: February 21, 2011, 01:55:05 AM »
Btw,

Code: [Select]
`imul cx, ax, bx; orimul ecx, eax, ebx`

I don't think this is legal (nasm will error out). According to the book, source2 can only be an immediate value, e.g.:
Code: [Select]
`imul, ecx, eax, 10`

#### Frank Kotler

• NASM Developer
• Hero Member
• Posts: 2429
• Country:
##### Re: Storing multiplication value from two 16bit registers to a 32bit register
« Reply #4 on: February 21, 2011, 07:00:05 AM »
You are correct. Sorry.

Best,
Frank