NASM - The Netwide Assembler
NASM Forum => Using NASM => Topic started by: AndrejM on February 19, 2011, 12:35:20 AM
-
Small example:
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?
-
Well...
mov cx, dx
shl ecx, 16
mov cx, ax
or maybe...
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...
imul cx, ax, bx
; or
imul ecx, eax, ebx
Simplest thing, if you're doing 32-bit code, is to use 32-bit registers...
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
-
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?
-
Btw,
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.:
imul, ecx, eax, 10
-
You are correct. Sorry.
Best,
Frank