DCX and Carry flag

885 Views Asked by At

I am learning 8080 assembly as part of a course that I am undertaking. I am referring to Intel 8080-8085 Assembly Language Programming manual (dated 1977).

In Chapter 3 Instruction Set of the manual, I see the following description in relation to DCX:

DCX decrements the contents of the specified register pair by one. DCX affects none of the condition flags.

The associated example says:

Assume that the H and L registers contain the address 9800H when the instruction DCX H is executed. DCX considers the contents of the two registers to be a single 16-bit value and therefore performs a borrow from the H register to produce the value 97FFH.

I tried the math on my own using two's complement addition and there is definitely a carry generated.

So my question is: Is carry bit set only in the case of arithmetic operations?

TIA

PV

2

There are 2 best solutions below

0
Alexey Frunze On BEST ANSWER

Setting the particular CPU aside and just considering binary arithmetic, no borrow is generated when subtracting 1 from 9800H. A carry is generated when adding 0FFFF to 9800H, though. In both cases you get 97FFH in the 16 least significant bits of the result.

Irrespective of the design choices put into the CPU you need to simply follow the documentation, e.g. this document, MCS®-80/85 FAMILY USER'S MANUAL.

It says in 5.6.1 Data Transfer Group:

Condition flags are not affected by any instruction in this group.

5.6.2 Arithmetic Group:

Unless indicated otherwise, all instructions in this group affect the Zero, Sign, Parity, Carry, and Auxiliary Carry flags according to the standard rules.

Similarly in 5.6.3 Logical Group:

Unless indicated otherwise, all instructions in this group affect the Zero, Sign, Parity, Auxiliary Carry, and Carry flags according to the standard rules.

5.6.4 Branch Group:

Condition flags are not affected by any instruction in this group.

5.6.5 Stack, I/O, and Machine Control Group:

Unless otherwise specified, condition flags are not affected by any Instructions in this group.

You need to memorize how common instructions affect flags. You may compile a simple cheat sheet or, perhaps, find one made by someone else (some assembly books had those for the programmer's convenience).

If you're interested in why some instructions don't affect flags or some affect them in a particular way, it depends. The reasons can be different, depending on the particular instruction: cheaper circuitry, easier to program common problems, compatibility with earlier designs or just simply carrying forward what had worked well without giving it much additional thought.

0
Ivan Kosarev On

DCX H is certainly an arithmetic instruction, but 16-bit increment and decrement instructions do not change any flags on i8080, even though 8-bit ones, such as DCR H and DCR L, do. You can find further details on this in the Intel 8080 Microcomputer Systems Users Manual, available here:

http://www.nj7p.info/Manuals/PDFs/Intel/9800153B.pdf

Also, looking at internals of Z80 emulators may help. Here's the relevant snippets to compare from my implementation:

    void on_dec_rp(regp rp) {
        self().on_set_regp(rp, dec16(self().on_get_regp(rp)));
    }

DCX rp

    void on_dec_r(reg r) {
        fast_u8 n = self().on_get_reg(r);
        fast_u8 f = self().on_get_f();
        fast_u8 hf = (n & 0xf) > 0 ? hf_mask : 0;
        n = dec8(n);
        f = (f & (cf_mask | yf_mask | xf_mask | nf_mask)) |
                (n & sf_mask) | zf_ari(n) | hf | pf_log(n);
        self().on_set_reg(r, n);
        self().on_set_f(f);
    }

DCR r