user629034
user629034

Reputation: 669

JL instruction in MSP430

Given the code in MSP430:

     CLR  R6
     MOV  #5, R5                                  
L1:  DEC  R5
     CMP  #0, R5
     JL   L1
     INC  R6

I was told the value of R5 after execution is 4 and not 0.

Is this something specific to JL instruction?

Upvotes: 1

Views: 1325

Answers (3)

Clifford
Clifford

Reputation: 93476

JL is "Jump if less than".

From the instruction set:

JL : Jump to Label if (N .XOR. V) = 1 

So the jump occurs only if either the negative or overflow flag (but not both) are set.

The CMP instruction can set either of these as a result of performing b - a (R5 - 0 in this case) - CMP #0, R5 is simply a way of testing the value of R5.

The CMP and JL together mean IF R5 < 0 GOTO L1.

Since you have set it to 5, and decremented it to 4, it will not be less than zero, so the JL does not branch.

Perhaps JNZ was intended ("Jump if not zero"), or its synonym JNE ("Jump if not equal").

     CLR  R6      ; R6 = 0
     MOV  #5, R5  ; R5 = 5                        
L1:  DEC  R5      ; R5 = R5 - 1

     CMP  #0, R5  ; if R5 - 0 ...
     JL   L1      ; ... is less than zero ... <-- This is never true
                  ; ... then goto L1 

     INC  R6      ; R6 = R6 + 1
                  ; Here R5 = 4, R6 = 1

Note also the the DEC instruction also sets the N and V flags, so the CMP is in fact unnecessary.

     CLR  R6      ; R6 = 0
     MOV  #5, R5  ; R5 = 5                        
L1:  DEC  R5      ; R5 = R5 - 1

     JNZ  L1      ; if R5 is not zero ... 
                  ; ... then goto L1   <-- This will loop until R5 == zero

     INC  R6      ; R6 = R6 + 1
                  ; Here R5 = 0, R6 = 1

Upvotes: 3

old_timer
old_timer

Reputation: 71546

This is I consider the important point missing from the other answers. As Clifford points out JL means jump if V xor N is 1

0x0005 - 0x0000

subtraction is done with addition, invert and add one

11111111111111111
 0000000000000101
+1111111111111111
===================
 0000000000000101

N is 0, V is 0

N xor V is 0 xor 0 which is 0

JL should not branch.

N is 0, V is 0, Z is 0 and the carry out depends on the architecture it may keep it as is C = 1 or it may invert it because this is a subtract and consider it a borrow C = 0

From a comment it sounds like you want this to exit the loop with R5 = 0 so we know what the flags will be for the R5 is greater than 0 (well we can assume)

0x0000 - 0x0000

   11111111111111111
    0000000000000000
 +  1111111111111111
====================
    0000000000000000

V is 0, N is 0, Z is now a 1, and C is 0 or 1 depending on the architecture

The easiest one to use is jump if not zero (jump if the z flag is not set), this also does not have any of the issue that greater than or less than have as you have to understand for the architecture and from the documentation if greater than less than mnemonics are for unsigned math or signed math as it makes a difference. if you look at arm for example you will see it call out the unsigned from the signed (two mnemonics/names for the same instruction).

Upvotes: 0

CL.
CL.

Reputation: 180070

See this example from the manual:

CMP  @R7,R6   ; Is R6 < @R7?
JL   Label5   ; Yes, go to Label5
...           ; No, continue here

In all two-operand MSP instructions, the first operand is the source, and the second one is the destination. This convention is more readable for all the other instructions, but for CMP, it means that the second operand is compared against the first.

So CMP #0, R5, JL checks if R5 is less than zero (which is not the case).

To ensure that R5 is zero after the loop, jump when R5 is not equal to zero, i.e., JNE. (And CMP #0, x is the same as TST x.)

Upvotes: 0

Related Questions