# M68K assembly

[Courses](https://asm-editor.specy.app/learn/courses.md) › M68K assembly

Web page: https://asm-editor.specy.app/learn/courses/m68k

> Start with simple M68K programs, then work through arrays, subroutines, graphics and small games.

By Specy

This course is about the Motorola 68000, and about writing programs for it.

If you have never written assembly before, read
[Assembly basics](https://asm-editor.specy.app/learn/courses/assembly-basics.md) first. It goes through registers, memory,
branching and the stack quickly. This course takes all of that for granted and covers the same
ground again, slowly, on the 68000.

## Introduction

- [Getting started with M68K](https://asm-editor.specy.app/learn/courses/m68k/introduction/getting-started.md): Meet the Motorola 68000, see what assembly programming reveals about a computer, and preview the journey from machine fundamentals to interactive programs.
- [Data and address registers](https://asm-editor.specy.app/learn/courses/m68k/introduction/data-and-address-registers.md): Meet the M68K data and address registers, learn what an address represents, and see why a7 is also called sp.
- [Memory, big endian and sizes](https://asm-editor.specy.app/learn/courses/m68k/introduction/memory-big-endian-and-sizes.md): How byte-addressed memory holds words and longs, how hexadecimal makes their bytes visible, and how big-endian order and alignment govern access.
- [Bytes, words and longs](https://asm-editor.specy.app/learn/courses/m68k/introduction/bytes-words-and-longs.md): How M68K byte, word and long operations affect data registers, how signed readings depend on width, and how to sign-extend a value with ext.
- [The M68K instruction set](https://asm-editor.specy.app/learn/courses/m68k/introduction/the-m68k-instruction-set.md): A beginner's map of M68K instruction purposes and a first working set for copying, adding and subtracting register values.
- [Addressing modes](https://asm-editor.specy.app/learn/courses/m68k/introduction/addressing-modes.md): Learn how M68K operands name registers, literals and memory, then calculate effective addresses with address registers, displacements, indexes and automatic updates.
- [The condition code register](https://asm-editor.specy.app/learn/courses/m68k/introduction/the-condition-code-register.md): The five CCR flags X, N, Z, V and C, how sized results set them, and how move, add, sub, ext, cmp and tst affect them.
- [org, equ, dc and ds](https://asm-editor.specy.app/learn/courses/m68k/introduction/org-equ-dc-and-ds.md): Use labels and the org, equ, dc and ds directives to choose addresses, define constants, place known data and reserve uninitialised memory.
- [Memory regions and executable files](https://asm-editor.specy.app/learn/courses/m68k/introduction/memory-regions-and-executable-files.md): Map M68K code and data placed with org, dc and ds, then compare this editor's Build with sections and loading in executable files.

## Think in assembly

- [Compare and branch](https://asm-editor.specy.app/learn/courses/m68k/think-in-assembly/compare-and-branch.md): Use CCR flags to guide control flow with bra and conditional branches. Compare signed and unsigned values, test zero, and build forward if and if/else shapes.
- [Loops and dbra](https://asm-editor.specy.app/learn/courses/m68k/think-in-assembly/loops-and-dbra.md): Build top-tested and count-controlled loops, then use dbra with its N minus one count, word-sized counter and zero-count guard.
- [Arithmetic, logic and bits](https://asm-editor.specy.app/learn/courses/m68k/think-in-assembly/arithmetic-logic-and-bits.md): Multiply and divide with the 68000's word-sized forms, then use Boolean masks, shifts, rotates, carry and bit instructions on data registers.
- [Arrays, strings and (a0)+](https://asm-editor.specy.app/learn/courses/m68k/think-in-assembly/arrays-strings-and-postincrement.md): Traverse and index equal-size array elements, then measure and copy zero-terminated byte strings safely.
- [The stack, -(sp) and movem](https://asm-editor.specy.app/learn/courses/m68k/think-in-assembly/the-stack-and-movem.md): Use sp to push and pop temporary values, trace the stack in memory, and save or restore register lists with movem.
- [bsr, rts, link and unlk](https://asm-editor.specy.app/learn/courses/m68k/think-in-assembly/bsr-rts-link-and-unlk.md): Calling and returning on the M68K, arguments in registers and arguments on the stack, and the two instructions that build and tear down a stack frame with a6 as the frame pointer.

## Talking to the outside world

- [trap #15 and its tasks](https://asm-editor.specy.app/learn/courses/m68k/talking-to-the-outside-world/trap-15-and-its-tasks.md): How this editor uses trap #15 and a task number in d0.b to print strings and numbers, read a decimal number, and end a program.
- [The screen, keyboard and mouse through traps](https://asm-editor.specy.app/learn/courses/m68k/talking-to-the-outside-world/screen-keyboard-and-mouse.md): This M68K has no device at any address: the screen, the keyboard and the mouse are all trap #15 tasks. Drawing, colours, double buffering, key state and the pointer, each with a program you can run.
- [Exceptions and the vector table](https://asm-editor.specy.app/learn/courses/m68k/talking-to-the-outside-world/exceptions-and-the-vector-table.md): Handle exceptions and traps on the 68000, and see how the processor returns to a program.
- [Interrupts and cycles](https://asm-editor.specy.app/learn/courses/m68k/talking-to-the-outside-world/interrupts-and-cycles.md): Handle timer and keyboard interrupts, control their priority with the interrupt mask, and measure code with the cycle counter.

## Examples

- [Moving values around](https://asm-editor.specy.app/learn/courses/m68k/examples/moving-values.md): Two numbers in registers, added and doubled into a perimeter, with the answer read in the registers panel.
- [Variables in memory and constants](https://asm-editor.specy.app/learn/courses/m68k/examples/variables-in-memory.md): Two longs the assembler writes into memory, a constant that never leaves the instruction, and the total written back where you can see it.
- [The bigger of two numbers](https://asm-editor.specy.app/learn/courses/m68k/examples/if-else.md): A compare and a branch pick the larger of two registers, and a second branch turns the difference into a distance.
- [Fill an array with the numbers from 1 to 10](https://asm-editor.specy.app/learn/courses/m68k/examples/counting-loop.md): A dbra loop that writes ten words into reserved room, stepping the pointer itself, with the result read in the memory panel.
- [Sum of an array](https://asm-editor.specy.app/learn/courses/m68k/examples/sum-of-an-array.md): Six numbers sitting in memory, added up with a pointer that steps through them, and the total left in d0.
- [The largest element](https://asm-editor.specy.app/learn/courses/m68k/examples/max-of-an-array.md): One walk through eight words, keeping the best one seen so far and where it was, with a negative number in there to make the signed condition matter.
- [Length of a string](https://asm-editor.specy.app/learn/courses/m68k/examples/string-length.md): A walk to the zero byte at the end of a string, with the length worked out as the difference between two addresses.
- [Reverse a string in place](https://asm-editor.specy.app/learn/courses/m68k/examples/reverse-a-string.md): Two pointers that start at the ends of a string and swap bytes on their way towards each other, with no copy anywhere.
- [Multiply and divide, with the remainder](https://asm-editor.specy.app/learn/courses/m68k/examples/multiply-and-divide.md): Days into hours with mulu, seconds into minutes and seconds with divu, and the two answers a division packs into one register pulled apart.
- [Even or odd, count the set bits, multiply by shifting](https://asm-editor.specy.app/learn/courses/m68k/examples/bit-tricks.md): Four questions about one number answered with a bit test, a shift, a mask and a loop that reads the carry flag.
- [A subroutine with its arguments in registers](https://asm-editor.specy.app/learn/courses/m68k/examples/subroutine-with-register-arguments.md): Euclid's greatest common divisor as a subroutine called with bsr, its arguments in d0 and d1 and its answer in d0.
- [Stack arguments and a stack frame](https://asm-editor.specy.app/learn/courses/m68k/examples/subroutine-with-stack-arguments.md): Two arguments pushed by the caller, a local that survives a call inside a link frame, and a subroutine that gives back the register it borrowed.
- [Recursion: factorial and Fibonacci](https://asm-editor.specy.app/learn/courses/m68k/examples/factorial-and-fibonacci.md): Two subroutines that call themselves, each call with a stack frame of its own, and the word sized multiply that puts a limit on how far factorial can go.
- [Bubble sort](https://asm-editor.specy.app/learn/courses/m68k/examples/bubble-sort.md): Eight words sorted where they lie by an inner loop that swaps neighbours and an outer loop that keeps sending it back.
- [A jump table](https://asm-editor.specy.app/learn/courses/m68k/examples/jump-table.md): Four addresses in memory, an index that picks one of them, and a jmp through an address register instead of a chain of comparisons.
- [Binary search](https://asm-editor.specy.app/learn/courses/m68k/examples/binary-search.md): Twelve sorted words searched by halving the range four times, with the index left in d3 and -1 when the value is not there.
- [A 2D array](https://asm-editor.specy.app/learn/courses/m68k/examples/two-dimensional-array.md): Three rows of four words, one element reached by row and column, and a whole column added up by stepping a row at a time.
- [Print a string](https://asm-editor.specy.app/learn/courses/m68k/examples/hello-world.md): Print two lines with trap #15: a greeting, then a string followed by a signed number.
- [Read two numbers and print their sum](https://asm-editor.specy.app/learn/courses/m68k/examples/sum-of-two-numbers.md): Two prompts that wait for you to type, the numbers coming back in d1, and their sum printed on a line of its own.
- [Print a number in any base without help](https://asm-editor.specy.app/learn/courses/m68k/examples/number-to-string.md): Repeated division for the digits, a buffer filled backwards from its end, and the same number printed in base 16, 10 and 2.
- [Drawing shapes on the screen](https://asm-editor.specy.app/learn/courses/m68k/examples/drawing-on-the-screen.md): Draw a house, sun and sky with screen traps for colours, shapes, lines, flood fill and text.
- [A bouncing ball](https://asm-editor.specy.app/learn/courses/m68k/examples/bouncing-ball.md): A ball that reverses at the Screen edges, with complete frames, a short delay, and a bar showing elapsed program time.
- [Move a square with the keyboard](https://asm-editor.specy.app/learn/courses/m68k/examples/keyboard-control.md): Task 19 polled once a frame for the four arrow keys, a direction kept in memory, and a square that keeps going until you steer it somewhere else.
- [The snake game](https://asm-editor.specy.app/learn/courses/m68k/examples/snake.md): Play Snake while tracing packed board cells, a body shifted by a loop, arrow key steering, food placement, and a score drawn each frame.
- [Flappy ASM](https://asm-editor.specy.app/learn/courses/m68k/examples/flappy-asm.md): A game with a state machine in it: three states, gravity counted in sixteenths of a pixel, a press that only counts on the frame it began, and three pipes recycled into an endless course.
- [Play a WAV and a MIDI file](https://asm-editor.specy.app/learn/courses/m68k/examples/play-sound-files.md): Create a square-wave WAV for the standard player and a four-note MIDI file for the DirectX player.
- [Chiptune](https://asm-editor.specy.app/learn/courses/m68k/examples/chiptune.md): Play a short tune on the Synth: a pulse lead, a triangle bass and noise drums on three channels, stepped by a table in memory.
- [Sampled sound](https://asm-editor.specy.app/learn/courses/m68k/examples/sampled-sound.md): Compute a plucked sound into memory, upload it into the Synth's Sample memory and play it at eight pitches by changing the playback rate.

## Optional

- [The whole M68K assembly course](https://asm-editor.specy.app/learn/courses/m68k/llms-full.txt): every Lecture in order in one file, to search through
