[A83] Re: check if positive


[Prev][Next][Index][Thread]

[A83] Re: check if positive




> From: "Maarten Blackhead" <m021085@hotmail.com>
>
> mainloop:
>    ld a,10 ; ld 10 into a
>    ld b,57 ; store 57 in register part b
>    sub b,a ; substract a from b and return value in b
>    xor a   ; accumulator minus accumulator
>    cp b    ; check if b is positive(???you can't compare with a if b
>            ; is positive???)
>    jr p,positive ; if positive jump to label positive
> negative:
>    call _homeup
>    ld hl, neg
>    call _puts
>    jr mainloop
> positive:
>    call _homeup
>    ld hl, pos
>    call _puts
>    jr mainloop
>    ret
>
> pos: ds. "Variable b is positive",0
> neg: ds. "Variable b is negative",0
>
> .end

>    sub b,a ; substract a from b and return value in b

First thing you have to know is that in Z80 assembly comparing, (8-bit)adding and (8-bit)subtracting is always done with the accumulator, which simply means that you compare with acc, add to acc and subtract from acc. So you can't do something like "sub b,a" which would mean you subtract from 'b'.

>    xor a   ; accumulator minus accumulator

Acc minus acc would mean you typed "sub a". "xor a" gives the same result but is something entirely different. 'xor' is a logical operator which gives 1 as result if both operands are different and 0 when they are the same. So if you xor acc with itself - all bits are the same - you get zero as result.

>    cp b    ; check if b is positive(???you can't compare with a if b
>            ; is positive???)

As I said, comparing is always done with acc, so "cp b" means you compare 'b' with 'a'. This command returns the same way as the "sub" command. (Actually subtracting can be called a destructive way of comparing.) The zero flag is set (z) when 'b' and 'a' were equal and not set (nz) when they weren't (how obvious :). The carry flag is set (c) when 'b' was greater than 'a' and not set (nc) when 'b' was smaller or equal to 'a'. You can also use (p) and (m) for this matter but since they aren't used that much - only when you use signed bytes - and the carry flag is quite sufficient. I prefer it anyway...

Tijl Coosemans

------------------------------------------------------------
Want a free mail at http://www.mail.be ?