Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

multiply a variable x 2

Status
Not open for further replies.
Multipling by two is trivial in binary, it's just a logical shift left - as you're going to more than 8 bits you need to make it a 16 bit shift using two bytes, this is why shift instructions shift the bit out to carry - you can then shift it to the next byte.
 
Nigel Goodwin said:
Multipling by two is trivial in binary, it's just a logical shift left - as you're going to more than 8 bits you need to make it a 16 bit shift using two bytes, this is why shift instructions shift the bit out to carry - you can then shift it to the next byte.
I made precisely this .
 
I don't know about PICs but on the x86 in virtual mode it's simple in assembly, the add instruction was faster than the logical shift left.

For example to multiply x by 2:
mov ax,[x]
add [x],ax
The above code works in NASM.
 
Last edited:
So can't you just apply this example to the micro you're using?

It's exactly the same principle.
 
Hero999 said:
So can't you just apply this example to the micro you're using?

It's exactly the same principle.

Why would you want to use two instructions instead of one?, although for a PIC it will need more than one as it's a 16 bit shift - even so, it's still probably half the instructions of using add.
 
512 Well it's not quite a 16 bit value more like 9 bits. You could probably save a byte and use the carry register which would have the correct bit after a logical shift left.
 
blueroomelectronics said:
512 Well it's not quite a 16 bit value more like 9 bits. You could probably save a byte and use the carry register which would have the correct bit after a logical shift left.

But only assuming you're using it immediately, and don't have to store it anywhere - if you've got to store it you may as well use two GPR's as a 16 bit register, which makes the shifting dead simple.
 
Nigel Goodwin said:
Why would you want to use two instructions instead of one?, although for a PIC it will need more than one as it's a 16 bit shift - even so, it's still probably half the instructions of using add.
I take it you haven't done any programming on the PC then.

Because the x86 can't directly add, subtract or move data in one memory location to another, it has to go via a register. Therefore unless you're using register variables you can't do add [x],[x].

Wait I'm talking total rubbish, you can always do shl [x],1 :D
 
Hero999 said:
I take it you haven't done any programming on the PC then.

Because the x86 can't directly add, subtract or move data in one memory location to another, it has to go via a register. Therefore unless you're using register variables you can't do add [x],[x].

Wait I'm talking total rubbish, you can always do shl [x],1 :D

I've done plenty of programming on PC's, but we're not talking PC's, and I can't believe any processor would be faster doing an add rather than a simple shift? - it's such a basic instruction, and so easy for a processor to do.
 
Well the Intel processors are usually pretty crap, so what can you expect? :D

When a decades old Amiga running at 8MHz odd, multi-tasks better than a Pentium IV running at multi-GHz it really shows up the limitations of Intel processors.
 
Nigel Goodwin said:
When a decades old Amiga running at 8MHz odd, multi-tasks better than a Pentium IV running at multi-GHz it really shows up the limitations of Intel processors.
I blame Bill Gates' crappy operating system rather than the CPU architecture.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top