Author Topic: how to work with signed integers on the spectrum z80  (Read 5311 times)

0 Members and 1 Guest are viewing this topic.

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
i have been trying to find out about working with z80 signed numbers but im still unsure on a couple of details,

im wondering if i dump any byte be it signed or unsigned in the accumulator with the msb set too 1 will this cause the s bit in the status register to be set to 1 so that a check can be performed.

and also how would i go about adding a - signed number to a positive one to cause a subtraction.

this is just for a simple ball where i add a delta factor on, changing the sign at the screen boundries.

cheers!
Challenge Trophies Won:

Offline rain_storm

  • Here comes the Rain
  • DBF Aficionado
  • ******
  • Posts: 3088
  • Karma: 182
  • Rain never hurt nobody
    • View Profile
    • org_100h
Positive numbers range from 0x00 -> 0x7F that looks like this in binary 0000 0000 -> 0111 1111 (+0x00 -> +0x7F)
Negative numbers are   from 0xFF -> 0x80 that looks like this in binary 1111 1111 -> 1000 0000 (-0x01 -> -0x80)
because the numbers wrap around to zero it is simply a matter of adding them together c = a + (-b) or c = a - (-b). The wrap around will take care of the rest.

look at this sequence from -1 to -8 compared to +0 to +7

 -1            +0
11111111  00000000
11111110  00000001
11111101  00000010
11111100  00000011
11111011  00000100
11111010  00000101
11111001  00000110
11111000  00000111
 -8           +7

now lets see what happens when we add them

-1 + 1

11111111 + 00000001 = 00000001 00000000 <- result is too big for 1 byte so it wraps around to zero the byte itself contains the right result (0)
« Last Edit: June 30, 2008 by rain_storm »

Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
cool i totally get that now thanks rain! k+

Challenge Trophies Won:

Offline rain_storm

  • Here comes the Rain
  • DBF Aficionado
  • ******
  • Posts: 3088
  • Karma: 182
  • Rain never hurt nobody
    • View Profile
    • org_100h
Anytime nino :)

Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Loading a value in to A won't set the sign flag, but you can either do a CP A,0 or AND A,A or almost any other operation to set the flags.

As rain_storm helpfully points out, twos-complement is very clever when it comes to signs - mostly you just have to think about the numbers the right way and the operations remain the same!

Jim
Challenge Trophies Won:

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
ahh cheers jim so some action must be taken on the register first, cool.

im gonna have a wee shot at learning binary fixed point next which should be intresting.
Challenge Trophies Won:

Offline rain_storm

  • Here comes the Rain
  • DBF Aficionado
  • ******
  • Posts: 3088
  • Karma: 182
  • Rain never hurt nobody
    • View Profile
    • org_100h
Stone monkey was looking into fixed point (or was it floating point) on z80 asm recently I will dig out the link as there were some good links in that topic

Edit - ahh it was actually 6502 (C64 / NES / maybe amiga too) but still its worth a read

http://dbfinteractive.com/index.php?topic=3034.0
http://dbfinteractive.com/index.php?topic=3048.0
« Last Edit: July 01, 2008 by rain_storm »

Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
It was fixed point but on the 6502, both are 8 bit tho and probably have a very similar instruction set with perhaps the z80 having a few more.

Offline ninogenio

  • Pentium
  • *****
  • Posts: 1668
  • Karma: 133
    • View Profile
thanks guys i only had a quick flick through for now but for sure theres some good stuff in there that ill have to go through a bit more in depth.
Challenge Trophies Won: