Omnimaga

Calculator Community => TI Calculators => ASM => Topic started by: TiAddict on May 27, 2011, 02:00:51 pm

Title: Another question..
Post by: TiAddict on May 27, 2011, 02:00:51 pm
how do you compare number bigger than 256?
i know cp compares number to accumulator, but it can only hold up to 256.. so how? ???
Title: Re: Another question..
Post by: Ashbad on May 27, 2011, 02:06:10 pm
you can always do things such as subtracting DE from HL to compare 16 bit numbers and look at different flags.  Also, I suggest from now on, you rename resolved threads to something like "Assembly questions" rather than a whole new thread per small question ;)
Title: Re: Another question..
Post by: TiAddict on May 27, 2011, 02:07:19 pm
haha okay :D and thanks!
Title: Re: Another question..
Post by: Deep Toaster on May 27, 2011, 02:07:28 pm
Load the value to HL (since that's two bytes), load the second value to either DE or BC, reset the carry flag, and use SBC to subtract the values. For example,

Code: (Z80 ASM) [Select]
    LD HL,256    ; Two-byte number
    LD DE,257    ; Another two-byte number
    OR A    ; Reset carry flag
    SBC HL,DE    ; Subtraction with carry
    JR C,Carried    ; This will get executed because HL-DE is less than zero
Title: Re: Another question..
Post by: TiAddict on May 27, 2011, 02:21:35 pm
thank you! now i get it! haha :D
Title: Re: Another question..
Post by: SirCmpwn on May 27, 2011, 02:22:52 pm
Code: [Select]
CPDEBC:
    push de
    ex de, hl
    or a
    sbc hl, bc
    ex de, hl
    pop de
    ret
Preserves all registers, etc, and sets flags just like CP.
Title: Re: Another question..
Post by: TiAddict on May 27, 2011, 02:24:45 pm
oh yeah can you explain what's "or a" or "xor a" is? i dont know where to learn about them.
Title: Re: Another question..
Post by: Deep Toaster on May 27, 2011, 02:25:33 pm
Sorry don't use XOR A. Not sure what I was thinking :P

OR A does the binary OR comparison of A to itself. All you have to know about it is that it resets the zero and carry flags (that's pretty much all it's used for) :)
Title: Re: Another question..
Post by: TiAddict on May 27, 2011, 02:29:49 pm
ah okay. haha :) so or only compares to accumulator? can you do like or hl?
Title: Re: Another question..
Post by: SirCmpwn on May 27, 2011, 02:34:58 pm
oh yeah can you explain what's "or a" or "xor a" is? i dont know where to learn about them.
Those involve Bitwise Arithmetic.  It modifies numbers based on their binary values.
The following:
XOR B
Will apply XOR logic on A and B.  The logic is as follows, using registers A and B for examples:
Value of A: 11110101
Value of B: 10011011
AND
A AND B will preserve the ones in B and overwrite the zeros in A. (1 and 1 = 1, 0 and 0 = 0, 1 and 0 = 0)

    11110101 (A)
and 10011011 (B)
------------------
    10010001


OR
OR does the opposite: (1 or 0 = 1, 0 or 1 = 1, 0 or 0 = 0)
    11110101 (A)
or  10011011 (B)
------------------
    11111111


XOR
XOR toggles the bits in A that are 1 in B. (1 xor 0 = 1, 0 xor 1 = 1, 1 xor 1 = 0)
    11110101 (A)
xor 10011011 (B)
------------------
    01101110
Title: Re: Another question..
Post by: TiAddict on May 27, 2011, 02:48:04 pm
woahh! :D thats interesting. Thank you for that!   :thumbsup:
Title: Re: Another question..
Post by: SirCmpwn on May 27, 2011, 02:50:22 pm
Sure thing.  Make sure you look back over it again, there were about 10 edits <_<
Title: Re: Another question..
Post by: TiAddict on May 27, 2011, 02:52:13 pm
hahahahha :) aiight! thank you for your time! :D
Title: Re: Another question..
Post by: thepenguin77 on May 27, 2011, 04:22:35 pm
Since it hasn't been mentioned, (though it's been stepped around), here is calc84's CP HL, DE:
Code: [Select]
or a ;reset carry flag
sbc hl, de
add hl, de
This one is small enough that it doesn't need it's own call, so just put it inline like you would CP A. And just like SirCmpwn said, this messes with the flags in the exact same way that CP A does.
Title: Re: Another question..
Post by: Runer112 on May 27, 2011, 04:27:17 pm
And just like SirCmpwn said, this messes with the flags in the exact same way that CP A does.

It affects the n flag differently. ;)
Title: Re: Another question..
Post by: TiAddict on May 27, 2011, 04:40:44 pm
umm sorry but what is "sbc"?
Title: Re: Another question..
Post by: Deep Toaster on May 27, 2011, 04:46:19 pm
SBC is SuBtract with Carry. It's like SUB, but it also subtracts one if the carry flag is set. It's the only way to do 16-bit subtraction/comparison since SUB can only operate on A.
Title: Re: Another question..
Post by: TiAddict on May 27, 2011, 04:47:36 pm
oh i see. thanks Deep Thought! :D
Title: Re: Another question..
Post by: thepenguin77 on May 27, 2011, 04:50:34 pm
Subtract with carry. It subtracts DE and the carry flag.

SBC is usually used when you need to do subtraction with 4 bytes and such.
Code: [Select]
;we will subtract big num 1 from big num 2

ld hl, (bigNum2) ;hl = $0201
ld de, (bigNum1) ;DE = $0605
or a ;this time we don't want the carry flag
sbc hl, de
ld (result), hl ;DE was bigger than HL, so the carry flag is set

ld hl, (bigNum2+2)
ld de, (bigNum1+2)
sbc hl, de ;since the last operation carried, (remember 2nd grade math)
ld (result+2), hl ;we had to subtract an extra 1 from this result

bigNum1:
.db 5, 6, 2, 3
bigNum2:
.db 1, 2, 8, 9
result:
.db 0, 0, 0, 0

This is what SBC is used for. But, we are forced to use it because SUB HL, DE doesn't exist. :P

I noticed that I have been ninja'd, but I don't care.
Title: Re: Another question..
Post by: TiAddict on May 27, 2011, 04:58:25 pm
hahah thanks you for that! xD