Build Z80: How to add a signed 8-bit value to a 16-bit register?

I wrote a Z80 disassembler that starts from ROM in my SBC. One of the last things I need to do (as yet unplanned errors aside) is to translate relative addresses and output them as absolute addresses, so when the disassembler falls into one of the five variants of the JR code, it displays the absolute address Code JR operation points to.

JR code variants use an 8-bit offset value to tell the Z80 where the memory needs to go to. The offset is a single, signed (-128 ↔ 127) byte, which I need to add to the current memory location in the HL register in order to get the absolute address.

My brain seems to be suffering from a serious syntax error, although maybe even dividing by zero, since I can't get my life working on how to add an 8-bit signed (or 2 padding) byte to a 16-bit register for getting an absolute address. They were looking for interruptions, and no answers were received.

Can someone suggest a solution or point me in the right direction?

+4
source share
1 answer

The easiest way is to sign the extension of the 8-bit value to 16 bits, and then use the 16-bit addition. Here is the code that does this. Ais an 8-bit signed value, and HLis a 16-bit base address to which an 8-digit signed value will be added. Result inHL

   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

, JR , JR.

+10

Source: https://habr.com/ru/post/1694356/


All Articles