 |
|
|
Atari demoscene BBS
| Here it is then... |
|
Posted by: Ragstaff
|
Oct,06.2003-16:22
|
*************alpha version!******
Pete,
It isn't quite as straight-forward as what you are asking for - there are no direct assembler equivilants to the specific "For" and "do while" keywords in C. You can make the same logical programming constructs however.
I'm not sure what level you are at, whether you understand the basics of assembly or nothing at all. You may know more than I do... I'll just have to make a few assumptions and explain the best I can!
I will explain basically "what you do" in assembly language, and just what you would need to know in order to construct a "For" and "Do While" loop (logical IF will also become clear)
So obviously I won't explain (for now at least!)
*Hex, binary, octal, decimal conversion etc
*Specific Atari ST hardware
*Addressing modes
*All 68000 instructions (hardly any at all actually!)
*Rather important things like the stack pointers, Program counter, and data register.
~*~*~*~*~*~*~Figure 1~*~*~*~*~*~*~
/* Example of unprotected loop in C */
i=0
do
{
i++;
printf("%d\n",i)
}while(i<100)
/* Example of protected loops in C */
i=0
while(i<100)
{
i++;
printf("%d\n",i)
}
for(i=0;i<100;i++)
{
printf("%d\n",i)
}
~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~
You can construct both unprotected and protected loops in assembly language using _branch_ and _comparison_ instructions.
So before I explain any kind of loop construct, I will have to explain branches and comparisons!
But before I do that, I'll have to explain _registers_, and most importantly, the _Condition-code register_ (CCR)!
Ok, I hope this doesn't take too long :-)
~*~*~REGISTERS~*~*~
-------------------
BASICS
You probably know all this, BUT....
A register is a piece of memory. It is internal to the CPU however, so don't confuse it with RAM!
The 68000 has 19 registers. 18 of those are 32 bits in size. These registers are:
*The Status register (SR), the only 16 bit register. More on this in a moment.
*Eight data registers, called D0,D1,....,D7.
*Seven Address registers called A0,A1,....,A6.
*Two Stack pointers, called A7 (for user mode), and 'A7 for supervisor mode. A7 and 'A7.
*Program counter (PC)
Remember, except for the SR, they can all hold 32 bits of information.
And yes, I know I said I would explain the Condition-code register, but it didn't even appear in that list! ;-)
STATUS REGISTER (SR)
As I mentioned before, the SR is 16 bits. The upper 8 bits I'll mostly forget about in this quick tutorial. They are collectively known as the System Byte. The System Byte contains all sorts of horrible sounding things like "IRQ Mask", "Trace Mode", etc.
The lower 8 bits are known as the Condition-Code Register, or CCR. Without the CCR, we're not going to get very far with our loop constructs, so let's have a closer look!
~*~*~THE CCR~*~*~
-----------------
As I said, the CCR is the lowest 8 bits of the SR. It gets simpler! The upper three bits of the CCR are unused, so we are left with five bits to worry about. They are not too hard to understand.
~*~*~*~*~Figure 2 - CCR ~*~*~*~*~*~
*(C)arry Flag (bit 0)
*O(v)er flow flag (bit 1)
*(Z)ero Flag (bit 2)
*(N)egative Flag (bit 3)
*E(x)tension Flag (bit 4)
~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~
Before I explain the significance of each of those five bits, I'll try to explain what CCR flags do in general.
This is a key-concept number 1:
_When the CPU executes certain instructions, the bits in the CCR reflect the result_
For instance:
* If we were to subtract 10 from 10, the result would be Zero, and the Zero Flag (bit 2) would consequently be set to 1.
* If we were to subtract 9 from 10, the result would be 1, which is non-zero, and consequently the Z flag would NOT be set to 1, but cleared to 0.
* If we were to subtract 11 from 10, the result would be -1. This is non-zero, so the Z flag would be cleared to 0, as in the previous example. However, the N flag would be set to 1, because the result is negative.
Different instructions can have affects on all the flags. For example, moving 0 from one register to another would set the Z flag, while performing a jump (JMP) to somewhere else in memory would not have any affect on the CCR bits at all. If you want to know how each 68000 instruction affects the CCR bits, the information is freely available in various 68000 references all over the 'net. I might cover them in the future.
One type of instruction I will cover though, are comparisons... because I said I would at the start!
~*~*~COMPARISONS ~*~*~
----------------------
Comparisons are instructions that affect the CCR flags, but don't actually alter the values that are being checked. In the examples listed above, the CCR flags were altered as a result of a real subtraction, which had an affect on a real value (eg, take 9 from a value stored in a register, and the register now holds (former value) - 9).
With a compare instruction, the CCR flags are altered as if a real subtraction took place, while the register or memory address are untouched.
There are a number of compare instructions - CMP, CMPI, CMPA, which compare registers or memory addresses with other operands, which can be immediate values, or values in OTHER memory addresses or registers.
All I will cover in this tutoral is TST. It subtracts 0 from whatever operand it is passed (eg TST D1). As you well may deduce, 0 - 0 = 0! So, if the register or memory address TST is used on is 0, the Z flag will be set. It's a quick, easy way to check for 0.
~*~*~ BRANCHES ~*~*~
----------------------
I will skip ahead (I should be explaining each of the CCR bits!) to introduce key-concept number 2 while the first concept is still fresh in our minds. This concept is:
_Conditional branching_
The branching is "conditional" because whether the branch occurs or not depends on the state of the CONDITIONAL code register. (There are 16 branch instructions that will branch on 16 different configurations of the CCR, but we won't cover all of them)
Some simple examples:
* Immediately after performing a subtraction (as seen in the above examples, in the CCR section), we could use the BMI (Branch if Minus) instruction to take execution of the program to a certain place in memory when the result was negative (N Flag is 1, eg 10 - 11).
* If we wanted to take execution to another part of memory when the result was Zero (Z Flag set, eg 10 - 10), we could use the BEQ instruction (Branch if Equal).
* We could branch to another location still when the result was NOT Zero (Z Flag cleared, eg 10-5), with BNE (Branch If Not Equal)
So before I explain each of the bits of the CCR, I hope it is clear how they are significant? Any calculation, and many other instructions (eg MOVEs) affect the CCR, and based on the affect, we can branch to different parts of memory and execute different code. These two "key concepts" are the building blocks of decision-making and looping in our programs.
INTERLUDE...
I had intended to explain the significance of every CCR flag, and detail all of the branch instructions related to them, but i won't get around to that tonight. I've just explained enough (I hope!) to introduce what I consider to be the "key concepts" for creating some possible assembler equivilants for Protected loop and an Unprotected loop. I hope you also recognise the logical IF operation...
I may turn this into a proper tutorial some day, and add yet another assembly page to the internet. That is what the internet needs, just like the World needs another boy-band. But I digress!
~*~*~ LOOPS ~*~*~
----------------------
OK, I had to explain registers before explaining the CCR, and I had to explain the CCR before explaining comparisons and branches. We've done all that, so let's (finally) get on with the loop constructs.
Here we go then, Pete.
This code has n[...]
|
[All messages in this thread] [Start new thread]
|
Topic
|
Posted by
|
Date
|
|
What I'd love to see is...
|
Pete
|
Oct,04.2003-23:44
|
|
Re: What I'd love to see is...
|
Grz
|
Oct,05.2003-12:27
|
|
Re: What I'd love to see is...
|
Simon Sunnyboy
|
Oct,05.2003-15:16
|
|
Re: What I'd love to see is...
|
Pete
|
Oct,05.2003-15:48
|
|
Re: Ok, a basic tutorial...
|
Ragstaff
|
Oct,06.2003-10:20
|
|
Re: Ok, a basic tutorial...
|
Pete
|
Oct,06.2003-12:15
|
|
Here it is then...
|
Ragstaff
|
Oct,06.2003-16:22
|
|
Re: and the rest of it....
|
Ragstaff
|
Oct,06.2003-16:28
|
|
Re: and the rest of it....
|
Pete
|
Oct,06.2003-16:36
|
|
np =)
|
Ragstaff
|
Oct,06.2003-16:49
|
|
np =)
|
Ragstaff
|
Oct,07.2003-00:16
|
|
lets try the code again
|
Ragstaff
|
Oct,06.2003-16:43
|
|
Re: Ok, a basic tutorial...
|
creature XL
|
Oct,06.2003-15:32
|
|
Re: Ok, a basic tutorial...
|
Ragstaff
|
Oct,06.2003-16:25
|
What's the anti-troll code? That's your personal code to be able to add comments and messages on the dhs.nu site.
Don't have a code or forgot it? Fix it here.
|
|
 |