Omnimaga

Calculator Community => TI Calculators => ASM => Topic started by: ben_g on October 02, 2011, 03:24:44 pm

Title: 16 by 24 bit signed addition
Post by: ben_g on October 02, 2011, 03:24:44 pm
Hi,

I'm trying to do a 16 by 24bit signed addition for my z80 program. Now I have this code:
Code: [Select]
  ld hl,(var1)
  ld de, (var2)
  ld a, h
  rla
  sbc a,a
  add hl, de
  ld b, a
  ld a, (var1+2)
  adc a, b
  ld (var1+2), a
It seems to work in some situations, but it also sometimes gives other results.
What am I doing wrong?

btw: var1 is the 24bit var, var2 the 16bit one. I haven't fully written this routine, a part of it comes from a load 16bit to 24bit routine from calc84maniac.

many thanks in avantage.
Title: Re: 16 by 24 bit signed addition
Post by: calc84maniac on October 02, 2011, 03:29:31 pm
You need to sign extend using the top bit of DE, not of HL. Replacing ld a,h with ld a,d should fix your problem.
Title: Re: 16 by 24 bit signed addition
Post by: ben_g on October 02, 2011, 03:40:31 pm
Thanks, but it still doesn't fully work. (It gets the same results, actually.
Title: Re: 16 by 24 bit signed addition
Post by: Xeda112358 on October 02, 2011, 03:45:23 pm
Wait, so are you adding 24 bits at (var1) and 16 bits at (var2)?
If that is the case, does this work (I have not tested this!):
Code: [Select]
  ld hl,(var1)
  ld de, (var2)
  add hl,de
  sbc a,a
  ld b,a
  ld a,(var1+2)
  sub b
  ld (var1+2),a
The result is in AHL and does essentially the same thing (I believe) with two bytes less (I think).

EDIT: Is there a particular reason you are only saving the upper 8 bits and not the lower 16?
Title: Re: 16 by 24 bit signed addition
Post by: calc84maniac on October 02, 2011, 03:47:37 pm
Thanks, but it still doesn't fully work. (It gets the same results, actually.
Oh, it looks like you forgot to save the HL result back to var1. Also Xeda, I don't think that code works for a signed 16-bit value (that looks like it could work for unsigned though)
Title: Re: 16 by 24 bit signed addition
Post by: Xeda112358 on October 02, 2011, 03:51:01 pm
Oh, right, signed XD. Sorry, I missed that. I don't think I've ever made a signed math routine O.O
Title: Re: 16 by 24 bit signed addition
Post by: ben_g on October 02, 2011, 03:52:13 pm
That also gives the same result...

BTW: I do save the lower 16 bits. I just forgot to copy the last line, which is :
Code: [Select]
ld (tx2), hl
EDIT: double ninja'd O.O I should try to type faster...
Title: Re: 16 by 24 bit signed addition
Post by: calc84maniac on October 02, 2011, 03:52:58 pm
Haha, you edit ninja'd me about saving the result. Also, here's a slightly more optimized unsigned addition:
Code: [Select]
 ld hl,(var1)
  ld de, (var2)
  add hl,de
  ld (var1),hl
  ld a,(var1+2)
  adc a,0
  ld (var1+2),a

Edit: That post was aimed at Xeda, btw.

ben_g, I'm not sure what the problem could be then.
Title: Re: 16 by 24 bit signed addition
Post by: ben_g on October 02, 2011, 03:56:39 pm
But if xeda's code only works for unsigned numbers, and it gave the same result as mine, does that mean if I'm just handeling the sign incorrectely? If so, then what am I doing wrong with the sign?

EDIT: I'm also not entirely sure if it's the addition that causes the problem, but as the rest of the code is almost simular to my previous triangle drawing routine (16bit and 8bit), eccept for the addition and the storing to variables.

This is what it does:
(http://picturestack.com/586/20/Zqsprobleemnpv.gif)
I made the two triangles 50% transparent so you can see both of them well. They should form a rectangle, but my triangle drawing routine fails on one of them.
Title: Re: 16 by 24 bit signed addition
Post by: calc84maniac on October 02, 2011, 04:01:48 pm
I am pretty sure this code would work for signed:
Code: [Select]
  ld hl,(var1)
  ld de, (var2)
  ld a, d
  rla
  sbc a, a
  ld b, a
  add hl, de
  ld (var1), hl
  ld a, (var1+2)
  adc a, b
  ld (var1+2), a
Title: Re: 16 by 24 bit signed addition
Post by: ben_g on October 02, 2011, 04:11:34 pm
It still gives the exact same result :(
Title: Re: 16 by 24 bit signed addition
Post by: calc84maniac on October 02, 2011, 04:14:16 pm
Could you give an example of an addition that doesn't work?
Title: Re: 16 by 24 bit signed addition
Post by: ben_g on October 02, 2011, 04:23:31 pm
It's difficoult to calculate what walues are passed to the routine by the screenshot, so as logn as i can't get wabbitemu to debug, I can't tell which arguments are passed to it.
Title: Re: 16 by 24 bit signed addition
Post by: ben_g on October 04, 2011, 04:06:30 pm
This are some of the results it gets, compared to those the windows calculator gets. The results from the calculator are formated as actual HEX numbers (Big endian), while the results from the routine are like they are stored to the memory(little endian). I've found 3 cases on which the result from the routine doesn't match those from the calculator.
Code: [Select]
   Results from calculator
-----------------------------

00 19 00    00 00    00 19 00
00 19 00    01 8C    00 1A 8C

00 19 FF    01 80    00 1B 7F
00 1A 8C    01 8C    00 1C 18


00 49 00    FE 80    01 47 80
00 49 00    00 00    00 49 00

00 47 80    FE 80    01 46 00
00 49 00    2F 00    00 78 00


    Results from routine
-----------------------------
  In 1       In2       Out

00 19 00    00 00    00 19 00
00 19 00    8C 01    8C 1A 00

FF 19 00    80 01    7F 1B 00
8C 1A 00    8C 01    18 1C 00


00 49 00    80 FE    40 47 00   <- Wrong?
00 49 00    00 00    00 49 00

80 47 00    80 FE    00 46 00   <- Wrong?
00 49 00    00 2F    00 49 00   <- Wrong?
Is there a problem with the routine or did I calculate the numbers wrong?