简体   繁体   English

MSP430 SWAP字节说明程序集

[英]MSP430 SWAP bytes explanation assembly

When we have a code like this : 当我们有这样的代码时:

main:   MOV     #SFE(CSTACK), SP        ; set up stack
     ;;; some instructions .......
    ; load the starting address of the array1 into the register R4
    MOV.W   #arr1, R4               
    ; load the starting address of the array1 into the register R5
    MOV.W   #arr2, R5 
;       Sum arr1 and display
        CLR     R7                      ; Holds the sum
        MOV     #8, R10                 ; number of elements in arr1
lnext1: ADD     @R4+, R7                ; get next element
        DEC     R10
        JNZ     lnext1
        MOV.B   R7, P1OUT               ; display sum of arr1
        SWPB    R7
        MOV.B   R7, P2OUT

What is the reason/meaning behind doing SWPB R7 in this example? 在此示例中执行SWPB R7的原因/含义是什么? I read the docs and understand that it exchanges low/high end bytes; 我阅读了文档,并了解它可以交换低/高端字节; in some docs it says it multiplies by 256. Is that the only reason or am I missing something deeper here? 在某些文档中它表示乘以256。这是唯一的原因,还是我在这里缺少更深的东西? The code supposed to add the elements of a register. 该代码应添加寄存器的元素。

MOV.B can access only the lower byte. MOV.B只能访问低字节。 So to be able to copy the upper byte somewhere else, it must be moved to the lower byte first. 因此,要能够将高位字节复制到其他位置,必须先将其移至低位字节。 (That the previous lower byte is in the upper byte after the swap is an unimportant side effect.) (交换之后,前一个低字节在高字节中是不重要的副作用。)

There would be other, less efficient mechanisms to get at the upper byte, such as shifting the register right eight times: 还有其他效率较低的机制来获取高字节,例如将寄存器右移八次:

    MOV.B R7, P1OUT
    RRA R7
    RRA R7
    RRA R7
    RRA R7
    RRA R7
    RRA R7
    RRA R7
    RRA R7
    MOV.B R7, P2OUT

Or storing the 16-bit value into a temporary variable, and then accessing the two bytes of that variable directly: 或将16位值存储到一个临时变量中,然后直接访问该变量的两个字节:

    MOV.W R7, temp_low     ; writes both bytes
    MOV.B temp_low, P1OUT
    MOV.B temp_high, P2OUT

    .bss
    .align 2
temp_low:  .space 1
temp_high: .space 1

With newer MSP430 families, the port registers are arranged so that you can access two ports with a single 16-bit access: 对于较新的MSP430系列,安排了端口寄存器,以便您可以通过单个16位访问来访问两个端口:

    MOV.W R7, PAOUT

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

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