# 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

##### Share on other sites

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

##### Share on other sites
```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

##### 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

##### 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.

##### 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.

##### 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
```

##### 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

##### Share on other sites

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.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

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