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. 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). Thanks for the correction, and good use of sbc.
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
Last edited by Guest on 20 Nov 2008 02:00:04 pm; edited 1 time in total |
|
Back to top |
|
|
|