I want test the variable sign. In other words, I want know answer to the whether a variable is positive or negative. How can I write following if-then-else conditions in assembly language ?
if X > 0
goto A
else
goto B
I am using PİC16f87x
To do my question to be more general, if you know can you give answer to below questions?
i.)
if X > Y
goto A
else
goto B
ii.)
if X >= Y
goto A
else
goto B
iii.)
if X != Y
goto A
else
goto B
Best Answer
I'm not acquainted with the PIC microcontroller, so I'll give a more general answer. (edit: PIC-specific answer added at the bottom of this post)
Especially smaller microcontrollers, like 8-bit and their derivatives, are limited in what they can do in a single instruction. An instruction may contain the target address for a jump, but not two of them, so
then-else
is out. You only have theif-then
part, but that's enough. There are two approaches. Some controllers let you jump to a given address if a condition is met, others only let you skip the next instruction. In the former case your code will look as follows:If you can only skip the next instruction you'll write something like
The test itself has also limited possibilities. Like you can't pass two numbers to compare them. What you do is load the accumulator with the first number and in the next instruction subtract the second number. This will cause condition codes like the zero and carry flags to be set/cleared. Conditional instructions will test for those flags. So if you want to write
if A = B then do-equal else do-not-equal
this would becomeImportant: the instruction set manual will tell you which condition codes will be affected by a given instruction. For instance, in the Z80 the
ld
instruction (for "load accumulator") will not change any flags. So here loading the accumulator is not sufficient to determine if the data is zero.edit
OK, so I did some research and I found the following:
The PIC has only 2 conditional jump instructions,
BTFSS
andBTFSC
.BTFSS: Bit Test F, Skip if Set
Syntax: BTFSS f,b
where f is a register [0..127]
and b is the bit in that register to be tested [0..7]
Description: If bit in register is
0
the next instruction is executed. If the bit is1
the next instruction is discarded and aNOP
is executed instead.BTFSC: Bit Test F, Skip if Clear
Syntax: BTFSC f,b
where f is a register [0..127]
and b is the bit in that register to be tested [0..7]
Description: If bit in register is
1
the next instruction is executed. If the bit is0
the next instruction is discarded and aNOP
is executed instead.