Mike Harris Posted May 17, 2019 Share Posted May 17, 2019 (edited) Can someone post some code that would check if greater than in Z80 Assembly?I know CP if I want to check if say A = ? but how do you find out if a number is > or <. I looked it up on google and I get no examples. BOB: EQU 100 LD A, BOB CP A, 100 JP Z, YAKITY If BOB was greater than 100 without CP 101, 102, 103 and so on. And equally if BOB Was less than 100 UPDATE...Figured a solution but I welcome alternatives. Maybe there is a better way. Edited May 17, 2019 by Mike Harris Quote Link to comment Share on other sites More sharing options...
+nanochess Posted May 17, 2019 Share Posted May 17, 2019 CP 100 JP NC,GREATEROREQUAL ; HERE IF LESS THAN CP 100 JP C,LESSTHAN ; HERE IF GREATER OR EQUAL 3 Quote Link to comment Share on other sites More sharing options...
Mike Harris Posted May 17, 2019 Author Share Posted May 17, 2019 (edited) CP 100 JP NC,GREATEROREQUAL ; HERE IF LESS THAN CP 100 JP C,LESSTHAN ; HERE IF GREATER OR EQUAL too insane it is that easy Edited May 17, 2019 by Mike Harris Quote Link to comment Share on other sites More sharing options...
artrag Posted May 17, 2019 Share Posted May 17, 2019 (edited) If A and the value to be compared, say B, have sign (i.e. can be negative and are restricted to -128,127) the conditions to test is m (A<B) or p (A>=B). Note that they are supported by JP and not by JR. May be an example may help LD A,-1 CP 1 JP m,isless ; true as A<1 Last condition is true, but note that here C is reset, and the test is interpreted as: LD A,255 ; same as - 1 in binary CP 1 JP NC,isbiggerorequal ; true as 255>1 Edited May 17, 2019 by artrag Quote Link to comment Share on other sites More sharing options...
carlsson Posted May 17, 2019 Share Posted May 17, 2019 For that matter, checking the carry flag is how you usually determine if something is smaller/larger in any contemporary architecture, let it be Z80, 6502 or anything else. The amount of flags might differ but generally Z for zero and C for carry use to exist. Quote Link to comment Share on other sites More sharing options...
artrag Posted May 17, 2019 Share Posted May 17, 2019 For that matter, checking the carry flag is how you usually determine if something is smaller/larger in any contemporary architecture, let it be Z80, 6502 or anything else. The amount of flags might differ but generally Z for zero and C for carry use to exist.True but if you do not deal with signs you will incurr in errors. The Z80 has the S flag that is a copy of the highest bit of the result and can be tested by m and p conditions. 1 Quote Link to comment Share on other sites More sharing options...
+nanochess Posted May 17, 2019 Share Posted May 17, 2019 Let us just remind that in 6502 the comparison sense is reversed. C is set when greater than equal, and clear when less than. Also in Z80 the usage of S for signed comparison only works with numbers between -64 and 63, otherwise it will fail miserably. If you want to do proper signed comparison you should do this: xor $80 ; value to be compared cp $e4 ; $64 xor $80 jp c,lessthan ; here greater than or equal Quote Link to comment Share on other sites More sharing options...
artrag Posted May 17, 2019 Share Posted May 17, 2019 Let us just remind that in 6502 the comparison sense is reversed. C is set when greater than equal, and clear when less than. Also in Z80 the usage of S for signed comparison only works with numbers between -64 and 63, otherwise it will fail miserably. If you want to do proper signed comparison you should do this: xor $80 ; value to be compared cp $e4 ; $64 xor $80 jp c,lessthan ; here greater than or equal True, I should have added that one should test both S and Overflow for full range comparisons http://www.z80.info/zaks.html Quote Link to comment Share on other sites More sharing options...
ChildOfCv Posted May 17, 2019 Share Posted May 17, 2019 It typically depends on how the ALU works. Intel and Z80 set the C flag for either carry or borrow. The PIC and 6500 lines set carry on add if it overflows, but set carry on subtract when there is NO borrow. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.