M68K Complete Documentation

Addressing Modes

Direct
Gets the content in the register directly. (the SP register is an alias of the a7)
d0, a0, sp
Dn
Data register
An
Address register
Indirect
Gets the value contained in memory with address being the content of the address register specified. Specifiying an offset by writing a number before the (), the addressing mode becomes indirect with displacement and the final address to read the memory will be (address + offset).
(a0), 4(sp)
(An)
Indirect
Indirect Post/Pre increment
Gets the value contained in memory with address being the content of the address register specified. If it's the post increment, the address register will be incremented after reading the memory. If it's the pre increment, the address register will be incremented before reading the memory. The amount of increment is specified by the size of the instruction. In the documentation, wherever there is (An), this addressing mode is valid too
(a0)+, -(sp)
(An)
Post increment
(An)
Post increment
Immediate
Represents a numerical value, it can be a number or a label. When the program is assembled, the labels will be converted to the address of the label. Immediate values can be represented in many bases. (replace <num> with the actual number). Note, a string will be represented as a list of bytes.
#1000, #$FF, #@14, #%10010, #'a', #'hey', #label
Im
Immediate
#<num>
Decimal
#$<num>
Hexadecimal
#@<num>
Octal
#%<num>
Binary
#'<char/string>
Text
Effective address
Represents the address of the memory where the data is stored. It can be a label or a number. When the program is assembled, the labels will be converted to the address of the label.
$1000, some_label, 140, %101010, @22, 'e'
ea
Effective address
<ea>
Effective address
Base displacement indirect
Gets the value contained in memory with address being the sum of (address + offset + base), where the first register (address) will be the base address, the second register (base) and offset being the number before the ().
In the documentation, wherever there is (An), this addressing mode is valid too
4(a0, d2), (sp, a0)
(An)
Base displacement indirect

Condition Codes

When performing operations, the CPU will set condition codes in the status register after the instruction is executed.
For example the tst, cmp instructions will set the condition codes that represent the result of the comparison of the operands.
The following are all the condition codes available:
hi
Unsigned higher
ls
Unsigned lower or same
cc
Carry clear
cs
Carry set
ne
Not equal
eq
Equal
vc
Overflow clear
vs
Overflow set
pl
Plus
mi
Minus
ge
Greater or equal
lt
Less than
gt
Greater than
le
Less than or equal
hs
Unsigned higher or same
lo
Unsigned lower
The instructions that use the condition codes are: bcc dbcc scc

Condition codes flags

The condition codes X, N, Z, V, C are the individual flags that can be set in the status register.

X is the extend flag, it is set when the result of an operation is too large to fit in the destination register.
N is the negative flag, it is set when the result of an operation is negative.
Z is the zero flag, it is set when the result of an operation is zero.
V is the overflow flag, in arithmetical operations, it is set if it caused the result to overflow.
C is the carry flag, when an operation causes a carry, like an addition or a shift, it is set to the value of the carry.
hi
!c && !z
ls
c || z
cc
!c
cs
c
ne
!z
eq
z
vc
!v
vs
v
pl
!n
mi
n
ge
(n && v) || (!n && !v)
lt
(n && !v) || (!n && v)
gt
(n && v && !z) || (!n && !v && !z)
le
z || (n && !v) || (!n && v)
hs
!c
lo
c

Shift Directions

The shift logical, shift arithmetical and rotate, instructions use the directions to specify in which direction the bits are shifted.
l
Left
r
Right
Those instructions are: lsl lsr rol ror asl asr

Directives

dc
(b, w, l)

Defines constants, following the directive there can be a list of constants separated by commas, the size of each constant depends on the selected size. If no size is selected, the size is determined by the value of the constant. If the constant is a string, it will be stored as a sequence of bytes, if it is a number, it will be stored as a sequence of words

dc.b 'Hello world!', 4, %10, $F, @8, 'a', some_label
ds
(b, w, l)

Defines a space in memory of N elements, the size of each element depends on the specified size, the content of the space is undefined

ds.l 100
dcb
(b, w, l)

Defines a space in memory of N elements, the size of each element depends on the specified size, the content of the space is initialized to the second operand

dcb.b 50, 1
org

Sets the current position in memory for the following instructions

org $1000
equ

Defines a constant that will be replaced by the value when the program is assembled

name equ 10

Assembler Features

Immediate and absolute arithmetics
Indirect and absolute values allow expressions to be used, it will be calculated at assemble time.
#label+2, #$F*10, #450-%1010, #@5834/4, #($FF+%1010)*3

Instructions

add

(b, w, l) {w}
Op 1
Dn/An/(An)/Im/ea/(An, Xn)
Op 2
Dn/(An)/An/ea/(An, Xn)

Adds the value of the first operand to second operand. If the second operand is an address register, the ADDA instruction is used instead.

add.l (a4, d3), d1

adda

(l, w) {w}
Op 1
Dn/An/(An)/Im/ea/(An, Xn)
Op 2
An

Adds the value of the first operand to second operand. It does not change the SR. When using word size, the first operand is sign extended to long and the second is read and written as a long.

adda.l d0, a0

addi

(b, w, l) {w}
Op 1
Im
Op 2
Dn/(An)/ea/(An, Xn)

Adds the immediate value to the second operand

addi.w #4, d1

addq

(b, w, l) {w}
Op 1
Im
Op 2
Dn/(An)/An/ea/(An, Xn)

Adds the value of the first operand to second operand. The first operand value must be between 1 and 8. If the destination is a address register, it is always treated as a long, and the condition codes are not affected.

addq.w #4, d1

and

(b, w, l) {w}
Op 1
Dn/(An)/Im/ea/(An, Xn)
Op 2
Dn/(An)/ea/(An, Xn)

Performs a logical AND between the first and second operand, stores the result in the second operand

and.l d0, d1

andi

(b, w, l) {w}
Op 1
Im
Op 2
Dn/(An)/ea/(An, Xn)

Performs a logical AND between the first immediate value and second operand, stores the result in the second operand

andi.l #$FF, (a0)

asd

(b, w, l) {w}
Op 1
Dn/(An)/Im/ea/(An, Xn)
Op 2
Dn/(An)/ea/(An, Xn)

Shifts the bits of the second operand to the {direction} as many times as the value of the first operand, depending on the specified size. The new bits are filled with the sign bit. Defaults to word. Note: ASL sets the overflow flag if the MSB changes during the shift, while ASR always clears it

`as<d> d0, d3` Where d is either (l)eft or (r)ight

bcc

Op 1
ea

Branches to the specified address if {condition code}

`b<cc> label` Where cc is one of the condition codes

bchg

Op 1
Dn/(An)/Im/ea/(An, Xn)
Op 2
Dn/(An)/ea/(An, Xn)

Inverts the bit of the second operand at the position of the value of the first operand

bchg #%101, d3

bclr

Op 1
Dn/(An)/Im/ea/(An, Xn)
Op 2
Dn/(An)/ea/(An, Xn)

Clears the bit of the second operand at the position of the value of the first operand

bclr d2, d7

bra

Op 1
ea

Branches to the specified address unconditionally

bra $2000

bset

Op 1
Dn/(An)/Im/ea/(An, Xn)
Op 2
Dn/(An)/ea/(An, Xn)

Sets to 1 the bit of the second operand at the position of the value of the first operand

bset #1, d1

bsr

Op 1
ea

Branches to the specified address and stores the return address in the stack

bsr label

btst

Op 1
Dn/(An)/Im/ea/(An, Xn)
Op 2
Dn/(An)/ea/(An, Xn)

Tests the bit of the second operand at the position of the value of the first operand, it changes the Z (zero) flag, the destination operand is not modified

btst #4, d0

clr

(b, w, l) {w}
Op 1
Dn/(An)/ea/(An, Xn)

Sets to 0 all the bits of the destination operand, how many bits are set to 0 depends on the specified size, defaults to long

clr.b d0

cmp

(b, w, l) {w}
Op 1
Dn/An/(An)/Im/ea/(An, Xn)
Op 2
Dn/An

Compares the second operand with the first operand, it sets the flags accordingly which will be used by the branching instructions. Works by subtracting the first operand from the second operand and setting the flags. If the second operand is an address register, the CMPA instruction is used instead.

cmp.l -(sp), d0

cmpa

(l, w) {w}
Op 1
Dn/An/(An)/Im/ea/(An, Xn)
Op 2
An

Compares the second operand with the first operand, it sets the flags accordingly which will be used by the branching instructions. When using word size, the first operand is sign extended to long and the second is read and written as a long.

cmpa.l $1000, a0

cmpi

(b, w, l) {w}
Op 1
Im
Op 2
Dn/(An)/An/ea/(An, Xn)

Compares the second operand with the first operand, it sets the flags accordingly which will be used by the branching instructions.

cmpi.w #10, d3

cmpm

(b, w, l) {w}
Op 1
(An)
Op 2
(An)

Compares two memory regions, only valid operand is the post increment, it sets the flags accordingly which will be used by the branchin instructions.

cmpm.b (a0)+, (a1)+

dbcc

Op 1
Dn
Op 2
ea

Decrements the first operand by 1 and branches to the specified address if {condition code} is false and the first operand is not -1. dbra is the same as dbf (will decrement untill it reaches -1). It reads the operand as a word, so it can run at maximum 64k times

`db<cc> d0, label` Where cc is one of the condition codes

dbra

Op 1
Dn
Op 2
ea

Decrements the first operand by 1 and branches to the specified address if the first operand is not -1. dbcc is the same as dbf (will decrement untill it reaches -1)

dbra d0, label

divs

Op 1
Dn/(An)/Im/ea/(An, Xn)
Op 2
Dn

Divides (signed) the value of the second operand by the value of the first operand (op2 / op1). The quotient is stored in the first 16 bits of the destination register and the remainder is stored in the last 16 bits of the destination register. The first operand is read as a word, the second as a long

divs #2, d1

divu

Op 1
Dn/(An)/Im/ea/(An, Xn)
Op 2
Dn

Divides (unsigned) the value of the second operand by the value of the first operand (op2 / op1). The quotient is stored in the first 16 bits of the destination register and the remainder is stored in the last 16 bits of the destination register. The first operand is read as a word, the second as a long

divu #4, d1

eor

(b, w, l) {w}
Op 1
Dn
Op 2
Dn/(An)/ea/(An, Xn)

Performs a logical XOR between the first and second operand, stores the result in the second operand

eor.l d0, d1

eori

(b, w, l) {w}
Op 1
Im
Op 2
Dn/(An)/ea/(An, Xn)

Performs a logical XOR between the first immediate value and second operand, stores the result in the second operand

eori.l #1, (sp)+

exg

Op 1
Dn/An
Op 2
Dn/An

Exchanges the values of the two operands, only works in 32 bits

exg d0, a1

ext

(l, w) {w}
Op 1
Dn

Extends the sign of the operand, depending on the specified size. If the part to extend is negative, it will be filled with 1s, otherwise it will be filled with 0s. Defaults to word

ext.w d0

jmp

Op 1
(An)/ea

Jumps to the specified address unconditionally

jmp (a0)

jsr

Op 1
(An)/ea

Jumps to the specified address, like the "lea" instruction, when resolving the address, it does not read the memory, so "jsr 4(a0)" will jump to the value of "a0 + 4", the address is loaded and stores the return address in the stack

jsr (sp)

lea

Op 1
(An)/ea
Op 2
An

Loads the address of the first operand into the second operand, when using indirect addressing, the value is not read, only the address is loaded. For example "lea 4(a0), a0" will load a0 + 4 in a1

lea (a0), a1
Op 1
An
Op 2
Im

Pushes to the stack the long content of the address register, sets the address register to the current stack pointer and then decrements the stack pointer by the specified amount

link a0, #-16

lsd

(b, w, l) {w}
Op 1
Dn/(An)/Im/ea/(An, Xn)
Op 2
Dn/(An)/ea/(An, Xn)

Shifts the bits of the second operand to the {direction} as many times as the value of the first operand, depending on the specified size. The new bits are filled with 0s. Defaults to word

`ls<d> #3, d7` Where d is either (l)eft or (r)ight

move

(b, w, l) {w}
Op 1
Dn/An/(An)/Im/ea/(An, Xn)
Op 2
Dn/(An)/An/ea/(An, Xn)

Moves the value from the first operand to second operand. If the second operand is an address register, the MOVEA instruction is used instead.

move.b #10, d0

movea

(l, w) {w}
Op 1
Dn/An/(An)/Im/ea/(An, Xn)
Op 2
An

Moves the value from the first operand to second operand. If the size is word, it is sign extended to long. It does not change the SR. When using word size, the first operand is sign extended to long and the second is written as a long.

movea.l d0, a0

movem

(l, w) {w}
Op 1
Dn/(An)/An/ea/(An, Xn)
Op 2
Dn/(An)/An/ea/(An, Xn)

Move many, useful when you want to save a bunch of registers, for example to save their value when branching to a function it moves a list of registers to memory, or memory to a list of registers. The first operand is the list of registers, the second operand is the memory region. If you define the registers as the first operand, then it will save the registers to memory, if the first operand is the memory, then it will load the registers from memory. You can write the list of registers by separating them with a "/", and the range between registers by using a dash. ex: a3-a5/d0-d2 will select d0, d1, d2, a3, a4, a5. The order of the register will be converted to first data, then address registers, from 0 to 7. When using the pre-decrement operand, the order of the registers will be reversed, going from a7 to a0, and d7 to d0.

movem.l d0-d2, (a0)

moveq

Op 1
Im
Op 2
Dn

Moves the value from the first operand to second operand. The first operand is read as a byte so only values between -127 and 127.

moveq #10, d0

muls

Op 1
Dn/(An)/Im/ea/(An, Xn)
Op 2
Dn

Multiplies the value of the first operand by the second operand. The result is stored in the second operand. The first operand is read as a word, the second as a long

muls d0, d1

mulu

Op 1
Dn/(An)/Im/ea/(An, Xn)
Op 2
Dn

Multiplies (unsigned) the value of the first operand by the second operand. The result is stored in the second operand. The first operand is read as a word, the second as a long

mulu d5, d2

neg

(b, w, l) {w}
Op 1
Dn/(An)/ea

Flips the sign of the operand, depending on the specified size, defaults to word

neg.l d0

nop

nop

not

(b, w, l) {w}
Op 1
Dn/(An)/ea/(An, Xn)

Inverts the bits of the operand depending on the specified size

not.b d0

or

(b, w, l) {w}
Op 1
Dn/(An)/Im/ea/(An, Xn)
Op 2
Dn/(An)/ea/(An, Xn)

Performs a logical OR between the first and second operand, stores the result in the second operand

or.l #$FF, d1

ori

(b, w, l) {w}
Op 1
Im
Op 2
Dn/(An)/ea/(An, Xn)

Performs a logical OR between the first immediate value and second operand, stores the result in the second operand

ori.l #%1100, (a0)

pea

Op 1
(An)/ea

Same as lea, but it pushes the address to the stack

pea (a0)

rod

(b, w, l) {w}
Op 1
Dn/(An)/Im/ea/(An, Xn)
Op 2
Dn/(An)/ea/(An, Xn)

Rotates the bits of the second operand to the {direction} as many times as the value of the first operand, depending on the specified size. Defaults to word

`ro<d> d2, d5` Where d is either (l)eft or (r)ight

rts

Returns from a subroutine, pops the return address from the stack and jumps to it

rts

scc

Op 1
Dn/(An)/ea/(An, Xn)

Sets the first byte of the destination operand to $FF (-1) if flags {condition code} is true, otherwise it sets it to 0

`s<cc> d0` Where cc is one of the condition codes

sub

(b, w, l) {w}
Op 1
Dn/An/(An)/Im/ea/(An, Xn)
Op 2
Dn/(An)/An/ea/(An, Xn)

Subtracts the value of the first operand from second operand and stores in the second. If the second operand is an address register, the SUBA instruction is used instead.

sub.w $1000, d1

suba

(l, w) {w}
Op 1
Dn/An/(An)/Im/ea/(An, Xn)
Op 2
An

Subtracts the value of the first operand from second operand and stores in the second. It does not change the SR. When using word size, the first operand is sign extended to long and the second is read and written as a long.

suba.w #$FF, a1

subi

(b, w, l) {w}
Op 1
Im
Op 2
Dn/(An)/ea/(An, Xn)

Subtracts the immediate value to the second operand

subi #1, d3

subq

(b, w, l) {w}
Op 1
Im
Op 2
Dn/(An)/An/ea/(An, Xn)

Subtracts the value of the first operand from second operand and stores in the second. The first operand value must be between 1 and 8. If the destination is a address register, it is always treated as a long, and the condition codes are not affected.

subq.b #1, d3

swap

Op 1
Dn

Swaps the two word of the register, you can see the register as [a,b] after the swap it will be [b,a]

swap d0

trap

Op 1
Im

Executes a trap, the value of the operand is used as the trap number, only #15 is supported. The register d0 will be used as the trap type which are:

OpcodeDescription
0Print string pointed by a1 with length read in d1.w, null terminated with max of 255, then prints a new line.
1Print string pointed by a1 with length read in d1.w.
2Read string from keyboard, writes the string at address of a1 and overrides the value of d1 with the length of the string.
3Print signed number at d1.
4Read number, writes to d1.
5Read character, writes to d1.
6Print character at d1.
8Get time, writes to d1.
9Terminate.
13Prints null terminated string pointed by a1 then prints new line, errors if string is longer than 16kb, to prevent infinite loops.
14Prints null terminated string pointed by a1, errors if string is longer than 16kb, to prevent infinite loops.
15Prints unsigned number at d1 in base (from 2 to 36) specified in d2.b
23Delays the execution of the simulator by milliseconds of the value in d1.
trap #15

tst

(b, w, l) {w}
Op 1
Dn/(An)/An/ea/(An, Xn)

Compares the operand with 0

tst.b (a0)

unlk

Op 1
An

Sets the SP to the address register, then Pops a long value from the stack and stores the result in the address register

unlk a0