In my 8080 assembly project I need to check if user entered digits and if so do some stuff on it, otherwise display some error and close app.
Normally i would do it like that
CPI '0';if below '0'
JS ERR
CPI 58D;if over '9'
JNS ERR
but there's no JN and JNS in 8080 instructions, So how can I accomplish this?
Best Answer
CPI compares an immediate value with the accumulator, by internally using a subtract operation:
(Some notes on usage here: I am using I for the Immediate value, and A for the Accumulator).
So you are looking for the carry bit to be 1 (I > A) for the first ERR branch. That can be achieved with JC (Jump if Carry).
The second CPI wants to see if I < A, which using the same method as above makes things a little tricky. You need to know that the Carry bit is not set (I <= A) and the Zero bit is not set, which gets a little tricky.
JNC performs a Jump if Not Carry, and JZ performs a Jump if Zero. So you want to combine those.
Firstly you know that the Zero bit must not be set under any circumstances. That bit being set would indicate the two values are the same, which is valid. So you can first check that bit to rule it out. Z being set means its a valid entry ('9') regardless of the state of the carry:
And then if the Carry bit is not set you can do a JNC to your error routine:
And finally give it somewhere for the GOOD label t jump to:
So to wrap it all up:
So remember, when doing a CPI you can test for the three things - exactly equal to (Z = 1), Less than (C = 1), and Greater Than or Equal To (C = 0).
If you want to lose some readability, but save a byte or two of program space, you can instead of comparing your upper bound with '9', compare it with '9'+1, which is ':'. That way you're only really interested in the "less than" portion of the CPI, and all others go to ERR: