multiply a variable x 2

Status
Not open for further replies.

savnik

Member
I have a variable which is byte and change from 0-255
and the result show on LCD.
I want to multiply the variable x 2 , therefore will change from 0-512 (now is word) and to show on Lcd.
 
Final i change the code and show 0-512, but because i measure volts with adc on porta.0 , i want to show on lcd 0-500.
 
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:
Hero999 said:
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.
I use 16f88
 
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
 

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?

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.
 
Hero999 said:
I blame Bill Gates' crappy operating system rather than the CPU architecture.

Probably partially so, but the Intel CPU isn't really suited to multitasking, whereas the Motorola 68000 was.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…