3

In principle, squaring the value of a register isn't hard:

mov ax, [var]
mov cx, [var]
mul cx         // square of answer is in DX:AX

But I got to thinking -- the course that I'm learning Assembly for prizes efficiency very highly; a difference of even a single line less could be worth up to 5 points.

I realize this is micro-optimization, but would the following code perform the the same way?:

mov ax, [var]
mul ax          // is answer still in DX:AX ?

I suppose a much simpler way of expressing my question: is AX (or AL/AH) a valid parameter for the mul and imul commands in 8086 assembly?

4

2 回答 2

3

Yes, mul ax puts ax*ax to dx:ax.

于 2010-07-18T20:13:01.750 回答
1

You can use mul ax it would do DX:AX = AX * AX but note that in this case you will lose the value you had in AX so if you need to use this value again sometime it is better to use the mul bx option, because BX remains intact.

Also if you use mul al or (mul ah) you will not do AX=AXAX but AX=ALAL (or AX=AL*AH) so if the value in AX is bigger than 255 you aren't doing a multiplication of AX because you completely ignore and overwrite the higher part of the value (you ignore AH).

于 2010-08-04T11:57:33.777 回答