简体   繁体   English

MSP430中的JL指令

[英]JL instruction in MSP430

Given the code in MSP430: 鉴于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. 告诉我执行后R5的值为4而不是0。

Is this something specific to JL instruction? 这是JL指令特有的吗?

JL is " Jump if less than ". JL为“ 如果小于跳转 ”。

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. CMP指令可以设定这些作为执行的结果b - aR5 - 0在这种情况下) - CMP #0, R5是简单地测试R5的值的一个方式。

The CMP and JL together mean IF R5 < 0 GOTO L1 . CMP和JL一起表示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. 由于已将其设置为5,并将其递减为4,因此它将不小于零,因此JL不会分支。

Perhaps JNZ was intended (" Jump if not zero "), or its synonym JNE (" Jump if not equal "). 可能是JNZ的意图(“ 如果不为零则跳转 ”),或者其同义词JNE(“ 如果不等于则为跳转 ”)。

     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. 还要注意,DEC指令也设置了N和V标志,因此CMP实际上是不必要的。

     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

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. 在所有两个操作数的MSP指令中,第一个操作数是源,第二个操作数是目标。 This convention is more readable for all the other instructions, but for CMP, it means that the second operand is compared against the first. 对于所有其他指令,此约定更具可读性,但对于CMP,则意味着将第二个操作数与第一个操作数进行比较。

So CMP #0, R5 , JL checks if R5 is less than zero (which is not the case). 因此CMP #0, R5JL检查R5是否小于零(不是这种情况)。

To ensure that R5 is zero after the loop, jump when R5 is not equal to zero, ie, JNE . 为了确保循环后R5为零,请在R5不等于零(即JNE (And CMP #0, x is the same as TST x .) (并且CMP #0, xTST x相同。)

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 正如克利福德指出,如果V xor N为1,则JL表示跳跃

0x0005 - 0x0000 0x0005-0x0000

subtraction is done with addition, invert and add one 减法是通过加法,求反和加法完成的

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

N is 0, V is 0 N是0,V是0

N xor V is 0 xor 0 which is 0 N xor V为0 xor 0为0

JL should not branch. JL不应分支。

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 N为0,V为0,Z为0,并且进位取决于体系结构,可以将其保持为C = 1,也可以将其求反,因为这是减法并认为是借位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) 从注释中听起来,您希望它退出R5 = 0的循环,所以我们知道R5大于0的标志是什么(我们可以假设)

0x0000 - 0x0000 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 V是0,N是0,Z现在是1,C是0或1,具体取决于架构

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. 最容易使用的一个是如果不为零则跳转(如果未设置z标志则为跳转),这也不会有大于或小于您对于体系结构和文档了解的问题对于无符号数学或有符号数学,大于助记符小于助记符会有所不同。 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). 例如,如果您看一下arm,您会看到它从已签名的代码中调用了未签名的符号(同一指令的两个助记符/名称)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM