Archived

This topic is now archived and is closed to further replies.

__ASM multiplication

This topic is 6355 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am currently trying my hand at an isometric game, and I am just doing a few optimizations to get the thing to run a little faster. Right now, I am experimenting with the inline assembler in Visual C++ to quickly multiply and divide. I know how to initialize inline __asm, but I don''t know any assembler to speak of. Can anybody help me out with how to multiply two numbers with it (and maybe divide too)?

Share this post


Link to post
Share on other sites
I''ll email you a help file in a sec. But here''s the basic idea:
In an Intel CPU you''ve got a number of registers:
eax - This is the accumulator register
ebx - Base register
ecx - Counter register (Used for loops and other counting stuff)
edx - Data register (Used for an extra term when dealing with data)

So the two main commands you''ll want are:
mul - Unsigned Multiply
div - Unsigned Divide
imul - Signed Multiply
idiv - Signed divide

Mul - Multiply
Usage: mul src
Unsigned multiply of the accumulator by the source. If "src" is
a byte value, then AL is used as the other multiplicand and the
result is placed in AX. If "src" is a word value, then AX is
multiplied by "src" and DX:AX receives the result. If "src" is
a double word value, then EAX is multiplied by "src" and EDX:EAX
receives the result. The 386+ uses an early out algorithm which
makes multiplying any size value in EAX as fast as in the 8 or 16
bit registers.

DIV - Divide
Usage: DIV src
Modifies flags: (AF,CF,OF,PF,SF,ZF undefined)
Unsigned binary division of accumulator by source. If the source
divisor is a byte value then AX is divided by "src" and the quotient
is placed in AL and the remainder in AH. If source operand is a word
value, then DX:AX is divided by "src" and the quotient is stored in AX
and the remainder in DX.

IMUL - Signed Multiply
Usage: IMUL src
Signed multiplication of accumulator by "src" with result placed
in the accumulator. If the source operand is a byte value, it
is multiplied by AL and the result stored in AX. If the source
operand is a word value it is multiplied by AX and the result is
stored in DX:AX. Other variations of this instruction allow
specification of source and destination registers as well as a
third immediate factor.

IDIV - Signed Integer Division
Usage: IDIV src
Signed binary division of accumulator by source. If source is a
byte value, AX is divided by "src" and the quotient is stored in
AL and the remainder in AH. If source is a word value, DX:AX is
divided by "src", and the quotient is stored in AL and the
remainder in DX.

Oh and I guess it''s important to mention AX is the 16-bit version of eax which is 32 bit. There''s also al (The lower 8bits) ah (The higher 8bits of the ax register). The reason for these funny names is cause eax wasn''t introduced until the 386 before that all you had was ax so ah was really the high 8 bits at that time.

Hope that helps!

Oh and if you want to move a value into one of the regosters use "mov" it works like this:
mov dest, src

So here''s an example:


mov eax, MyVariable ;Move in our variable
mov ebx, 10 ;Let''s use 10 for our multiplicant
mul ebx ;Multiply eax by ebx


now eax would have MyVariable*10 in it, which you could move into a structure or something whatever you want.
Good luck!
See ya,
Ben

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Unless you''ve profiled and found that this is a hotspot, don''t bother. Visual C++ code generation is fairly good. You''re not going to beat it in any significant way simply by doing divides and multiplies in assembly.

Share this post


Link to post
Share on other sites
Cyberben, it''s people like you that make me thank the lord I don''t know, and have never tried to learn, assembly.

;]

- Goblin
"In order to understand the full depth of mankind, you must first seperate the word into its component parts: 'mank' and 'ind'."

Share this post


Link to post
Share on other sites