简体   繁体   English

Z80汇编:如何将带符号的8位值添加到16位寄存器?

[英]Z80 assembly: How to add signed 8-bit value to 16-bit register?

I've written a Z80 disassembler that runs from the ROM in my SBC. 我写了一个Z80反汇编程序,它从我的SBC中的ROM运行。 One of the last things I need to do (as-yet-unspotted bugs aside) is to translate relative addresses and output them as absolute addresses, so that when the disassembler hits one of the five JR opcode variants, it displays the absolute address the JR opcode is pointing to. 我需要做的最后一件事(暂时尚未发现的错误)是转换相对地址并将它们作为绝对地址输出,这样当反汇编程序遇到五个JR操作码变体之一时,它会显示绝对地址JR操作码指向。

The JR opcode variants use an 8-bit offset value to tell the Z80 where in memory to jump to. JR操作码变体使用8位偏移值来告诉Z80跳转到内存的哪个位置。 The offset is a single, signed (-128 <-> 127) byte which I need to add to the current memory location in the HL register to get the absolute address. 偏移量是单个有符号(-128 < - > 127)字节,我需要将其添加到HL寄存器中的当前存储器位置以获得绝对地址。

My brain seems to be suffering a serious syntax error though, perhaps even a division by zero, as I can't for the life of me work out how to add an 8-bit signed (or 2's complement) byte to a 16-bit register to get the absolute address. 我的大脑似乎正在遭受严重的语法错误,甚至可能被零除,因为我不能为我的生活找出如何将一个8位带符号(或2的补码)字节添加到16位注册以获取绝对地址。 Have searched the interweb and no answers are forthcoming. 已经搜索了互联网,没有答案即将到来。

Can anyone suggest a solution or point me in the right direction? 任何人都可以提出解决方案或指出我正确的方向吗?

Easiest way is to sign extend the 8 bit value to 16 bits and then use a 16 bit add. 最简单的方法是将8位值扩展为16位,然后使用16位加法。 Here's some code that does it. 这是一些代码。 A is the 8 bit signed value and HL is the 16 bit base address that the 8 bit signed value will be added to. A是8位有符号值, HL是将添加8位有符号值的16位基址。 Result in HL 导致HL

   LD  E,A
   ADD A,A      ; sign bit of A into carry
   SBC A,A      ; A = 0 if carry == 0, $FF otherwise
   LD  D,A      ; now DE is sign extended A
   ADD HL,DE

Also keep in mind that for JR the offset is relative to the address just after the instruction, not the address of the JR instruction itself. 还要记住,对于JR ,偏移量是相对于指令之后的地址,而不是JR指令本身的地址。

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

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