Sie sind auf Seite 1von 1

CECS 285

Computer Organization & Assembly Language

2010 R. W. Allison

ExtraSecretson8051InstructionsLessThanandGreaterThanComparison
Often it is necessary not merely to check whether a register is or isnt equal to a certain value, but to determine whether a register is less than or greater than another register or value. The designers of the 8051 instruction set architecture were aware of this need and built in the ability for the programmer to do thishowever there are not explicit instructions for less than or greater than conditional jumps. As it turns out, the CJNE instruction, in combination with the carry flag, provides a way to accomplish this. When the CJNE instruction is executed, not only does it compare parameter1 to parameter2 and branch if they are not equal, it also sets or clears the carry flag based upon which parameter is greater or less than the other (thank you very much!): this is shown below:

If parameter1 < parameter2 If parameter 1 >= parameter2

CY 1 CY 0

This secret aspect of CJNE is how an 8051 assembly language program can perform a greater than/less than test for decision making purposes. For example, if the accumulator holds some value and we want to know if the value is less than or greater than 70h, the following code snippet could be employed: cjne ljmp A, #70h, Check_for_lessthan ;If A=70 then jump to A_was_equal_to_70h ; take care of that case

Check_for_lessthan: jc A_was_lessthan_70h A_was_greaterthan_70h: . . . . . . .

;If C=1, then jump to A < 70h code

;Here because A > 70h, so do ...

The code above first compares the accumulator to 70h. If they are equal to each other, the program will fall through to the next line and jump to a section of code that handles that case (i.e. A_was_equal_to_70h). If the two operands are not the same, we jump to the instruction at the Check_for_lessthan label to continue testing for less than. If the carry bit is set at that point, then A was less than 70h, and we jump to a section of code that handles that case (i.e. A_was_lessthan_70h). However, if the carry bit wasnt set, execution will fall through to the A_was_greaterthan_70h label where there would be code to handle that case. It is well worth emphasizing that the CJNE clears the carry flag if parameter1 is greater than or equal to parameter2, hence it is very important to check to see if the values are equal before using the carry bit to determine lessthan/greater than. Otherwise, the program may branch to a greater than label when, in fact, the two parameters are equal!

ConditionalJumponOverfloworNotOverflow
Although there is not an explicit conditional jump based on the overflow flag (set/cleared by arithmetic instructions), the 8051 designers did give us an even more powerful pair of conditional jump instructions: the JB and JNB, which can do a conditional test on any 8051 bit that is accessible. Since the overflow flag (OV) is in the 8051 PSW register (i.e. PSW.2), we simply perform
jb OV, It_was_overflow . . . . . jnb OV, It_wasnt_Overflow . . . . . .
CECS 285 Extra Code Tips - Page 1

Das könnte Ihnen auch gefallen