Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
11/05/06 23:17
Read: times


 
#127405 - 8051 Instructions
Responding to: ???'s previous message
Hi Abdullah,

Yes, you can do what you're describing in 8051 assembler code, but not by using the IF, THEN and ELSE operators. Your three possible conditions are that A > B, A < B and A = B. In the specific case you describe it only matters if A > B, in which case you set all of the Port 1 bits low, otherwise you set all of the Port 1 bits high. Still, lets consider each possible case separately.

First, since A and B are special register names, and since B isn't always usable, let's consider comparing the contents of R0 and R1 instead.

Begin by subtracting R1 from R0. Start by moving R0 into A, (MOV A, R0). Now do SUBB A, R1.

At this point the contents of A are either <0, =0 or >0. If A<0 then R0<R1. If A>0, then R0>R1. And finally if A=0 then R0=R1.

The first test is easy. Set up a block of code labeled R0EQR1 (R0 EQuals R1) and execute a JZ R0EQR1 instruction. This will test the contents of A, and if it is zero will jump to the label R0EQR1. There you will MOV P0, #0x0FF and jump unconditionally past the next two blocks of code, the blocks you will set up to handle the other two possible conditions (R0<R1 and R0>R1).

Immediately after you test A to find if it is zero, but before the block of code labeled R0EQR1, you will begin testing to see if the contents of A are >0 or <0. Of course, if it was zero this bit of code will not execute because you will have already jumped over it.

After JZ R0EQR1, insert an instruction to logically AND the accumulator with 0x080 (binary 1000 0000). The instruction to do this is ANL A, #0x080. Once you do this, you are guaranteed that B0 through B6 of the Accumulator are all zeroes. The only way that A is not equal to zero now is if B7 was a 1 to begin with. If you're familiar with 2s compliment arithmetic, you already understand that this means that the contents of A were negative (A<0) after your earlier subtraction, or that R0<R1 initially. If you're not familiar with 2s compliment arithmetic, take my word that B7 = 1 means the number was negative.

And of course, if R0>R1 initially, then A>0 after your earlier subtraction and the accumulator would be zero after your logical AND operation because B7 would have been a 0.

Thus, in the general case, you would set up two more blocks of code labeled R0LTR1 (R0 Less Than R1) and R0GTR1 (R0 Greater Than R1) after the block labeled R0EQR1. After the ANL A, #0x080 instruction, insert a JZ R0GTR1 followed by a JNZ R0LTR1.

Also, in the specific case of your question, you don't really need a third block of code labeled R0LTR1 since you're just going to do the same thing as in R0EQR1, so you could just jump there. In such a case, remember to specifically set the accumulator back to zero before the JZ CONTINUE instruction, otherwise in those cases where R0 < R1 you would still execute the R0GTR1 block of code too.

; Compare R0 and R1

          MOV  A, R0
          SUBB A, R1
          JZ R0EQR1

          ANL A, #0x080
          JZ R0GTR1
          JNZ R0LTR1

R0EQR1:   MOV P1, #0x0FF
          JZ CONTINUE

R0GTR1:   MOV P1, #0x000
          JZ CONTINUE

R0LTR1:   MOV P1, #0x0FF

CONTINUE: Here's where the rest of your code picks up.


As I said, this could be condensed for your specific case, but I hope it shows you in general how this can be done in assembler.

Good luck,

Joe

List of 12 messages in thread
TopicAuthorDate
compare registers            01/01/70 00:00      
   Bible            01/01/70 00:00      
      Andy, please, don't mislead him            01/01/70 00:00      
   this is not my homework !!!            01/01/70 00:00      
      directives are to control the behaviour...            01/01/70 00:00      
   8051 Instructions            01/01/70 00:00      
      nobody mentioned CJNE            01/01/70 00:00      
         Skinnin' cats            01/01/70 00:00      
            miaaaaauuuuuuuuuuw            01/01/70 00:00      
            does 0730 mean \"late late night\" ? :-)            01/01/70 00:00      
               Probably            01/01/70 00:00      
                  a better assembler will handle that            01/01/70 00:00      

Back to Subject List