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

Offline AndrejM

  • Jr. Member
  • *
  • Posts: 9
Small example:
Code: [Select]
mov bx, 10d
mov ax, 10d
mul 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?

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
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, dx
shl ecx, 16
mov cx, ax

or maybe...

Code: [Select]
push dx
push ax
pop 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
; or
imul ecx, eax, ebx

Simplest thing, if you're doing 32-bit code, is to use 32-bit registers...

Code: [Select]
mov eax, 10
mov ebx, 10
mul 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


Offline 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?

Offline 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
; or
imul 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

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
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