Jump to content
IGNORED

The simplest things - CC65


GadgetUK

Recommended Posts

It's been a long time since i played around with C - around 10 years infact... I've found that some of the simplest things can be a challenge (well, for me at least!).

 

Just trying to maintain a score > 32767 seems awkward at the moment. It seems that an int is 8 bit on this platform - is that correct? And a long is 16 bits, strangely enough even if I declare the score as an unsigned long it still only seems to hold +/-32767. I can dig around for ideas on how to get around this on the net but just wanted to float this past in case I am missing something simple.

 

The other oddity that I dont remember from when I used C years ago is that you seem to need to declare variables at the start of a procedure or function - you can't just declare one anywhere in the procedure? Is this just me going crackers here or what? I found the compiler goes mental if for example I declare:- int loop = 0; after ive already done some other assignment type code or anything other than declaration of variables previous to the entry.

Link to comment
Share on other sites

You can declare an int as this:

 

unsigned int a;

 

Then "a" can be 0..65535

 

You can also declare a byte as

 

unsigned char a;

 

In this case "a" van be 0..255

 

If you need more you can use

 

unsigned long a;

 

Now "a" can be 0..4294967295

 

The int of a Lynx is 16 bits.

 

If you want to have negative numbers then use "signed" instead of "unsigned".

 

 

You can also declare variables in several places:

 

char b=5;

 

void main() {

}

 

In this case the variable b is assigned outside the program and it reserves memory from the DATA segment.

 

void main() {

char b = 5;

}

 

Now b is initialized inside the program and resides on the stack in the BSS segment,

 

void main() {

if (1 > 0) {

char b = 6;

}

}

 

Now b is not defined outside the if-statement.

Link to comment
Share on other sites

If you need to convert large values to ascii for printout there are some nice functions available:

 

stdlib.h:char* __fastcall__ ltoa (long val, char* buf, int radix);

stdlib.h:char* __fastcall__ ultoa (unsigned long val, char* buf, int radix);

 

Or the other way around:

 

stdlib.h:long __fastcall__ atol (const char* s);

stdlib.h:long __fastcall__ strtol (const char* nptr, char** endptr, int base);

stdlib.h:unsigned long __fastcall__ strtoul (const char* nptr, char** endptr, int base);

Edited by karri
Link to comment
Share on other sites

I think my first problem was using itoa to convert long to ascii, seems to wrap at > 32767 even when using unsigned long.

 

The declaration problem is very odd - I will post examples later because it sounds like it should work but it doesnt. Heres an example of what doesnt work:-

 

tgi_clear();

 

int loop = 0;

 

while (loop < 7)

{

....

}

 

Compiler complains about 'expected statement at line' pointing to loop declaration.

 

If the declaration is moved above the tgi_clear, it works.

Edited by GadgetUK
Link to comment
Share on other sites

If you need to convert large values to ascii for printout there are some nice functions available:

 

stdlib.h:char* __fastcall__ ltoa (long val, char* buf, int radix);

stdlib.h:char* __fastcall__ ultoa (unsigned long val, char* buf, int radix);

 

Or the other way around:

 

stdlib.h:long __fastcall__ atol (const char* s);

stdlib.h:long __fastcall__ strtol (const char* nptr, char** endptr, int base);

stdlib.h:unsigned long __fastcall__ strtoul (const char* nptr, char** endptr, int base);

 

 

Thanks! The problem with the integer wrapping at the 32k mark was me trying to use itoa with a long, I can confirm ultoa works fine! Some of these are new functions to me, I miss printf =(

Link to comment
Share on other sites

Read this page for some pointers on helping the compiler produce the best code:

http://www.cc65.org/doc/coding.html

 

Also you can generate asm listings at compile time, so you "check" what the compiler is doing. I also find that downloading the library source is useful if you want to know what a particular function actually does and why it works the way it does.

Edited by Shawn Jefferson
Link to comment
Share on other sites

Read this page for some pointers on helping the compiler produce the best code:

http://www.cc65.org/doc/coding.html

 

Also you can generate asm listings at compile time, so you "check" what the compiler is doing. I also find that downloading the library source is useful if you want to know what a particular function actually does and why it works the way it does.

 

Thanks, those are very useful!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...