Dark Bit Factory & Gravity

PROGRAMMING => Other languages => ASM => Topic started by: ninogenio on June 30, 2008

Title: how to work with signed integers on the spectrum z80
Post by: ninogenio on June 30, 2008
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!
Title: Re: how to work with signed integers on the spectrum z80
Post by: rain_storm on June 30, 2008
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)
Title: Re: how to work with signed integers on the spectrum z80
Post by: ninogenio on June 30, 2008
cool i totally get that now thanks rain! k+

Title: Re: how to work with signed integers on the spectrum z80
Post by: rain_storm on June 30, 2008
Anytime nino :)
Title: Re: how to work with signed integers on the spectrum z80
Post by: Jim on June 30, 2008
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
Title: Re: how to work with signed integers on the spectrum z80
Post by: ninogenio on July 01, 2008
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.
Title: Re: how to work with signed integers on the spectrum z80
Post by: rain_storm on July 01, 2008
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
Title: Re: how to work with signed integers on the spectrum z80
Post by: Stonemonkey on July 01, 2008
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.
Title: Re: how to work with signed integers on the spectrum z80
Post by: ninogenio on July 01, 2008
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.