Jump to content

Photo

Z80 MATH?


8 replies to this topic

#1 Mike Harris OFFLINE  

Mike Harris

    Chopper Commander

  • 246 posts

Posted Thu May 16, 2019 6:47 PM

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 by Mike Harris, Thu May 16, 2019 7:38 PM.


#2 nanochess ONLINE  

nanochess

    Processorus Polyglotus

  • 5,898 posts
  • Coding something good
  • Location:Mexico City

Posted Thu May 16, 2019 9:10 PM

CP 100
JP NC,GREATEROREQUAL
; HERE IF LESS THAN
CP 100
JP C,LESSTHAN
; HERE IF GREATER OR EQUAL


#3 Mike Harris OFFLINE  

Mike Harris

    Chopper Commander

  • Topic Starter
  • 246 posts

Posted Thu May 16, 2019 9:40 PM

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 by Mike Harris, Thu May 16, 2019 9:48 PM.


#4 artrag ONLINE  

artrag

    Stargunner

  • 1,286 posts

Posted Fri May 17, 2019 12:45 AM

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 by artrag, Fri May 17, 2019 1:29 AM.


#5 carlsson ONLINE  

carlsson

    Metagalactic Mule

  • 8,997 posts
  • Location:Västerås, Sweden

Posted Fri May 17, 2019 1:34 AM

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.



#6 artrag ONLINE  

artrag

    Stargunner

  • 1,286 posts

Posted Fri May 17, 2019 1:42 AM

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.

#7 nanochess ONLINE  

nanochess

    Processorus Polyglotus

  • 5,898 posts
  • Coding something good
  • Location:Mexico City

Posted Fri May 17, 2019 9:25 AM

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


#8 artrag ONLINE  

artrag

    Stargunner

  • 1,286 posts

Posted Fri May 17, 2019 11:28 AM


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

#9 ChildOfCv OFFLINE  

ChildOfCv

    Moonsweeper

  • 404 posts

Posted Fri May 17, 2019 12:22 PM

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. 






0 user(s) are browsing this forum

0 members, 0 guests, 0 anonymous users