Page 1 of 1

Exercises to build 6502 coding experience

Posted: Sat Mar 02, 2019 2:02 pm
by BigEd
This idea came up nearby: what kind of relatively simple programming exercises might be useful to someone new to the 6502, unfamiliar with the addressing modes, unsure how to use the registers, perhaps used to a CPU with rather different facilities?

My first thought was this:
Quote:
here's an idea: write a routine to clear a 1000 byte block of memory, the address being known at assembly time. You can probably write it three or four ways, and doing so might itself be a good exercise.
(I think people who speak of a coding dojo would call these exercises kata. But I'm not thinking about general coding competence, more at targeting competence in making best use of the 6502.)

(Edit: I think it's best not to discuss or attempt solutions on this thread, so here's one for that.)

Re: Exercises to build 6502 coding experience

Posted: Sat Mar 02, 2019 2:33 pm
by Arlet
Assuming a UART "PutChar" routine, write a routine to output a hexadecimal nibble, and then a routine to output a hexadecimal byte.

Re: Exercises to build 6502 coding experience

Posted: Sat Mar 02, 2019 3:02 pm
by BigEd
Nice one, thanks!

Re: Exercises to build 6502 coding experience

Posted: Sat Mar 02, 2019 3:50 pm
by Chromatix
Given two arrays of 1000 byte values, addresses known at assembly time, calculate the SAD (sum of absolute differences). You'll need 3 bytes to store the result.

Code: Select all

for(i=0; i<1000; i++)
    sum += (A[i] > B[i]) ? A[i] - B[i] : B[i] - A[i];
Do this twice, first assuming the bytes are unsigned values, and then assuming they are two's-complement signed values; the result will be unsigned in both cases.

For extra credit, a third repetition should assume one array is signed and the other unsigned.

Re: Exercises to build 6502 coding experience

Posted: Sat Mar 02, 2019 8:58 pm
by drogon
Leading on from hex print, how about simply (!) print a 16-bit value in fixed field decimal. (unsigned) so given $1234 stored in zp $10, $11, low bye first, output ^4660 (where ^ is a space)

This was a programming task I was given as part of a job "interview" a very long time back. However CPU was an 8080 (using z80 mnemonics) which I'd never used before but a couple of years of 6502 stood me in good standing... I got the job, but didn't quite complete it in the hour or so I had - mostly due to 100% unfamiliarity with everything - including the computer (Northstar Horizon running N* DOS) and how to download the code into the target 8080 system to even test it.

I then spent the next year as a PhD research assistant writing 8080 code for the system which was a real-time blood analysis machine, while designing 6502 SBCs for other automation, but that's another story.

-Gordon

Re: Exercises to build 6502 coding experience

Posted: Sat Mar 02, 2019 9:19 pm
by barrym95838
Given a PASCAL-style "counted" ASCII string at a fixed address, find and convert all lower-case alphabetic characters to UPPER-case, leaving control characters and punctuation as-is.

Re: Exercises to build 6502 coding experience

Posted: Sat Mar 02, 2019 9:24 pm
by BigEd
Nice exercises, thanks!

Re: Exercises to build 6502 coding experience

Posted: Sat Mar 02, 2019 10:17 pm
by drogon
Maybe we ought to start to provide solutions ;-)

Lets also not concern ourselves over the fact that from 10 programmers, you'll get (at least) 11 solutions too... e.g. hex print - I have 2 solutions.. And then where do you draw the line when it comes to handling 16-bit numbers - like clearing 1000 bytes - having done a bit of Sweet-16 recently (including a re-implementation of sweet-16), would writing it in sweet16 be acceptable?

-Gordon

Re: Exercises to build 6502 coding experience

Posted: Sat Mar 02, 2019 10:28 pm
by BigEd
Hmm, I think this thread will stand much better if it doesn't have solutions. Feel free though, to start a new thread with some solutions. I can link to it from the head post here.

Re: Exercises to build 6502 coding experience

Posted: Sun Mar 03, 2019 1:21 am
by Druzyek
Project Euler has a lot of fun problems you can solve in any language. The site keeps track of your progress if you log in, and you can sort the problem list by difficulty. You could reasonably do some of the easier ones in 6502 assembly for practice. For example:

Problem 9:
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,

a^2 + b^2 = c^2
For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.

There exists exactly one Pythagorean triplet for which a + b + c = 1000.

Find the product abc.

Re: Exercises to build 6502 coding experience

Posted: Sun Mar 03, 2019 3:32 am
by barrym95838
Project Euler looks cool, Druzyek. I have had a nice experience participating in the Rosetta Code wiki as well. Be aware that a bit of petty bickering can pop up from time to time over the edits, though ...

Re: Exercises to build 6502 coding experience

Posted: Sun Mar 03, 2019 10:16 am
by BigEd
drogon wrote:
Maybe we ought to start to provide solutions ;-)
-Gordon
I've started a new thread for anyone who'd like to do that. I think it's best for this thread only to be for the sharing of suitable exercises - and I also think it's best to tackle the problems yourself, maybe creating several solutions, before seeing how other people did it. I'm afraid I can resist everything except temptation, and I always look in the back of the book. Don't be me!

Re: Exercises to build 6502 coding experience

Posted: Wed Mar 20, 2019 8:54 pm
by BigEd
Just turned up nearby, could well be a good exercise: base64-encode a string

And a related problem: write an srecord loader

And even: write a command line interpreter. The prompt should be '*' and the first command to implement is 'help'. The second command to implement is 'go' which has an address as argument, in hex.

Re: Exercises to build 6502 coding experience

Posted: Wed Mar 20, 2019 9:04 pm
by drogon
BigEd wrote:
Just turned up nearby, could well be a good exercise: base64-encode a string

And a related problem: write an srecord loader

And even: write a command line interpreter. The prompt should be '*' and the first command to implement is 'help'. The second command to implement is 'go' which has an address as argument, in hex.
And would dots be used to shorten commands?

If-so, then I have 90% of that written... ;-)

-Gordon

Re: Exercises to build 6502 coding experience

Posted: Wed Mar 20, 2019 9:07 pm
by BigEd
It still might be a good exercise to write it from scratch! (Note that this thread isn't about accumulating a library, it's about learning the idioms of the 6502.)