LF: For Newbies... II


[Prev][Next][Index][Thread]

LF: For Newbies... II



II) First instructions: move/arithmetics

1) Move

one of the most important action you need to do when programming is handling
data. I mean that you need to know how to move data to or fro memory.
The corresponding insturction is: "Move" ( as "mov" for th 8086 processor
family ) 
ex: move     D2, D4 will move ( copy ) data from D2 to D4. More generaly, in
all instructions you 'll use, the first parameter is the source and the
second th destination.
Then, you need to precise the data form you will use : BYTE, WORD or
LONGWORD: you can move the lower byte of D2, its lower WORD or its whole
content. Here is how to do this: 

move.B   D2,D4     : byte
move.W   D2,D4     :word
move.L     D2,D4     :longword

this syntax is used for all instructions too. 
Important: when programming, never forget to precise the lenght of the data
you use: this is capital ! If you clear the lower word of a register, and
something is in the upper word, then you may have pbs later...
something else: when you : move.w   D2,D4, the uper word of D4 is unaffected.
Move may be used to put certain values in some places: you wish to put "1270"
( in decimal ) in the D4 register. just type : move.w   #1270,D4
the "#" stands for immediate value: you will thus store the number 1270 or
"10011110110" in the D4 register .
if the number you got is hex, just type: move.w  #$4F6,D4
"$" stands for hexadecimal.
still, if you want to move data from a certain place in the memory ( not the
processor's one ): you know the adress of the data you need: $4F6.
type : move.w    $4F6,D4
the porcessor will take the word stored at the adress 4F6 and copy it to D4.

Here it is ! You have seen the principals operatins you can do with move. 
However, before going on, i think you should try to take a look a jimmy
mardel' s guide: 
he describes all ways of using the move command: all ways of adressing
memory. This may seem not very exciting but it is usefull to know waht you
can do before trying to program.  

So: we know how to change things in memory: but what operations can we do on
these datas ? 

2)   arithmetics

I will present here simple arithmetics: addition, substractions and negative
numbers
let' s imagine you want to calculate 100+25
you would do: move.w  #100,D0
                     move.w  #25,D1
                     add.w      D0,D1

this piece of code will do: D0+D1->D1
you' ll have 125 in D1 at the end of this code. But of course in binary.
now, for 100 - 25 , 

move.w    #100,D0
move.w     #25,D1
sub.w         D1,D0

in which case, you do: D0 - D1 ->D0
let ' s imagine we had done : 
sub.w      D0,D1
then, there would be - 75 in D0, a negative number ! 
So: we know that to recognize a neg number, we look at its highest bit ( if
set, negative ) 
let' s imagine it was that simple:
we wish to add 4 and -1. we should get +3
         4       0100
    +  -1       1001
________________
       +3       1101= - 5     

this doesn' t work. So, we use a slightly different way to represent negative
numbers

we have +4: 0100
we want - 4 : we take the complement to one ( we are inverting all bits of
the number and we add 1                                     11      :
retenues
then - 4 is : 1100. yes:  we get:   1011 
                                            +  0001
                                          __________
                                                1100   ( don' t forget
retenues ) 

in our exemple of:   4+  -1

  0100
  1111
  ____
  0011 = + 3

so: this is the way neg numbers are handled: don' t forget that a negative
number is just in your mind: the processor don' t know the difference between
11111111 ( -1 in a byte ) 
and  11111111 ( 255 in a byte ) . so: the content of the registers depends on
what you want them to be. 
There is something you need not to forget: always precise the lenght of the
data used: 
what if you had done a byte addition on +4 and -1 written in a half byte way
? ( i did it on a half byte ) 
you would have got: 10011 ( i haven' t taken into acount the retenue which
was  out of the 4 bits. ) 
yes: -1 in a byte is:  11111111     
and +4   is              00000100

try now a byte addition on these numbers: you' ll get +3 on a byte: 00000011

that' s all for today ! 

BTW, if you have questions on this, e mail , i' ll try to answer 

Mathieu, <hm lacage@aol.com>