The Ellard Guide to Writing in Assembly


  1. Define your algorithm.

    For this assignment, we give you the precise algorithms for the code you need to write, so this shouldn't be a problem. You may find it useful to modify these algorithms slightly, in order to express them in ANT, but make sure that you have the basic ideas totally nailed down before diving into the code.

  2. Write your program in C (or at least in pseudo-C).

    If you can't precisely specify what it is that your algorithm is supposed to do and how to do it, then there's little chance you'll get it right be tinkering with it in assembly language. Since the beginning of the semester, you have been learning how to express algorithms precisely by writing them in C. Use this experience!

    Note that the most appropriate or elegant algorithms in C may not be the most appropriate or elegant algorithms in ANT. Think about what's going to be easiest to express in ANT; it may change your algorithm.

  3. Study the ANT references and look over the examples from lecture and the course notes.

    If you don't know how to express yourself in ANT, then even the best algorithm is doomed to failure.

  4. Divide the algorithm into smaller and smaller pieces.

    Eventually, each piece of the algorithm should start to look like something that you can easily convert into a sequence of ANT instructions and/or data declarations.

  5. Decide what temporary variables you need, choose registers to keep them all in, and document this.

    Do this before you write any code, and once you start to write code be very reluctant to change the register allocations without good reason! If you change the meaning of a register in one part of your code, you may need to change many other parts of your code in order to avoid clobbering something else.

  6. Don't try to write everything at once.

    Write small pieces that you can check and test carefully. An example of this in included later in this handout.

    Make use of the ANT debugger (ad) to test your code. You can execute your code line by line, checking to make sure that what is in the registers and data memory is what you expect after each step.

  7. Learn the debugger.

    ANT comes with a decent debugger. Learn your way around it! You will not be able to debug your programs by staring at them- most people find the debugger invaluable (the rest find ANT programming impossible, because they can't find their bugs without using the debugger).

    The debugger takes five minutes to learn and can save you hours.

  8. Use checkin early and often, and use scratch files.

    Make sure you keep track of what each scratch file is used for (i.e. what idea you were experimenting with in it) so you can pick out the good stuff later. There's no reason why the programs you need to write for this assignment should be the only ANT programs you ever write!


Please bring any errors or inconsistencies in this document to the attention of the author.

Dan Ellard - (ellard@deas.harvard.edu)