02-29-2012, 10:35 PM
na_th_an Wrote:I usually code in C, and I can assure you that this code works as intendedThis is a bit... let's say complicated: ANSI C does not define a default "int" size (which is supposed to be the default for the target platform). The << operator (shift) are defined on INT types. The question here is: z80 "int" (platform default) should be 8 or 16bit? There's no rule to decide that (e.g. could be 16 bits, because it's the pointer size, or 8 bits because it's the architectural/logical/accumulator size, etc. etc.).![]()
Code:#include <stdio.h>
void main (void) {
unsigned char lsb = 0;
unsigned char msb = 60;
unsigned int address;
unsigned char x = 0, cx = 100;
int dx;
address = lsb + (msb << 8);
printf ("%d\n", address);
dx = x - cx;
printf ("%d\n", dx);
}
15360 and -100 are printed on screen. This works in any C compiler I've tried so far (z88dk and gcc).
FreeBasic (which I abandoned, because it's not BASIC anymore. It's C++ with BASIC keywords: Pointers??, Classes with multiple inheritance, Templating :??

na_th_an Wrote:This should print -100:Code:Sub printMe (a as Integer)
Print a
End Sub
Dim as uByte x, cx
x = 0: cx = 100
printMe (x-cx)
Another question here:
x - cx is an unsigned overflowed number (-100 = 156). Then you convert this unsigned 156 into an integer. Why should it be -100? You're converting from an unsigned to signed. Converting it to -100 means replicating higher bit (bit 7) to the higher byte.
This is because C (and FreeBASIC, alias C with BASIC keywords), uses a different scheme for dx = x - cx expression:
DX = X - CX
- ZX BASIC: Evaluate Right Part (X - CX). Coerce (typecast) to Left part type. Store result in Left Part (variable)
- C/C++/freebasiC++: (I guess) Coerce (typecast) 1st element of Right part (x) to Left part type. Then evaluate expression. Then store in Left part (variable).
NOTE: I was disenchanted with FreeBasic. BASIC Was not designed with that in mind (some of those concepts didn't even exist when BASIC was designed) but for simplicity's sake. Of course there are many things that can be added while preserving simplicity, not only OOP, but iterators, dynamic containers (List, Dictionaries, etc) and much much more. All of them allows more abstraction, or to write a program with fewer lines, and no pointers at all (BTW you can already mimic "pointers" with peek and poke)