我试图理解一些汇编代码,但我不明白一些值是如何找到的。如果代码:
ldi ZL, low(2*table)
ldi ZH, high(2*table)
与表一起执行:
table: .db 32, 34, 36, 37, 39, 41, 43, 45, 46, 48, 50, 52, 54, 55, 57, 59, 61, 63, 64, 66
我正在看表格的要素5,41。
在此示例中,LDI 运算符执行哪些操作?执行第一个 ldi 后,14 存储在 ZL 位置的 SRAM 中,但为什么是 14?
执行第二个后,02 存储在 ZH 位置。
闪存是"字寻址",汇编器标签table
以文字给出表的地址。* 2
以字节为单位提供正确的地址。我更喜欢使用<< 1
而不是* 2
因为对我来说,这让事情更清楚。
要实际访问表的第 5 个元素,您需要执行以下操作:
ldi ZL, low(table << 1) ; (or you could use `table * 2`)
ldi ZH, high(table << 1) ; Z Register points to start of table
ldi r25, 5 ; register r25 contains required offset in table
clr r1 ; (I always have r1 set to zero all the time)
add ZL, r25 ; add offset to base pointer
adc ZH, r1 ; if the low byte of the Z register "pointer"
; overflowed, add the carry flag to the high byte
lpm r24, Z ; read the 5th element of the table into r24
以下 SO 问题也涵盖了这一点:
- 在 AVR ASM 上使用 Z 寄存器访问程序存储器时,为什么要乘以 2
- Avr asm 标签*2
- 为什么这个数组指针移动了 1?