MSP430 SWAP 字节解释汇编
MSP430 SWAP bytes explanation assembly
当我们有这样的代码时:
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 背后的 reason/meaning 是什么?我阅读了文档并了解到它交换 low/high 结束字节;在一些文档中它说它乘以 256。这是唯一的原因还是我在这里遗漏了更深层次的东西?代码应该添加寄存器的元素。
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
当我们有这样的代码时:
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 背后的 reason/meaning 是什么?我阅读了文档并了解到它交换 low/high 结束字节;在一些文档中它说它乘以 256。这是唯一的原因还是我在这里遗漏了更深层次的东西?代码应该添加寄存器的元素。
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