Exercises to build 6502 coding experience

Programming the 6502 microprocessor and its relatives in assembly and other languages.
Post Reply
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Exercises to build 6502 coding experience

Post 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.)
Last edited by BigEd on Sun Mar 03, 2019 10:14 am, edited 1 time in total.
User avatar
Arlet
Posts: 2353
Joined: 16 Nov 2010
Location: Gouda, The Netherlands
Contact:

Re: Exercises to build 6502 coding experience

Post by Arlet »

Assuming a UART "PutChar" routine, write a routine to output a hexadecimal nibble, and then a routine to output a hexadecimal byte.
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: Exercises to build 6502 coding experience

Post by BigEd »

Nice one, thanks!
Chromatix
Posts: 1462
Joined: 21 May 2018

Re: Exercises to build 6502 coding experience

Post 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.
User avatar
drogon
Posts: 1671
Joined: 14 Feb 2018
Location: Scotland
Contact:

Re: Exercises to build 6502 coding experience

Post 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
--
Gordon Henderson.
See my Ruby 6502 and 65816 SBC projects here: https://projects.drogon.net/ruby/
User avatar
barrym95838
Posts: 2056
Joined: 30 Jun 2013
Location: Sacramento, CA, USA

Re: Exercises to build 6502 coding experience

Post 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.
Got a kilobyte lying fallow in your 65xx's memory map? Sprinkle some VTL02C on it and see how it grows on you!

Mike B. (about me) (learning how to github)
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: Exercises to build 6502 coding experience

Post by BigEd »

Nice exercises, thanks!
User avatar
drogon
Posts: 1671
Joined: 14 Feb 2018
Location: Scotland
Contact:

Re: Exercises to build 6502 coding experience

Post 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
--
Gordon Henderson.
See my Ruby 6502 and 65816 SBC projects here: https://projects.drogon.net/ruby/
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: Exercises to build 6502 coding experience

Post 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.
User avatar
Druzyek
Posts: 367
Joined: 12 May 2014
Contact:

Re: Exercises to build 6502 coding experience

Post 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.
User avatar
barrym95838
Posts: 2056
Joined: 30 Jun 2013
Location: Sacramento, CA, USA

Re: Exercises to build 6502 coding experience

Post 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 ...
Got a kilobyte lying fallow in your 65xx's memory map? Sprinkle some VTL02C on it and see how it grows on you!

Mike B. (about me) (learning how to github)
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: Exercises to build 6502 coding experience

Post 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!
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: Exercises to build 6502 coding experience

Post 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.
User avatar
drogon
Posts: 1671
Joined: 14 Feb 2018
Location: Scotland
Contact:

Re: Exercises to build 6502 coding experience

Post 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
--
Gordon Henderson.
See my Ruby 6502 and 65816 SBC projects here: https://projects.drogon.net/ruby/
User avatar
BigEd
Posts: 11464
Joined: 11 Dec 2008
Location: England
Contact:

Re: Exercises to build 6502 coding experience

Post 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.)
Post Reply