MSP430 SWAP字节说明汇编

时间:2017-09-11 02:49:45

标签: assembly bit-manipulation msp430 instruction-set

当我们有这样的代码时:

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

在此示例中执行SWPB R7的原因是什么?我阅读了文档,并了解它交换低/高端字节;在一些文档中它说它乘以256.这是唯一的原因还是我错过了更深层次的东西?该代码应该添加寄存器的元素。

1 个答案:

答案 0 :(得分:2)

MOV.B只能访问低位字节。 因此,为了能够在其他地方复制高位字节,必须先将其移动到低位字节。 (前一个低位字节在交换后的高位字节中是一个不重要的副作用。)

还有其他效率较低的机制来获取高位字节,例如将寄存器右移八次:

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

或者将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

对于较新的MSP430系列,端口寄存器的排列方式使您可以通过一个16位访问访问两个端口:

    MOV.W R7, PAOUT