Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiplication by Addition (using shifts)

Status
Not open for further replies.

weevilofdoom

Programmer
Jan 28, 2003
91
US
I've got two numbers
numOne WORD 666
numTwo WORD 34

I've converted inputted numbers (ascii) to binary, and stored them in these two "variables"
I need to multiply these by adding, and shifting, but I don't know where to start or how to do this? any help would be appreciated The weevil of doooooooooom
-The eagle may soar, but the weasel never gets sucked up by a jet engine (Anonymous)
 
Hey, nevermind, I figured it out, was forgetting to shift one of my items if the carry was set to 0, thanks though :) The weevil of doooooooooom
-The eagle may soar, but the weasel never gets sucked up by a jet engine (Anonymous)
 
hehe! never mind - the number of times i wanted to half or double a number and i "shl eax,02h" or "shr eax,02h" hehe
"There are 10 types of people in this world, those who know binary and those who don't!"
 
If you want to double a number, consider adding it to itself. It's quicker than shifting.
 
interesting, shame my manual doesnt include bus cycle timings. certainly worth an investigation me thinks :)

"There are 10 types of people in this world, those who know binary and those who don't!"
 
Hm, been looking again, I'm not sure how up-to-date I am. My book has limited clock-cycle info and is a bit old, but suggests shifts slower than adds. Abrash's assembly stuff lists the shifts as one cycle on a pentium, same as addition, but with the proviso that they are not capable of running in the V-pipe (though they don't stop another instruction from running in V when they're in the U-pipe). Addition definitely runs in either pipe, so it's a good option.
If you like this sort of thing, have a good look at LEA in protected mode, where you can scale things.
Something like lea ebx, [4*ebx + ebx]
should work, and multiply by 5 in a single clock (in either pipe)! This works in 16-bit mode too, but with less saving of time because it needs an operand size prefix, which takes time.

Does anyone know anything about timing of shl ax, 1 in its special shift-once form, compared to shl ax, n (the general instruction) using n=1 (assemblers won't normally write this, but I've heard it can be quicker??)??

Just an extra: clock cycles are good to know, but they're only one factor in deciding how fast the code goes. Loss of time waiting for video cards to cooperate, interaction with the cache(s) etc. are all important too. For instance, if you manage to write a loop bigger than the cache (very difficult to do!) then it will run disproportionately slowly because the cache will have to be refilled from memory every loop, and the extra pipes of a pentium will never be used.

Anyone any comments, please add. I'd be very interested in sources of up-to-date information.
 
Yeah, flushing TLBs and reloading is the slowest thing i ever saw a processor do! "There are 10 types of people in this world, those who know binary and those who don't!"
 
Hi straiph!
Slowest thing I ever did was put the processor in "trap" mode by accident (saved the flag register but restored it from the wrong place....). Now that was sloooooow....
(took me quite a while to track down the problem, too).
Lionel
 
hehe, yes that would be slow!

the code that i was testing created a selector and then called another routine which inadvertently flushed the TLBs. it took 1hr to fill the 64k table on the test run hehe (considering it should have taken milliseconds). i had to make considerable modifications to stop this occurring.

back to the bus cycles - i believe bus cycles are consumed during memory accesses more often than not. if i have free registers available ie esi,edi,ebp etc i tend to use them instead of storing the data and variables in memory operands, this also has the effect of reducing the size of your opcodes (less bus cycles) and make the code alot smaller (less 4 byte memory pointers). im sure all seasoned programmers do this but hopefully this might inspire someone to improve their methods.

straiph
"There are 10 types of people in this world, those who know binary and those who don't!"
 
Absolutely! Presumeably that's also the point of register variables in C (lots of discussion a few weeks ago about them. Very odd!)
In the good old days (where some of us still live) of real-mode programming, you could even dump data in a segment register. Oh horrors.
 
<breaks out into a cold sweat> hehe &quot;There are 10 types of people in this world, those who know binary and those who don't!&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top