Page 1 of 2

Sozobon C's a bit broken isn't it?

Posted: 24 Jan 2021 16:58
by stephen_usher
I was just writing a quick program to dump the contents of the memory mapped into the cartridge address space and found this wonder, e.g.

Code: Select all

#include <stdio.h>

#define CARTSTART 0xfa0000
#define CARTEND 0xfbffff

main()
{
	unsigned char *addr = CARTSTART;
	unsigned char *endaddr = CARTEND;

	for (; addr < endaddr; addr++)
	{
		printf("0x%02x\t", *addr);
	}
}
This code will fail as the test to see if addr is less than endaddr will fail. (Add to this that if you try to use CARTEND in the test then the compilation fails saying that there's in incorrect : expression.)

If you subtract addr from endaddr then you will get a value that's ffffffff.

If you change the start address to 0xfb0000 then it works. It looks like all tests and arithmetic, even on pointers, is 16 bit only, and unsigned arithmetic seems to be sort of signed. That's rather broken.

Re: Sozobon C's a bit broken isn't it?

Posted: 24 Jan 2021 17:19
by rubber_jonnie
I couldn't get Sozobon to work at all, but Pure C is nice. Might see if it works on that.

Re: Sozobon C's a bit broken isn't it?

Posted: 24 Jan 2021 17:23
by czietz
I wonder if it wants the constants to be explicitly defined as long int, i.e. 0xfa0000L, 0xfbffffL. However, why bother with ancient Sozobon C, at all?

Re: Sozobon C's a bit broken isn't it?

Posted: 24 Jan 2021 17:31
by stephen_usher
czietz wrote: 24 Jan 2021 17:23 I wonder if it wants the constants to be explicitly defined as long int, i.e. 0xfa0000L, 0xfbffffL. However, why bother with ancient Sozobon C, at all?
When you don't want to boot into MiNT but do want a command line environment for a quick fix thing. Pure C is far too GUI for me. :D

So, it's Gulam + Sozobon C for quick hacks.

P.S. The values put into the pointer variables are correct, so adding an 'L' to the end wouldn't make a difference. It's the pointer arithmetic which is broken.

Re: Sozobon C's a bit broken isn't it?

Posted: 24 Jan 2021 17:41
by czietz
Hm, apparently you're using a different version of Sozobon C than the one available on Compiler Explorer: https://tinyurl.com/y2w86b45
With that one, your code would not even compile without the cast to "unsigned char *", but then the pointer arithmetic is correct.

PS: You're aware that Pure C has a command-line compiler, too?

Re: Sozobon C's a bit broken isn't it?

Posted: 24 Jan 2021 17:53
by stephen_usher
czietz wrote: 24 Jan 2021 17:41 Hm, apparently you're using a different version of Sozobon C than the one available on Compiler Explorer: https://tinyurl.com/y2w86b45
With that one, your code would not even compile without the cast to "unsigned char *", but then the pointer arithmetic is correct.

PS: You're aware that Pure C has a command-line compiler, too?
The version of Sozobon C I'm using was downloaded from umich.edu in about 1990/91... It's a bit old. :-)

Oh, actually, must have been '89 as I was doing my MSc Comp Sci at the time.

Re: Sozobon C's a bit broken isn't it?

Posted: 24 Jan 2021 17:57
by rubber_jonnie
I just tried it on Pure C (I get you don't like GUIs) and had to add L to the values assigned to CARTSTART/CARTEND to have it stop moaning to me about "Constant is long in function main".

It's now moaning about "Non-portable pointer assignment in function main" and "Function should return a value in function main".

Will see if I can fix but I am a C beginner!!

Nice to have a little project to make me think.

Re: Sozobon C's a bit broken isn't it?

Posted: 24 Jan 2021 18:10
by stephen_usher
Yeah, there should be casts for the address assignments. The warning about main returning a value is a newfangled ANSI C thing. If you make have "void main()" or just have "return 1;" at the end that'll get rid of the annoyance.

Re: Sozobon C's a bit broken isn't it?

Posted: 24 Jan 2021 18:33
by rubber_jonnie
stephen_usher wrote: 24 Jan 2021 18:10 Yeah, there should be casts for the address assignments. The warning about main returning a value is a newfangled ANSI C thing. If you make have "void main()" or just have "return 1;" at the end that'll get rid of the annoyance.
Yep, I added void main() and that cleared that up, but still get the "Non-portable pointer assignment in function main" errors for lines 8/9.

It was nice to have something interesting that I could take as a learning point drop in my lap :)

Re: Sozobon C's a bit broken isn't it?

Posted: 24 Jan 2021 18:40
by stephen_usher
Yes, lines 8 & 9 should be:

Code: Select all

	unsigned char *addr = (unsigned char *) CARTSTART;
	unsigned char *endaddr = (unsigned char *) CARTEND;