Category:68000 Assembly: Difference between revisions

m
 
(13 intermediate revisions by the same user not shown)
Line 1:
{{stub}}{{language}}
{{merge language | M680x0 }}
{{language}}
68000 assembly is the assembly language used for the Motorola 68000, or commonly known as the 68K. It should not be confused with the 6800 (which predates it). The Motorola 68000 is a big-endian processor with full 32-bit capabilities (despite most systems that use it being considered 16-bit.) It was used in many computers such as the Amiga or the Canon Cat, as well as game consoles such as the Sega Genesis and Neo Geo.
 
Line 22 ⟶ 24:
 
This difference isn't usually relevant in the majority of situations, so don't concern yourself too much. It's much more important when doing [[6502 Assembly]] where registers are smaller than the address space.
 
===Notation Conventions===
How you write the source code depends on your assembler and the syntax it uses. This page is written using Motorola syntax but there is also Milo syntax which has different conventions.
 
How you go about defining numbers or text in your code varies wildly between assemblers. I'm using VASM and these are the rules I have to follow, but your assembler may be different.
 
* A number with a # in front represents a constant, literal value. For example, the 3 in <code>MOVE.B #3,D0</code> represents the number 3.
 
* A number without a # in front represents a memory location. For example, the 3 in <code>MOVE.B 3,D0</code> represents <i>the byte stored at memory address <code>$00000003</code></i>.
 
* A number that doesn't have a $ or % prefix is a decimal (base 10) value.
 
* A number that begins with a $ is a hexadecimal value.
 
* A number that begins with a % is a binary value.
 
* Single and double quotes can be used for ASCII values. When you type <code>MOVE.L "EGGS",D0</code> for example, this is equivalent to <code>MOVE.L #$69717183,D0</code>.
 
* The operand of <code>BTST</code>,<code>BSET</code>,<code>BCLR</code>, or <code>BCHG</code> represents a bit position, starting at the rightmost binary digit as 0 and counting up from right to left. For example, <code>BCLR #3,D0</code> performs the same bitwise operation that you would get from doing <code>AND.B #%11110111,D0</code>.
 
* The operand before the comma is the "source", and the operand after is the "destination." For example, <code>MOVE.L D3,D2</code> takes the value in D3 and stores it into D2, not the other way around. This is the opposite of x86 and ARM, which have the source on the right and the destination on the left.
 
 
Data blocks, on the other hand, begin with <code>DC.B</code>, <code>DC.W</code>, or <code>DC.L</code> and each represents a constant numeric value. Strangely, you do NOT prefix these with # to signify them as constants (doing so will cause an error on most assemblers). However, you can use the $ or % modifiers to denote hexadecimal or binary.
 
Keep in mind that there is no requirement to use hexadecimal, decimal, or binary in your source code. It all gets converted to binary anyway. However, it is recommended to use the notation that is appropriate for how your data is meant to be interpreted, for readability purposes.
 
===Data Registers===
Line 86 ⟶ 114:
 
====The Stack====
The 68000's stack is commonly referred to as <code>SP</code> but it is also address register <code>A7</code>. This register is handled differently than the other address registers when pushing bytes onto the stack. A byte value pushed onto the stack will be padded to the <b>right</b> with zeroes. The stack needs to pad byte-length data so that it can stay word-aligned at all times. Otherwise the CPU would crash as soon as you tried to use the stack for anything other than a byte! If a byte is popped, the zeroes are placed on the left side of the lower word of the data register, and the actual byte goes in the right side.
 
<lang 68000devpac>MOVE.B #$FF,-(SP) ;push #$FF then #$00 onto the stack, in that order.
MOVE.B (SP)+,D0 ;The values are popped in the order #$00 #$FF.</lang>
 
You can abuse this property of the stack to quickly swap bytes around. Suppose you had a number like <code>#$11223344</code> stored in <code>D0</code> and you wanted to change it to <code>#$11224433</code>:
 
<lang 68000devpac>MOVE.W D0,-(SP) ;push #$3344 onto the stack
MOVE.B (SP)+,D0 ;pop them in the order #$44 #$33.</lang>
 
===Length===
Line 115 ⟶ 135:
MOVE.L #0,D3 ;D3 = #$00000000</lang>
 
Loading immediate values into address registers is different. You can only move words or longer into address registers, and if you move a word, the value is sign-extended. This means that if the top nibble of the word is 8 or greater, the value gets padded to the left with Fs, and is padded with zeroes if the top nibble is 7 or less. ItIf you're adding a constant value less than 7FFF to an address, it's bestusually safe to alwaysuse movethe longsword intolength addressoperation, registers.which Thattakes wayless youbytes knowto whatencode you'rethan the long length gettingversion.
 
<lang 68000devpac>MOVEA.W #$8000,A4 ;A4 = #$FFFF8000. Remember the top byte is ignored so this is the same as #$00FF8000.
Line 164 ⟶ 184:
 
* It's best to push all registers (D0 thru D7 and A0-A6) onto the stack at the start and pop them off at the end. This is not required for the traps you call with the <code>TRAP #?</code> command, but for the others it's absolutely necessary, since you can't know in advance when they'll happen. You can leave out A7 when doing this since that's the stack pointer itself.
 
==Interrupts==
The 68000 supports 7 different interrupts, often called IRQs or Interrupt Requests. Enabling interrupts is often a twofold process: first, the interrupt source must be enabled, which is usually an implementation-defined process that involves interacting with memory-mapped ports. Second, the status register must be set accordingly to allow the interrupt to occur, which can be achieved with <code>MOVE #$2x00,SR</code> where X is the desired interrupt level. X can range from 0 to 7, and for an interrupt to occur, its interrupt level (which is determined by the hardware implementation and the placement of the desired address in the vector table) must be greater than X or it will not happen (even if the source is enabled.)
 
==Alignment==
Line 215 ⟶ 238:
#[[https://www.chibiakumas.com/68000/ ChibiAkumas Motorola 68000 Tutorial]]
#[[http://www.easy68k.com/paulrsm/doc/trick68k.htm 68000 Tricks and Traps]]
 
{{merge language | M680x0 }}
1,489

edits