【发布时间】:2021-02-04 12:40:41
【问题描述】:
例如,从位置 1900H 左移 1 位并存储在位置 1901H 中,空位位置用零填充
【问题讨论】:
标签: assembly bit-shift microprocessors z80
例如,从位置 1900H 左移 1 位并存储在位置 1901H 中,空位位置用零填充
【问题讨论】:
标签: assembly bit-shift microprocessors z80
家庭作业?
不是 100% 确定这是否是您要求的,但无论如何:
...
XOR A ; clear Carry
LD A,(1900H)
RLA ; left shift, bit 7 goes to Carry, bit 0 filled with 0
LD (1900H),A ; save shifted value
LD A,(1901H)
RLA ; left shift, bit 0 filled with bit 7 of the original (1900h)
LD (1901H),A ; save shifted value
...
当然,还有其他方法可以达到同样的效果。
从评论中添加了另一个:
LD HL,(1900H) ; load 16-bit value: L<-(1900H), H<-(1901H)
ADD HL,HL ; HL <- HL+HL = HL*2 - equivalent of left shift by 1 bit
LD (1900H),HL ; save to the same location
上面的 sn-p 大小为 7 个字节,并且可能是该任务的最短代码。
【讨论】:
RLA 来避免 XOR A 吗? x += x 相当于左移 1(移入零),当使用加法指令完成时,应将进位放入进位标志。我不太了解 Z80,但我认为它有 A+=A 指令。
让我们从左移一个字节的简单情况开始,然后
移入 0 位。在 Z80 上,这完美地描述了
sla 指令集:
sla [reg8]
其中reg8 是寄存器 A/B/C/D/E/H/L 或 (HL) 或 (IX+d) 之一
或 (IY+d)。最高位被移出寄存器(或
(HL)/(IX+d)/(IY+d)) 的情况下的内存位置并进入
携带旗帜。
对于 Z80 的 4 种特殊情况,有一个众所周知的技巧。
由于左移(同时移入 0)与
乘以 2,您实际上可以模拟 sla:
add a,a ; replaces `sla a`, save 1 byte, 4cc
add hl,hl ; replaces `sla l \ rl h`, save 3 bytes, 5cc
add ix,ix ; (to my knowedge, this is the only "legal" way to shift the IX register pair like this)
add iy,iy ; (same with IY)
这些更快,更小,几乎总是最好的 替换。
无论如何,移出的位现在在进位
标志,因此您可以使用 rl 将其转移到下一个字节
指令类:
rl [reg8] ;2 bytes, 8 clock cycles
rla ;1b, 4cc. Basically a fast `rl a` but affects fewer flags.
请注意,Z80 专门对此进行了优化 用于 A 寄存器。
现在回答您的问题:在 Z80。如果您确切知道字节在内存中的位置,@tum_ 提供了最快和最小的选项:
ld hl,(1900H) ; Read the two bytes into HL
add hl,hl ; Shift left
ld (1900H),hl ; Store HL back to the bytes.
这利用了之前的 sla 技巧,您可以在其中
只需将 HL 添加到自身 (HL*2) 即可将其向左移动。然而,如果
字节不在预定位置,但 HL 指向
字节,然后:
sla (hl) ; `sla` the byte at HL
inc hl ; Advance to the next byte
rl (hl) ; `rl` the byte at HL
你也可以慢一点(不推荐,只是显示 出于教育目的):
ld a,(hl) ; read the byte at HL into A
add a,a ; `sla a`, but faster and smaller
ld (hl),a ; Store it back to the byte at HL
inc hl ; advance to the next byte
ld a,(hl) ; read the byte at HL into A
rla ; `rl a`, but faster and smaller
ld (hl),a ; Store it back to the byte at HL
这是很多信息,但希望这会有所帮助!
【讨论】: