This is an archived, read-only copy of the United-TI subforum , including posts and topic from May 2003 to April 2012. If you would like to discuss any of the topics in this forum, you can visit Cemetech's z80 & ez80 Assembly subforum. Some of these topics may also be directly-linked to active Cemetech topics. If you are a Cemetech member with a linked United-TI account, you can link United-TI topics here with your current Cemetech topics.

This forum is locked: you cannot post, reply to, or edit topics. Z80 & 68k Assembly => z80 & ez80 Assembly
Author Message
darkstone knight


Advanced Member


Joined: 07 Sep 2008
Posts: 438

Posted: 20 Nov 2008 10:55:54 am    Post subject:

ik making an fractal program, and im confused about negeating my number (for those geeks, IM(|R|)^2)

right now, my data structure is this:

some_data:
.db 1,2,3,4

the "decimal point" is located behind byte 1

to substract it from zero, im using this:

LD HL,some_data
LD B,4
loop:
LD A,(HL)
CPL
LD (HL),A
INC A
DJNZ loop



in that case, i need to add one, right??

note: the number is never greater than ~3


Last edited by Guest on 22 Jul 2010 11:45:09 am; edited 1 time in total
Back to top
benryves


Active Member


Joined: 23 Feb 2006
Posts: 564

Posted: 20 Nov 2008 11:04:54 am    Post subject:

CPL calculates the one's complement of the accumulator, ie it inverts every bit. What you want is NEG, which calculates the two's complement.

NEG is the equivalent of DEC A \ CPL or CPL \ INC A. In your case, you need to negate a larger number. For a sixteen-bit number you could do this:


Code:
    DEC HL
    LD A,H \ CPL \ LD H,A
    LD A,L \ CPL \ LD L,A


Edit: Sorry about that, misread the question somewhat. Sad Here's a routine that negates a 32-bit integer pointed by HL, but I'm not really sure what format your variable is in.


Code:
; Input: HL = pointer to 32-bit integer (LSB first).
NegateInt32:
    push hl
    ld b,4
DecrementLoop:
    ld a,(hl)
    dec a
    ld (hl),a
    inc hl
    jr nc,Decremented
    djnz DecrementLoop
Decremented:
    pop hl
    ld b,4
ComplementLoop:
    ld a,(hl)
    cpl
    ld (hl),a
    inc hl
    djnz ComplementLoop
    ret


Last edited by Guest on 20 Nov 2008 11:17:21 am; edited 1 time in total
Back to top
calc84maniac


Elite


Joined: 22 Jan 2007
Posts: 770

Posted: 20 Nov 2008 12:02:51 pm    Post subject:

Ben, that doesn't work since dec a doesn't affect the carry flag. I'll whip up my own (unrolled):

Code:
ld hl,some_data
xor a \ ld b,a \ sub (hl) \ ld (hl),a
inc hl
ld a,b \ sbc a,(hl) \ ld (hl),a
inc hl
ld a,b \ sbc a,(hl) \ ld (hl),a
inc hl
ld a,b \ sbc a,(hl) \ ld (hl),a


Last edited by Guest on 22 Jul 2010 11:45:25 am; edited 1 time in total
Back to top
darkstone knight


Advanced Member


Joined: 07 Sep 2008
Posts: 438

Posted: 20 Nov 2008 12:36:55 pm    Post subject:

Quote:
I'm not really sure what format your variable is in.


you have four bytes

.db 1,2,3,4

the first byte is the actual number, the others are decimals

that means this number is actualy 1+(2/2^8)+(3/2^16)+(4/2^24)
confusing? yea..

the theory was that if i tried to invert someting like this:

random:
.db 5,52,23,125

the result would be 1/2^24 off, and i would need to set up an routine to add one

(wich is easy, but takes 1xxx clocks >.<)


Last edited by Guest on 22 Jul 2010 11:45:42 am; edited 1 time in total
Back to top
FloppusMaximus


Advanced Member


Joined: 22 Aug 2008
Posts: 472

Posted: 20 Nov 2008 12:54:24 pm    Post subject:

In that case, calc84maniac is correct, except that it sounds like your numbers are big-endian, whereas the routine he gave was for a little-endian number. You want

Code:
ld hl,some_data + 3
xor a \ ld b,a \ sub (hl) \ ld (hl),a
dec hl
ld a,b \ sbc a,(hl) \ ld (hl),a
dec hl
ld a,b \ sbc a,(hl) \ ld (hl),a
dec hl
ld a,b \ sbc a,(hl) \ ld (hl),a


But... depending on the context, you could probably do it faster, because you can easily keep a 32-bit number in registers.
Back to top
benryves


Active Member


Joined: 23 Feb 2006
Posts: 564

Posted: 20 Nov 2008 12:55:26 pm    Post subject:

calc84maniac wrote:
Ben, that doesn't work since dec a doesn't affect the carry flag. I'll whip up my own (unrolled):
Oh, whoops, sorry about that. For some reason I thought it did with 8-bit values (I knew it didn't with 16-bit values). Neutral Thanks for the correction, and good use of sbc. Very Happy

Last edited by Guest on 22 Jul 2010 11:44:49 am; edited 1 time in total
Back to top
darkstone knight


Advanced Member


Joined: 07 Sep 2008
Posts: 438

Posted: 20 Nov 2008 01:05:09 pm    Post subject:

well, thanks!

but actualy, im wondering now if being 1/2^24 off is that bad.. lol

but now im thinking about negative numbers... i thik my engine messes up atm...

leme think about it

edit: yay, all calucations (except the initial cordination set) are done (took me a long time..)
math is a bitch Mad


Last edited by Guest on 20 Nov 2008 02:00:04 pm; edited 1 time in total
Back to top
Display posts from previous:   
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
    »
» View previous topic :: View next topic  
Page 1 of 1 » All times are UTC - 5 Hours

 

Advertisement