Motorola 68000 Series Hardware Assembly Tutorial Part 2

in #programming7 years ago (edited)

Welcome Back!
If you have not yet checked out part 1 of this series you can find it here: https://steemit.com/programming/@sacredbanana/motorola-68000-series-hardware-assembly-tutorial-part-1

MC68000

The Registers
Last time we learnt about the memory and how it is a place to store all your code and data. There is another location to store data on the 68000 and that is the registers. The 68000 has 8 data registers labelled d0 to d7 and 8 address registers labelled a0 to a7. The advantage of using these registers as opposed to the RAM is that the CPU can access the registers hundreds of times faster than it can access RAM. Therefore, if we want our programs to be super speedy, we want to use the registers as much as we can. All the data and address registers are 32 bit, meaning they can store data up to 32 bits (long word) in length. Data registers specialise in storing data and address registers specialise in storing memory addresses.

One Very Quick Thing to Know Before We Get to the Actual Programming
The structure of a line of an assembly program consists of 4 main fields:
LABEL INSTRUCTION OPERAND COMMENT
These 4 fields are separated by either a tab or a space, it's your choice. The assembler accepts both. Not all fields are required, but if you skip the LABEL then most assemblers expect you to have a tab or a space before you write the INSTRUCTION because it may treat your INSTRUCTION as a LABEL.

A LABEL is just a marker for the line of code and is used to mark sections of your code and enables for easy jumping from one location to another. It is not necessary but it is considered good practice to end a LABEL with a colon (:). Labels are optional.

An INSTRUCTION is the instruction you wish the CPU to perform.

An OPERAND is extra information required by some instructions. If an operand is not required for the particular INSTRUCTION then the next field will be treated as a COMMENT.

A COMMENT is a note for yourself to remind you what the line does. These are highly mandatory for your sanity because you can easily forget what a section of code does. The semicolon character (;) or the asterisk character (*) before the comment will tell the assembler to ignore this line so you can write a whole line of comments if you wish. Although semicolons are not required on comments in the last field of the line of code, many programmers such as myself add them anyway so comments are easily spotted. Here is an example of a line of 68000 assembly code:

ProgramStart: move #100,d0 ;moves the value 100 into d0
ProgramStart: is the label, move is the instruction, #100,d0 is the operand and the rest is a comment.

Just a quick note, labels may or may not be case sensitive depending on your assembler. Instructions and operands and not case sensitive. If you prefer to write MOVE instead of move then that is fine.

Our first instruction!
Now that we have finally talked about the basics of how CPUs and memory work we can actually start learning how to program this baby! The instruction we will learn today is the MOVE instruction. Can you guess what this instruction does? I'll give you a hint: It starts with the letter M. If you guessed that it has something to do with moving then you are extremely intelligent and I like you! The MOVE instruction moves a byte, word or long word from one location to another. This location can either be a register or memory. Let's start with a basic example:

MOVE #100,d0

This moves the value 100 into register d0. The # symbol means move the immediate value. If you omit the # symbol, instead it will move the contents of location 100 in RAM into d0. We will be dealing with memory next lesson. But for now just know that if you want to move a value of your choosing into a location you have to precede it with #.

d0 now contains the decimal value 100. If we wish to move a hexadecimal number instead of decimal we use the $ sign.

MOVE #$100,d0

d0 now contains the hex value 100. It's rarely used but if you want to move binary you can use the % sign.

MOVE #%110101100,d0

d0 now contains the binary value 110101100.

There are 3 letters you can attach at the end of the MOVE instruction to say what the size of the move is.

  • MOVE.b - Moves a byte
  • MOVE.w - Moves a word (if you do not specify a letter, then w is the default)
  • MOVE.l - Moves a long word

Here's why specifying the size is important. What if our code looks like this:

move.l #$11111111,d3
move.w #$2da1,d3
move.b #$ff,d3

What value will d3 contain? After the first line is executed the full contents of d3 will be hex 11111111. After the second line the contents is now hex 11112da1. After the last line the contents is hex 11112dff. What if the last line was a move.l instead of a move.b? The contents would then instead be hex 000000ff, since ff is the same value as 000000ff but in that long word instruction you are moving that whole thing into d3.

Okay, this is just complicated
Don't worry! It's really not complicated once you've gotten the hang of it. Let's put all this into practice. If you want to follow along and you use Windows then go right ahead and download Easy68k from http://www.easy68k.com. Otherwise you can just follow along by looking at the screenshots.

1.JPG

Open Easy68k and you will be greeted by an empty project template. You can see Easy68k automatically colours our syntax in to make it more readable. Comments are blue, labels are purple, instructions are green and operands are black. The ORG (origin) line simply tells the assembler at what location to place the code. Let's begin by typing in our example code as shown here.

2.JPG

Once your code looks like above then go ahead and assemble the program by clicking on the play button on the far right of the top toolbar then click Execute.

3.JPG

Once on this screen click the play button (second button on the toolbar) to actually run the program.

4.JPG

The top of the window shows the contents of all the registers. You can see that our program moved 5 and 3ff3 into registers d0 and d1. Great! Our first 68k program! This window also contains buttons for rewinding and stepping through your program line by line to see the results of every line of your code. Feel free to experiment with all these features in all your programs.

5.JPG

Now close that window and modify the code as pictured above. Once again, assemble it with the play button and in the new window push the play button on that to run the program.

6.JPG

This is the result of the new program. Is it how you expected? Let's continue. Close that window.

7.JPG

Modify the program as shown above. Play then play :)

8.JPG

Hopefully you should be starting to get the hang of this. If not, keep playing around and feel free to ask questions in the comments. Close that window.

9.JPG

Let's just add a binary instruction in there to get all bases covered. Run and run.

10.JPG

Remember that you put in a binary number in the register but the window here represents everything in hexadecimal. It's exactly the same number though. Close the window.

11.JPG

Let's just put a number in an address register. Address registers only accept word or long word moves so this instruction is valid. Run and run.

12.JPG

You can see here that register a0 contains hex 20000. Don't close the window yet.

Quick look at memory before we leave
In the top menu click View, Memory. This will open the memory map.

13.JPG

Type in 1000 in the location field circled above then hit enter. You will then be shown the memory contents starting at location 1000. This is what your code looks like in memory! We will be going into this in more depth in the near future.

I hope you enjoyed this. It may seem daunting but if you stick with it, you will actually begin to have the most fun you've ever had! Learning assembly programming probably raises your IQ by at least hex 3F.

See you next time! Questions and feedback appreciated!

Coin Marketplace

STEEM 0.19
TRX 0.13
JST 0.030
BTC 63570.02
ETH 3400.95
USDT 1.00
SBD 2.56