COURS 2.TXT

From Atari Wiki
Revision as of 22:56, 16 December 2023 by Olivier.jan (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
   ******************************************************************
   *                                                                *
   *            68000 ASSEMBLY COURSE ON ATARI ST                   *
   *                                                                *
   *                 by The Ferocious Rabbit (from 44E)             *
   *                                                                *
   *                          Lesson Number 2                       *
   *                                                                *
   ******************************************************************

   THE 'MAGIC' NUMBERS

   Let's first understand in a simple way how a computer works by
   placing ourselves in the following situation: we need to send
   messages to a person with whom we are separated (for example,
   messages at night between distant people).

   We have a flashlight, which we can turn on or off, that's it. We
   can give 2 messages: 1) the flashlight is off (e.g., everything is
   fine) 2) the flashlight is on (e.g., here come the police!)

   Let's delve into the 2 states of the flashlight:
                         On                     Off

   which boils down to: Power On               No power
   or: Power?             YES                    NO

   Power value?            1                      0

   Tests will be noted as 0 or 1 depending on the flashlight being on
   or off.

   Since we are rich, we buy a 2nd flashlight.
   So, we have 4 message possibilities:

               FLASHLIGHT 1        FLASHLIGHT 2

              Off                 Off
              On                  Off
              Off                 On
              On                  On

   Counting with 3,4,5,6 ... flashlights, we realize that it is
   possible to find a simple relationship between the number of
   flashlights and the number of possibilities.

   Number of possibilities = 2 to the power of the number of flashlights.

   So, we get the following table:
   Remarks are just there to give you a hint!

   Flashlights  Possibilities                 Remarks
   1             2
   2             4
   3             8            There are 8-bit computers ...
   4             16           and 16 bits...
   5             32           The ST is a 16/32 bits
   6             64           Amstrad CPC... 64!!
   7             128          or Commodore 128?
   8             256          In computing, the character coding
                              (letters, numbers, etc., using ASCII)
                              allows for 256 characters!
   9             512          A 520 has 512 KB of memory, and
                              Amstrad sells a PC1 512.
   10            1024         The memory size of my 1040!
   11            2048         That of my brother's Mega 2
   12            4096         That of a Mega 4. Also the number of
                              displayable colors with an Amiga.
   etc...
   16            65536        In GFA, an array
                              cannot have more than 65536 elements.

   If my 4 flashlights are off (0000), I am at possibility 0. If they
   are all on (1111), I am at 15 (because from 0 to 15 makes 16), so
   0000 --> 0 and 1111 --> 15.

   So, I have a book of 16 pages giving the possibilities of the 16
   possible lighting configurations, and my correspondent has the
   same. How do I send him the message from page 13?

   Since the smallest digit is on the right (we note digits in
   order hundreds, tens, units), let's arrange the flashlights.

   Flashlight number:  4       3       2        1
   a) I have only one flashlight (the 1st one); it's on, so I get the
   value 1. (I can only get 0 or 1)

   b) I have 2 flashlights (1 and 2), both on, I get the 4th
   possibility. So, I have the value 3 (since I count the values 0,1,
   2, and 3, which makes 4). Since flashlight 1 is at most the value
   1, I deduce that flashlight 2 is alone worth a maximum of 2.

   In fact,      flashlight 1 on --> value 1
                flashlight 2 on --> value 2
   So, both on together --> value 3 = 4 possibilities.

   Flashlight number:     4             3            2            1
   'increase'            8             4            2            1

   To send the message 13, I need to turn on flashlight 4
   (value of 8), flashlight 3 (value of 4), and flashlight 1 (value
   of 1).
   Flashlight            4      3         2       1
   Flashlight state      1      1         0       1
   Value                8  +   4    +    0   +   1 = 13

   So, we are counting in binary.

   In decimal: dec means 10, because a digit can take 10 values
   (from 0 to 9).

   In binary: bi = two because each digit can only take 2 values
   (0 or 1).

   Computing is an Anglo-Saxon field. A 'binary digit' in English is
   called a 'BIT'! So, a bit can be 0 or 1. It is the smallest
   computing unit because the correspondent to whom we send messages
   is, in fact, a computer. Instead of turning on flashlights, we put
   current on a wire or not. An 8-bit computer has 8 wires on which
   current is placed or not!

   To send messages, we will prepare flashlights with small switches,
   and when our flashlights are ready, we will turn on the main
   switch to send the current and thus turn on the intended
   flashlights at once.

   Through our 'flashlights', we will send messages to the heart of
   the machine (in the case of the ST, it is a Motorola 68000
   microprocessor) which has been manufactured to respond in a
   certain way to different messages.

   So, we prepare our flashlights, and then we turn them on. We have
   16 flashlights because the Motorola 68000 is a 16-bit
   microprocessor.

   Here is a 'program' (i.e., a sequence of orders) as it is at the
   level of turning on or off the current on the 16 wires. All the
   way to the left is the value of wire 16, and to the right is the
   value of wire 1. 0 = no current on the wire, 1 = current. The
   microprocessor is surrounded by multiple drawers (memory cells),
   and among the orders it can execute are 'go get what's in that
   drawer' or 'go put this in that drawer'. Each drawer is identified
   by an address (like each house), i.e., by a number.

   So, we will tell the microprocessor: go get what's at address
   24576, add to it what's at address 24578, and put the result at
   address 24580. We could replace 'at address' with 'at the address'.

   Let's turn on the 16 flashlights accordingly; this gives:

   0011000000111000
   0110000000000000
   1101000001111000
   0110000000000010
   0011000111000000
   0110000000000100

   A single glance is enough; it's a total mess! How do we make sense
   of a program like this? If we forget to turn on a single
   flashlight, it won't work anymore, and finding the error in such a
   listing, good luck!
   It's a mess!!!

   So, we have the option to represent this not in binary but in
   decimal. Unfortunately, the conversion is not convenient, and
   anyway, we end up with large numbers (visually, because their size
   as a number doesn't change, of course!) For example, the 3rd line
   gives 53368. So, let's convert differently by separating our
   binary numbers into groups of 4 bits.

   VOCABULARY NOTE:

   We will speak only English. All abbreviations in computer science
   are abbreviations of English words or expressions. Reading them in
   French requires memorizing their meaning. By reading them as they
   SHOULD be read (in English), these expressions give their
   definition. One example is T$, which is systematically read as T
   dollar! However, $ is not, in this case, the abbreviation for
   dollar but for string. So, T$ must be read AND PRONOUNCED as T
   string. String meaning 'chain' in English, T is therefore a string
   of characters. Obviously, reading T dollar means absolutely
   nothing! The only interest is that it amuses Douglas, the joyful
   Brit who programs with me!

   One binary unit is called a BIT (binary digit).
   4 units form a NIBBLE.

   8 units form a byte (which we will call by its English name,
   BYTE).

   16 units form a word (WORD).

   32 units form a long word (LONG WORD).

   Let's go back to our conversion by grouping our 16 flashlights
   (so our WORD) into groups of 4 (so into NIBBLES).

    0011        0000         0011         1000

   These 4 nibbles form our first word.

   Let's count the possible values for a single nibble.

   Nibble state 0000         value 0
           0001         value 1
           0010         value 2
           0011         value 3
           0100         value 4
           0101         value 5
           etc..
           1010         value 10
           STOP  it's over! 10 is 1 and 0, but we've already used
   them!
   Well, yes, but apart from 0,1,2,3,4,5,6,7,8,9, we don't have much
   at our disposal... Well, there's the alphabet!
   So, let's write 10 with A, 11 with B, 12 with C, 13/D, 14/E, and
   15 with F. So, there are 16 digits in our new system (from 0 to
   F). 'Dec' means 10, and 'Hex' means 6 (a hexagon), so Hex + Dec =
   16. Decimal = having 10 digits (0 to 9), hexadecimal = having 16!

   Our program becomes hexadecimal:
   $3038
   $6000
   $D078
   $6002
   $31C0
   $6004

   Clearer, but it's not there yet.

   NOTE: To differentiate between a binary number and a decimal or
   hexadecimal number, by convention, a binary number will be
   preceded by %, a hexadecimal number by $, and there will be
   nothing in front of a decimal number. So, $11 does not equal 11 in
   decimal but 17.

   Let's think a little. In fact, we wrote:

   'Go get what's there'
   'at address $6000'
   'add to it what's'
   'at address $6002'
   'put the result'
   'at address $6004'

   The microprocessor can, of course, pick from the thousands of
   memory cells in the machine, but it also has some on itself
   (small pockets, so to speak, in which it temporarily stores things
   it will need quickly). It has 17 pockets: 8 in which it can put
   data, and 9 in which it can put addresses. Data = DATA and
   address = ADDRESS, these pockets will be identified by D0, D1,
   D2,... D7, and by A0, A1,... A7, and A7' (we will see later why
   it's not A8, and the differences between these types of pockets).

   NOTE: The phenomenon of on/off and the same for ALL current
   computers. The number of 'pockets' is specific to the Motorola
   68000.
   So, the same number of 'pockets' on an Amiga or a Macintosh since
   they also have a Motorola 68000. On a PC or a CPC, the
   characteristics (number of flashlights that can be turned on
   simultaneously, number of 'pockets'...) are different, but the
   principle is the same. It's either on OR off.

   Let's modify our 'text', which now becomes.

   'move into your pocket D0'
   'what you will find at address $6000'
   'add to what you have in your pocket D0'
   'what you will find at address $6002'
   'put the result of the operation'
   'at address $6004'

   The machine is very limited, as by design, the result
   of the operation in the 3rd line will go into D0 itself, overwriting
   what is already there. To keep the value that was there,
   it would be necessary to copy it, for example, into pocket D1!

   Moving is called in English MOVE
   Adding is called in English ADD

   Our program becomes

   MOVE    what is at $6000   into  D0
   ADD     what is at $6002     to   D0
   MOVE    what is now in  D0 to $6004

   That is:
   MOVE    $6000,D0
   ADD     $6002,D0
   MOVE    D0,$6004

   We have just written a program in machine language.

   The fundamental difference with a program in any other
   language is that here, each line corresponds to ONLY ONE
   operation of the microprocessor, whereas PRINT "HELLO" will
   make it do a lot. It is obvious that our BASIC,
   being a 'mechanical' translator, has a translation that is likely
   to be approximate, and, although effective, it
   uses many more instructions (for the microprocessor)
   than are actually needed.

   It is also worth having a fond thought for the first
   programmers of the 68000 who initially created a program with 1s and
   0s, a program that only translated hexadecimal numbers
   into binaries before transmitting them to the machine. They
   then created, in hexadecimal, programs translating instructions like MOVE, ADD, etc., into binary...

   It was then enough to group several instructions of this
   type under another name (not directly understood by the machine) and
   to create the corresponding translators, thus creating
   'advanced' languages (PASCAL, C, BASIC...)

   So, we are going to be interested in programming or rather in the
   transmission of orders to the 68000 Motorola. How many orders can it
   execute? Only 56!!! (with variations, but still not many). Research
   (at a level far too high for us!) has indeed shown that it is faster to have
   few instructions doing little each and therefore executing
   quickly one after the other, rather than having
   many instructions (the microprocessor likely losing time
   searching for the one it was asked to do) or complex instructions.

   Work to be done: reread all this at least twice and then take a break
   to clear your mind before reading the rest.

   ADVICE: do not start the next part right away.
   Digest EVERYTHING that is written, as understanding
   every detail will serve you.

   A lamp, it's not much, but one burnt out and you
   will understand the mess it brings.
   Here, it's the same. The smallest thing not understood and you won't
   understand anything in the following. But if everything is understood, the
   following will be just as easy, and above all, logical.

Back to ASM_Tutorial