Author Topic: 16 by 24 bit signed addition  (Read 6226 times)

0 Members and 1 Guest are viewing this topic.

Offline ben_g

  • Hey cool I can set a custom title now :)
  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +125/-4
  • Asm noob
    • View Profile
    • Our programmer's team: GameCommandoSquad
16 by 24 bit signed addition
« 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.
My projects
 - The Lost Survivors (Unreal Engine) ACTIVE [GameCommandoSquad main project]
 - Oxo, with single-calc multiplayer and AI (axe) RELEASED (screenshot) (topic)
 - An android version of oxo (java)  ACTIVE
 - A 3D collision detection library (axe) RELEASED! (topic)(screenshot)(more recent screenshot)(screenshot of it being used in a tilemapper)
Spoiler For inactive:
- A first person shooter with a polygon-based 3d engine. (z80, will probably be recoded in axe using GLib) ON HOLD (screenshot)
 - A java MORPG. (pc) DEEP COMA(read more)(screenshot)
 - a minecraft game in axe DEAD (source code available)
 - a 3D racing game (axe) ON HOLD (outdated screenshot of asm version)

This signature was last updated on 20/04/2015 and may be outdated

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: 16 by 24 bit signed addition
« Reply #1 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.
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline ben_g

  • Hey cool I can set a custom title now :)
  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +125/-4
  • Asm noob
    • View Profile
    • Our programmer's team: GameCommandoSquad
Re: 16 by 24 bit signed addition
« Reply #2 on: October 02, 2011, 03:40:31 pm »
Thanks, but it still doesn't fully work. (It gets the same results, actually.
My projects
 - The Lost Survivors (Unreal Engine) ACTIVE [GameCommandoSquad main project]
 - Oxo, with single-calc multiplayer and AI (axe) RELEASED (screenshot) (topic)
 - An android version of oxo (java)  ACTIVE
 - A 3D collision detection library (axe) RELEASED! (topic)(screenshot)(more recent screenshot)(screenshot of it being used in a tilemapper)
Spoiler For inactive:
- A first person shooter with a polygon-based 3d engine. (z80, will probably be recoded in axe using GLib) ON HOLD (screenshot)
 - A java MORPG. (pc) DEEP COMA(read more)(screenshot)
 - a minecraft game in axe DEAD (source code available)
 - a 3D racing game (axe) ON HOLD (outdated screenshot of asm version)

This signature was last updated on 20/04/2015 and may be outdated

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: 16 by 24 bit signed addition
« Reply #3 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?

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: 16 by 24 bit signed addition
« Reply #4 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)
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: 16 by 24 bit signed addition
« Reply #5 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

Offline ben_g

  • Hey cool I can set a custom title now :)
  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +125/-4
  • Asm noob
    • View Profile
    • Our programmer's team: GameCommandoSquad
Re: 16 by 24 bit signed addition
« Reply #6 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...
« Last Edit: October 02, 2011, 03:53:16 pm by ben_g »
My projects
 - The Lost Survivors (Unreal Engine) ACTIVE [GameCommandoSquad main project]
 - Oxo, with single-calc multiplayer and AI (axe) RELEASED (screenshot) (topic)
 - An android version of oxo (java)  ACTIVE
 - A 3D collision detection library (axe) RELEASED! (topic)(screenshot)(more recent screenshot)(screenshot of it being used in a tilemapper)
Spoiler For inactive:
- A first person shooter with a polygon-based 3d engine. (z80, will probably be recoded in axe using GLib) ON HOLD (screenshot)
 - A java MORPG. (pc) DEEP COMA(read more)(screenshot)
 - a minecraft game in axe DEAD (source code available)
 - a 3D racing game (axe) ON HOLD (outdated screenshot of asm version)

This signature was last updated on 20/04/2015 and may be outdated

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: 16 by 24 bit signed addition
« Reply #7 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.
« Last Edit: October 02, 2011, 03:54:13 pm by calc84maniac »
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline ben_g

  • Hey cool I can set a custom title now :)
  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +125/-4
  • Asm noob
    • View Profile
    • Our programmer's team: GameCommandoSquad
Re: 16 by 24 bit signed addition
« Reply #8 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:

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.
« Last Edit: October 02, 2011, 04:07:03 pm by ben_g »
My projects
 - The Lost Survivors (Unreal Engine) ACTIVE [GameCommandoSquad main project]
 - Oxo, with single-calc multiplayer and AI (axe) RELEASED (screenshot) (topic)
 - An android version of oxo (java)  ACTIVE
 - A 3D collision detection library (axe) RELEASED! (topic)(screenshot)(more recent screenshot)(screenshot of it being used in a tilemapper)
Spoiler For inactive:
- A first person shooter with a polygon-based 3d engine. (z80, will probably be recoded in axe using GLib) ON HOLD (screenshot)
 - A java MORPG. (pc) DEEP COMA(read more)(screenshot)
 - a minecraft game in axe DEAD (source code available)
 - a 3D racing game (axe) ON HOLD (outdated screenshot of asm version)

This signature was last updated on 20/04/2015 and may be outdated

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: 16 by 24 bit signed addition
« Reply #9 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
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline ben_g

  • Hey cool I can set a custom title now :)
  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +125/-4
  • Asm noob
    • View Profile
    • Our programmer's team: GameCommandoSquad
Re: 16 by 24 bit signed addition
« Reply #10 on: October 02, 2011, 04:11:34 pm »
It still gives the exact same result :(
My projects
 - The Lost Survivors (Unreal Engine) ACTIVE [GameCommandoSquad main project]
 - Oxo, with single-calc multiplayer and AI (axe) RELEASED (screenshot) (topic)
 - An android version of oxo (java)  ACTIVE
 - A 3D collision detection library (axe) RELEASED! (topic)(screenshot)(more recent screenshot)(screenshot of it being used in a tilemapper)
Spoiler For inactive:
- A first person shooter with a polygon-based 3d engine. (z80, will probably be recoded in axe using GLib) ON HOLD (screenshot)
 - A java MORPG. (pc) DEEP COMA(read more)(screenshot)
 - a minecraft game in axe DEAD (source code available)
 - a 3D racing game (axe) ON HOLD (outdated screenshot of asm version)

This signature was last updated on 20/04/2015 and may be outdated

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: 16 by 24 bit signed addition
« Reply #11 on: October 02, 2011, 04:14:16 pm »
Could you give an example of an addition that doesn't work?
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline ben_g

  • Hey cool I can set a custom title now :)
  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +125/-4
  • Asm noob
    • View Profile
    • Our programmer's team: GameCommandoSquad
Re: 16 by 24 bit signed addition
« Reply #12 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.
My projects
 - The Lost Survivors (Unreal Engine) ACTIVE [GameCommandoSquad main project]
 - Oxo, with single-calc multiplayer and AI (axe) RELEASED (screenshot) (topic)
 - An android version of oxo (java)  ACTIVE
 - A 3D collision detection library (axe) RELEASED! (topic)(screenshot)(more recent screenshot)(screenshot of it being used in a tilemapper)
Spoiler For inactive:
- A first person shooter with a polygon-based 3d engine. (z80, will probably be recoded in axe using GLib) ON HOLD (screenshot)
 - A java MORPG. (pc) DEEP COMA(read more)(screenshot)
 - a minecraft game in axe DEAD (source code available)
 - a 3D racing game (axe) ON HOLD (outdated screenshot of asm version)

This signature was last updated on 20/04/2015 and may be outdated

Offline ben_g

  • Hey cool I can set a custom title now :)
  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +125/-4
  • Asm noob
    • View Profile
    • Our programmer's team: GameCommandoSquad
Re: 16 by 24 bit signed addition
« Reply #13 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?
« Last Edit: October 05, 2011, 01:22:18 pm by ben_g »
My projects
 - The Lost Survivors (Unreal Engine) ACTIVE [GameCommandoSquad main project]
 - Oxo, with single-calc multiplayer and AI (axe) RELEASED (screenshot) (topic)
 - An android version of oxo (java)  ACTIVE
 - A 3D collision detection library (axe) RELEASED! (topic)(screenshot)(more recent screenshot)(screenshot of it being used in a tilemapper)
Spoiler For inactive:
- A first person shooter with a polygon-based 3d engine. (z80, will probably be recoded in axe using GLib) ON HOLD (screenshot)
 - A java MORPG. (pc) DEEP COMA(read more)(screenshot)
 - a minecraft game in axe DEAD (source code available)
 - a 3D racing game (axe) ON HOLD (outdated screenshot of asm version)

This signature was last updated on 20/04/2015 and may be outdated