Jump to content
IGNORED

Z80 MATH?


Recommended Posts

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

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
Link to comment
Share on other sites

 

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

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...