1

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?

CL.
  • 173,858
  • 17
  • 217
  • 259
user629034
  • 659
  • 2
  • 11
  • 30
  • 2
    How does the CMP instruction set the status bits? What action does JL take on these bits? Did you read the JL entry in the manual? What exactly is it that you do not understand? – CL. Mar 07 '18 at 17:50
  • I don't understand why the JL fails after the first decrement. My understanding is that after the first decrement, it would continue until R5 is 0. But I was told it does not jump to L1 after the first decrement. – user629034 Mar 07 '18 at 18:16

3 Answers3

3

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
Clifford
  • 88,407
  • 13
  • 85
  • 165
0

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.)

CL.
  • 173,858
  • 17
  • 217
  • 259
0

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).

old_timer
  • 69,149
  • 8
  • 89
  • 168
  • when programming in assembly the documentation doesnt always make it clear which way the subtraction is, nor do they always document the flags being tested, and certainly just blindly choosing jump if greater, jump if less than, etc, the odds of it being the less than you were thinking are pretty low 25%. signed vs unsigned a - b or b - a only one of the combinations is what you wanted the other 75% are combinations that wont work for you. so do your research, and stop and do a custom experiment if you feel you need to use those mnemonics (less than or greater than variations). – old_timer Mar 07 '18 at 20:38