Motorola 68000 Series Hardware Assembly Tutorial Part 3

in #programming7 years ago (edited)

Welcome Back!
If you have not yet checked out the other parts of this series you can find them here:

Part 1: https://steemit.com/programming/@sacredbanana/motorola-68000-series-hardware-assembly-tutorial-part-1-revised
Part 2: https://steemit.com/programming/@sacredbanana/motorola-68000-series-hardware-assembly-tutorial-part-2

m68000

A side note before we begin
In the previous lessons I mentioned that each cycle of the CPU corresponds to one instruction of the CPU, i.e. A 7Mhz CPU will perform at 7 million instructions per second. This is not entirely correct, as each instruction can take several cycles to complete. Simple moving instructions take a lot less cycles than the complicated instructions such as division. This is the true story of the Mhz speed measurement, and you can still see that the more Mhz the CPU, the more instructions get executed per second.

Let's get moving! (literally)
Last time we learnt the MOVE instruction, but we have not even come close to the power this instruction has. All we did last time was move values into registers. Let's try moving into memory instead! Check out this instruction:

MOVE.w #$ff12,100

Recall that # means IMMEDIATE value, that is a value you are supplying right there in the instruction. Also $ denotes the number is in hexadecimal format. What this instruction does is moves the hex value ff12 into memory location 100. What if we want to get this value out of memory and put it in a register? Let's try 2 attempts at this. The first attempt is incorrect and the second gets the job done perfectly.

Attempt 1
MOVE.w #100,d0

What do you think d0 will contain? Surely we are telling it to move what's in address 100 and put it in d0....??
d0 contains the value 100. What? Why? Well, as I explained earlier, the # denotes the value to be immediate. The # tells the CPU to move the value 100 into d0. You might have already guessed this would be the case from our lesson last time.

Attempt 2
MOVE.w 100,d0
OK, now we have removed that blasted # from the instruction. Now d0 contains $ff12! Yay!

You can probably see that we are moving a word from address 100 into d0. Each address only contains 1 byte so what is actually happening behind the scenes is it is moving the $ff from address 100 and also the $12 from address 101 and puts both values into d0. If you only wanted to move the $ff you could simply just change the MOVE.w to a MOVE.b. Addresses can also be in hexadecimal format too, so if you would prefer you can do that, just remember to precede the address with a $.

The power of address registers
Register addresses are registers specifically designed to store memory addresses. You can technically store other data in them but this is not recommended for a few reasons but one reason is address registers do not support all the same functions as data registers.

We already know how to move numbers into address registers... for example:

MOVE.l #$12345678,a4

This will move $12345678 into a4. Remember, you can only move word and long word values into address registers with the move instruction.

Let's now use the address we just put into a4 and store the number 24 into that address:

MOVE.b #24,(a4)

Using brackets in this way means "fetch the contents of this and use that value instead". So basically it fetches the contents of a4, which contains $12345678, and then the value 24 then gets put in address $12345678.

We are going to learn more "addressing modes" in future lessons which will expand the power of MOVE and other instructions.

Yes, we can move from memory to memory without a register:

MOVE.b 100,120

This copies the byte from address 100 to address 120.

The true power of labels
Remember labels? The thing you can put in the first column in a line of code? Here's a reminder:

Start: MOVE.b $12,d3

This instruction is simple enough and is labelled Start. Let's mark a specific location in memory to store some data:

Start: MOVE.b $12,d3
SIMHALT
SecretStorage: dc.b 10

OK... just one note with SIMHALT. This is not a true MC68000 instruction, but it's just a line specific to Easy68k that tells it to stop executing the code at that line. For now, if you are using an Amiga to program with instead of Easy68k, just replace SIMHALT with rts. Putting this line in is super important because without it, the CPU gets to the end of your code but doesn't stop! It will try and execute the next line... and then crash because it treats the data storage area as code which in fact it is not valid code at all. Always always always make sure your data is separate from your code.

Now for the exciting bit. The last line has a label I have called SecretStorage. The dc.b means Declare Constant of size byte and the 10 tells the assembler what value should be put into this location when it is assembled. To define a storage for a word or long word use a w or l instead of b. This is not a 68000 instruction at all, this is an instruction for the assembler and when you assemble your code, the assembler reads this part and puts a 10 there. When your program is first loaded into memory, there will be a 10 in memory at that location even before the CPU executes the very first instruction. But wait, what address is it? We only put this at the end of our code and so it could be anywhere. The power of labels is labels can be used to mark the location of any line of your code and fetch the actual address from it.

Let's fetch the number 10 from our SecretStorage:

MOVE.b SecretStorage,d5

Register d5 will now contain 10! OK, now let's try another thing and let's find out the address of SecretStorage and put it in a2:

MOVE.l # SecretStorage,a2

The # is important here!! Without the #, we would just be placing the value 10 in a2 just like the previous example. # means IMMEDIATE value. The immediate value of any label is the address of the label. Note: Due to Steemit hashtag formatting, I am forced to place a space between the # and SecretStorage. Please do not include a space in your code!

If we want to move a value into SecretStorage we write:

MOVE.b 15,SecretStorage

The destination field in MOVE will not accept # but just treat is as if the # was there already. Adding a # will cause an assembler error.

Now's a great time to introduce a simple new instruction:

LEA SecretStorage,a2

This in fact does the exact same thing as the previous line. LEA stands for Load Effective Address. It is generally best to use this form for loading label addresses into address registers rather than the MOVE instruction, for reasons too advanced at this point but I can get into it later. LEA also looks neater and easily stands out in your code. For the performance nuts out there LEA takes more cycles to execute than the MOVE.l # xxx,ax instruction but you will only notice the speed difference in tight loops in code which we are not even going to attempt at this stage.

One final note*
The 68000 can only access words and long words from EVEN addresses. If you try to access a word or long word from an ODD address, your program will crash (or a smart assembler will give you an error). A neat trick to guarantee that your word or long word storage area is at an EVEN address is to place the word EVEN above it as in:

EVEN
dc.w 4

If the address was even to begin with then the EVEN would do nothing. It's always good to put it in just in case. EVEN can also be written in lower case if preferred.

Homework
We wish to unlock a bank vault but we do not know the combination! Luckily your uncle uncovered the source code for the vault. Can you determine the combination?

BankVault

If you need to make things easier for checking your answer in Easy68k, you can type move.l Combination,d1 just before SIMHALT so your final program will put the final combination in d1. Try to work out the answer on paper beforehand though! If your guess was incorrect, use the "step into" button to run through the code line by line. You can also click View -> Memory to view a memory map of your program. Click the "Live" checkbox to have your memory map update live as each line is executed. For further help you can comment down below and I will happily assist you :)

See you in the next lesson!

Sort:  

Congratulations @sacredbanana! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of posts published

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!