Another example

SYS 64738
User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

Another example

Post by Shaun_B »

I'm on a bit of a roll with this learning BASIC stuff. In fact, I'm re-learning BASIC - not that I knew that much in the first place - but enough to do the... err... basics.

Anyway, Commodore BASIC returns 0 as false and -1 as true, which seems odd when you're exposed to C-derived languages so much. So, here's an example that returns 1 as true and 0 as false, and also moves a ball around the screen using the keys Z, X, P and L for left, right, up and down respectively. Space will toggle 'rub out' or 'draw'. This is the sort of program that I'd write on the VIC-20 in the Science labs, or when I got bored with Computing Studies using the BBCs (except I'd use plot x,y rather than print out a character).

Code: Select all

10 def fn b(x) = abs(x)
20 x = y = a = s = d = i = a$="": a = 81: s = 1024: d = 40
30 print"{clear}": for i=0 to 1
40 poke s + x + y * d, a: get a$
45 x = x + fn b (a$ = "x") - fn b ( a$ = "z"): y = y + fn b (a$ = "l") - fn b(a$ = "p")
50 x = x + fn b (x < 0) - fn b (x > 39): y = y + fn b (y < 0) - fn b (y > 24)
60 if a$ = " " and a = 81 then a = 32: a$ = ""
70 if a$ = " " and a = 32 then a = 81
80 i = 0: next
I've been informed by TMR that variables are quicker, so poke 1024, 81 is slower than poke s, a - so although it's a short piece of code, I've used some of the optimisations (?) that I've learnt during in there just for good measure.

I'm assuming that the not(x) is simply flipping the bits, so -1 in two's compliment will be ff, which is 11111111, so flipping the bits then adding one will give you 1 as true if using the function. It's been a while since I did anything with logic gates so I may be wrong or have explained it poorly.

Regards,

Shaun.
Last edited by Shaun_B on Sun Jun 29, 2014 6:35 pm, edited 1 time in total.


BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.
Gonzo
Member
Member
Posts: 15
Joined: Sun Apr 13, 2014 12:59 pm
Contact:

Re: Another example

Post by Gonzo »

"abs(x)", faster than "-x", both faster than this monstrosity you propose.
User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

Re: Another example

Post by Shaun_B »

Gonzo wrote:"abs(x)", faster than "-x", both faster than this monstrosity you propose.
It wasn't a proposal, it was an example. Please post a better one if you have one, but just so you know, my primary aim isn't to necessarily write 'fast' BASIC, although I'll use any speed improvement tips that come my way.

There's no point in me always optimising code just to slow it down again anyway as my primary target is the SuperCPU and not 1mhz.

Regards,

Shaun.
BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.
ajordison
Developer
Developer
Posts: 59
Joined: Wed Apr 16, 2014 1:28 pm
Location: Hartlepool, UK
Contact:

Re: Another example

Post by ajordison »

Why are you adding 1 in your function? For example, if you pass a 'true' to your function you're going to get not(-1) + 1 = 2 returned. If you pass 'false' you'll get not(0)+1 = 1 returned. Your calculations in lines 4 and 5 work because you're adding two then subtracting 1. Other than that, it's the best function abuse I've seen!
Try out CBM prg Studio for all your Commodore development needs!
Stay a while...stay for erm quite a while!
Gonzo
Member
Member
Posts: 15
Joined: Sun Apr 13, 2014 12:59 pm
Contact:

Re: Another example

Post by Gonzo »

Quite lot of the text you wrote was about true and false and how to convert 0/-1 to 0/1. Your method is ineffective, complex and unnecessarily hard to understand. Even you make assumptions about how NOT(x) works.

Here is the example for you:

Code: Select all

10 def fn b(x) = (not(x)) + 1
You can use the rest of your code if you want to confuscate the main point in your post.
Shaun_B wrote:so although it's a short piece of code, I've used some of the optimisations (?) that I've learnt during in there just for good measure.
Shaun_B wrote:but just so you know, my primary aim isn't to necessarily write 'fast' BASIC, although I'll use any speed improvement tips that come my way.

There's no point in me always optimising code just to slow it down again anyway as my primary target is the SuperCPU and not 1mhz.
You should make up your mind about optimizations. If you don't care about speed then write easy to understand, easy to maintain code.
User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

Re: Another example

Post by Shaun_B »

ajordison wrote:Why are you adding 1 in your function? For example, if you pass a 'true' to your function you're going to get not(-1) + 1 = 2 returned. If you pass 'false' you'll get not(0)+1 = 1 returned. Your calculations in lines 4 and 5 work because you're adding two then subtracting 1. Other than that, it's the best function abuse I've seen!
That wasn't what I was getting in the emulator, when I do PRINT NOT(-1) I get 0, and when I PRINT NOT(0) I get -1. I'm pretty sure that I tested that.

Anyway, try again :-)

Regards,

Shaun.
BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.
ajordison
Developer
Developer
Posts: 59
Joined: Wed Apr 16, 2014 1:28 pm
Location: Hartlepool, UK
Contact:

Re: Another example

Post by ajordison »

Shaun_B wrote:That wasn't what I was getting in the emulator, when I do PRINT NOT(-1) I get 0, and when I PRINT NOT(0) I get -1. I'm pretty sure that I tested that.
My mistake, I got NOT mixed up with ABS :oops: I'm blaming the late night/early morning combo...
Try out CBM prg Studio for all your Commodore development needs!
Stay a while...stay for erm quite a while!
User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

Re: Another example

Post by Shaun_B »

Gonzo wrote:Quite lot of the text you wrote was about true and false and how to convert 0/-1 to 0/1. Your method is ineffective, complex and unnecessarily hard to understand. Even you make assumptions about how NOT(x) works.
I'm sure that NOT just flips the bits, so -1 would be ff, !-1 would be zero, I didn't realise that I had this wrong.

I now see your main point, I'll keep my listings to myself unless they're clean from now on.

Many thanks,

Shaun.
BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.
User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

Re: Another example

Post by Shaun_B »

ajordison wrote:
Shaun_B wrote:That wasn't what I was getting in the emulator, when I do PRINT NOT(-1) I get 0, and when I PRINT NOT(0) I get -1. I'm pretty sure that I tested that.
My mistake, I got NOT mixed up with ABS :oops: I'm blaming the late night/early morning combo...
Don't worry about it, I'll make plenty more mistakes before you make another. Also had plenty of late nights recently (plus maybe alcohol included) - it happens.

Regards,

Shaun.
BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.
Post Reply Previous topicNext topic

Who is online

Users browsing this forum: No registered users and 5 guests