Intel 8086 Assembly — Squaring a Register?

Up vote 3 down vote favorite share g+ share fb share tw.

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? Assembly micro-optimization 8086 link|improve this question edited Jul 18 '10 at 20:15GregS9,5532717 asked Jul 18 '10 at 20:10Raven Dreamer1,050220 100% accept rate.

Thanks for the code formatting, GregS. Slipped my mind. – Raven Dreamer Jul 18 '10 at 21:14.

I would benchmark before assuming that it is faster as well. I can imagine that in hardware the ax = ax*ax operation will be more cumbersome than the mul bx equivalent. (I must admit this is pure speculation).

I wasn't predicting it was faster because it was multiplying by the AX register, but rather that it would be faster because it had one less execution (only one MOV command). The machine code between mul BX and mul AX only varies by the 3 bit register designation. Mine was just a question born of overcaution and self-doubt.

– Raven Dreamer Jul 18 '10 at 20:20 @Raven Dreamer: In assembly language, fewer lines does not always mean faster. Always benchmark if your code is performance-critical. – Greg Hewgill Jul 18 '10 at 20:27 Exactly; the naive assumption that 1 instruction takes 1 clock tick (yes, been there) is incorrect.

– mvds Jul 18 '10 at 20:38 @ Greg, Mvds: but as I stated in the question, I am specifically striving for fewer lines, not fastest runtime. Were this not for a course where number of lines was a grading metric, I'd agree with you. Perhaps "faster" was a poor choice of words.

– Raven Dreamer Jul 18 '10 at 20:54.

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=AX*AX but AX=AL*AL (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).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions