News:

Printed Amstrad Addict magazine announced, check it out here!

Main Menu

CP A

Started by kelp7, 07:12, 11 May 15

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

kelp7

Hi,


Just wondering what the purpose of the "CP A" Z80 command would be? I guess I am probably missing something. Perhaps it's something to do with wanting to set flags to a certain known state?

Joseman

#1
Totally right

a lot of programs do "oscure" instructions just to set the flags to a way they want in the program...

it's like' XOR a' to set a=0 instead of doing ld a,0


moreover i think it's an optimized way to do this way instead of use set, res, bit or doing any push pop thing... but this affirmation it woult be better that people more experienced than me, confirm this...

MaV

#2
CP A just clears sets the zero flag.

CP performs a subtraction of the stated register from A (as an implicit parameter) without saving the result, thus preserving the content of registers. Since A-A always results in zero, the zero flag is set.

http://www.ticalc.org/archives/files/ss/814/81455.gif

Not every instruction does make sense at first glance, namely mostly those that use the A register implicitly and having A as a parameter. Most of these then serve a special purpose, as is the case with CP A.

In the table above there's a regularity in rows 4 to b. The corresponding columns of one instruction show the registers of the first parameter in this sequence b, c, d, e, h, l, (hl), a, which is then repeated in the same row, so there's a pattern on the chip die for this, which is an indication that it was cheaper to leave instructions like XOR A and the like just as they are. They could have renamed some of them like CP A to CLZ (clear zero flag), but it would have obfuscated the pattern.

I hope that this makes sense to you.

(edited to correct the mistake in the first sentence.)
Black Mesa Transit Announcement System:
"Work safe, work smart. Your future depends on it."

Ast

A best way would be to use ´or a' instead of ´cp a ´ which did the same thing faster.

_____________________

Ast/iMP4CT. "By the power of Grayskull, i've the power"

http://amstradplus.forumforever.com/index.php
http://impdos.wikidot.com/
http://impdraw.wikidot.com/

All friends are welcome !

MaV

Quote from: Ast on 10:06, 11 May 15
A best way would be to use ´or a' instead of ´cp a ´ which did the same thing faster.
Uhm, they both take 4 cycles or 1 µs.
Black Mesa Transit Announcement System:
"Work safe, work smart. Your future depends on it."

Ast

It's clean like that... Effectively both take the same time
_____________________

Ast/iMP4CT. "By the power of Grayskull, i've the power"

http://amstradplus.forumforever.com/index.php
http://impdos.wikidot.com/
http://impdraw.wikidot.com/

All friends are welcome !

TFM

Quote from: MaV on 09:33, 11 May 15
CP A just clears the zero flag.


Actually... it sets the Z Flag.  ;)  And it resets the Carry.  :)
TFM of FutureSoft
Also visit the CPC and Plus users favorite OS: FutureOS - The Revolution on CPC6128 and 6128Plus

ronaldo

Quote from: TFM on 17:15, 11 May 15
Actually... it sets the Z Flag.  ;)  And it resets the Carry.  :)
On the contrary, OR A only sets Z flag when A contains a 0 (CP A always sets Z flag), and also resets the carry.

They take the same ammount of time, but serve different purposes ;)

TFM

Quote from: ronaldo on 17:21, 11 May 15
On the contrary, OR A only sets Z flag when A contains a 0 (CP A always sets Z flag), and also resets the carry.

They take the same ammount of time, but serve different purposes ;)


That's exactly what I said. I never talked about the OR command!
TFM of FutureSoft
Also visit the CPC and Plus users favorite OS: FutureOS - The Revolution on CPC6128 and 6128Plus

ronaldo

Quote from: TFM on 17:23, 11 May 15
That's exactly what I said. I never talked about the OR command!
I know, man. Was just trying to clarify  the difference with OR, not to confuse @kelp7 more! (May be I just added more confusion  :( ).

Nothing to object to your comment  ;)

MaV

Quote from: TFM on 17:15, 11 May 15Actually... it sets the Z Flag.  ;)  And it resets the Carry.  :)
Yep, thanks. A zero result of an operation SETS the zero flag, of course. I was answering at work and get distracted too often there.
Black Mesa Transit Announcement System:
"Work safe, work smart. Your future depends on it."

TFM

Quote from: MaV on 17:37, 11 May 15
Yep, thanks. A zero result of an operation SETS the zero flag, of course. I was answering at work and get distracted too often there.


Ah, I confuse that all the time since the Zero result actually set the Zero to '1'. So 0 = 1, haha!  :laugh:
TFM of FutureSoft
Also visit the CPC and Plus users favorite OS: FutureOS - The Revolution on CPC6128 and 6128Plus

kelp7

Thanks everyone :) I wasn't too far off with what I originally thought then.... thanks for the extra details. Did seem a bit confusing to begin with (why bother comparing A with A, heh...) but now I understand.

MaV

Quote from: TFM on 19:13, 11 May 15Ah, I confuse that all the time since the Zero result actually set the Zero to '1'. So 0 = 1, haha!  :laugh:
And would it not be more reasonable to clear the zero flag when the result is zero? But my guess is that it is cheaper to implement it  with transistors the way it is now.

Interestingly enough, in the second paragraph I wrote "the zero flag is set". Go figure!
Black Mesa Transit Announcement System:
"Work safe, work smart. Your future depends on it."

TFM

#14
Haha! Guess Tron is at your side.  ;)
TFM of FutureSoft
Also visit the CPC and Plus users favorite OS: FutureOS - The Revolution on CPC6128 and 6128Plus

EgoTrip

Im shit at coding and even I see the point and logic of setting the Zero flag to 1 when the result is 0. 1 is true, 0 is false, so if the result is 0 then the Zero flag would be true, hence 1.

MaV

That has got nothing to do with good or bad coding.

Equalling 1 with true and 0 with false is a convention; a useful and elegant one, one that most probably won't be changed, but still a convention.

And there is nothing preventing a CPU designer from setting zero for the zero flag when an operation results in a zero. True and false do not have a meaning on this level.
You're not asking: is the zero flag set to one, you're asking if the result of the operation is zero regardless of how the processor saves the value.
We just know for a fact that the Z80 as other processors set a single bit to one for that; and you can test this by pushing and popping AF to another register and checking the bit pattern.

It's as much a convention as is 8 bit for a byte (there were computers with 6 bits per byte for example), or the width of an integer (16 bit on the Z80, 32 bit on the 386 and up, 64 bit on the more recent processors). Integer width is a prime example that such conventions can change over time. And this one still is a source of bugs, when programmers do not keep variable integer width in mind.

Black Mesa Transit Announcement System:
"Work safe, work smart. Your future depends on it."

TFM

Quote from: MaV on 23:41, 12 May 15
That has got nothing to do with good or bad coding.


Right, but this guy is just trolling (again). The best is to ignore him.  ;)
TFM of FutureSoft
Also visit the CPC and Plus users favorite OS: FutureOS - The Revolution on CPC6128 and 6128Plus

Powered by SMFPacks Menu Editor Mod