home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / program / books / mc68ktu2 / mc680008.doc < prev    next >
Text File  |  1985-11-18  |  12KB  |  285 lines

  1. 02200000000801
  2. 1
  3. 2
  4. 9[...............................................................]0
  5. üMC 68000 ASSEMBLY LANGUAGE COURSE PART VIII Çby Mark van den Boer
  6.  
  7. ëProgram Control InstructionsÇ
  8.  
  9. This  class of instructions enables a programmer to create  loops 
  10. and  IF-THEN-ELSE  like  decisions.  That's  why  it's  ütheÇ  most 
  11. important group of instructions and every programmer should  have 
  12. a  thorough knowledge of this group.  This class of  instructions 
  13. are specifically meant to affect the program counter.
  14.  
  15. Instructions:  Bcc  (cc stands for Condition Code)
  16. Syntax:        Bcc  <address>
  17. Data sizes:    Byte  or word.  This implicates that  the  branch-
  18.                instructions  can branch in an area of  32K.  When 
  19.                using a branch with a byte offset you can put a .S 
  20.                suffix behind the instruction e.g.  BEQ.S  .  When 
  21.                using a branch with a word offset you can put a .W 
  22.                suffix behind the instruction e.g.  BEQ.W  .  Most 
  23.                assemblers  will  determine if the short  or  word 
  24.                form is needed. Also most assemblers will optimize 
  25.                word-branches to byte-branches whenever possible.
  26.  
  27. Condition codes affected:
  28.                None
  29. Function: Test  a  combination of the NZVC-flags in  the  status-
  30.           register and conditionally perform a branch to  another 
  31.           address. If the testing of the condition codes is true, 
  32.           then  the  branch  will be  taken,  in  the  other  the 
  33.           instruction  immediately following the Bcc  instruction 
  34.           will  be executed. A total of 15 possible variations of 
  35.           this instruction are listed below.
  36.           BCC: where  CC stands for Carry Clear.  The  branch  is 
  37.                taken if the C-bit is 0. This instruction is often 
  38.                used   in  combination  with  shift   and   rotate 
  39.                instructions.
  40.           BCS: where CS stands for Carry Set. The branch is taken 
  41.                if  the  C-bit  is  1.  This  instruction  is  the 
  42.                counterpart of the BCC-instruction.
  43.           BEQ: where EQ stand for EQual.  The branch is taken  if 
  44.                the  Z-bit is 1.  This instruction is  often  used 
  45.                after a TST-instruction or CMP-instruction.
  46.           BNE: where NE stands for Not Equal. The branch is taken 
  47.                if  the  Z-bit  is  0.  This  instruction  is  the 
  48.                counterpart of the BNE-instruction.
  49.           BPL: where PL stands for PLus.  The branch is taken  if 
  50.                the  N-bit is 0.  This instruction is  often  used 
  51.                after a TST-instruction or CMP-instruction.
  52.           BMI: where MI stands for MInus.  The branch is taken if 
  53.                the   N-bit  is  1.   This  instruction   is   the 
  54.                counterpart of the BPL-instruction.
  55.           BVC: where VC stands for oVerflow Clear.  The branch is 
  56.                taken if the V-bit is 0. This instruction is often 
  57.                used after an Integer Arithmetic instruction  like 
  58.                ADD, SUB, MUL etc.
  59.           BVS: where  VS stands for oVerflow Set.  The branch  is 
  60.                taken if the V-bit is 1.  This instruction is  the 
  61.                counterpart of the BVC-instruction.
  62.           BRA: where   RA   stands  for   bRanch   Always.   This 
  63.                instruction is often used at the end of a loop  to 
  64.                go back to the beginning of the loop.
  65.          Branches often used after an arithmetic operation on
  66.          two's complement numbers.
  67.           BGE: where GE stands for Greater or Equal.  This branch 
  68.                is  taken  if the N and V-bits  contain  the  same 
  69.                value.
  70.  
  71.           BGT: where GT stands for Greater Than.  This branch  is 
  72.                taken in the following cases:
  73.                - N is 1, V is 1, Z is 0
  74.                - N is V is Z is 0
  75.           BLE: where LE stands for Lower or Equal. This branch is 
  76.                taken in the following cases:
  77.                - Z is 1
  78.                - N and V-bits contain different values
  79.           BLT: where  LT  stands for Less Than.  This  branch  is 
  80.                taken  if  the  N  and  V-bits  contain  different 
  81.                values.
  82.          Brances often used after an arithmetic operation on
  83.          unsigned numbers.
  84.           BHI: where HI stands for HIgher.  This branch is  taken 
  85.                if the N and V-bits contain the same value.
  86.           BLS: where LS stands for Lower or Same.  This branch is 
  87.                taken  if  the  C  and  Z-bits  contain  different 
  88.                values.
  89. Example:
  90.           This  shows  a piece of a C-program and  an  equivalent 
  91.           piece  of  a PASCAL-program which are  translated  into 
  92.           assembler. (variabele is signed)
  93.           C:
  94.                if (variable == 1 || variable > 4) variable = 5;
  95.                else var *= 3;
  96.           PASCAL:
  97.                if (variable == 1) or (variable > 4)
  98.                then variable := 5
  99.                else variable := variable * 3
  100.  
  101.           * Most assemblers will optimize the branch-instructions
  102.           * to the short forms
  103.                  CMP.W   #1,variable
  104.                  BEQ     L10000
  105.                  CMP.W   #4,variable
  106.                  BLE     L2
  107.           L10000:
  108.                  MOVE.W  #5,variable
  109.                  BRA     L3
  110.           L2:
  111.                  MOVE.W  variable,R0
  112.                  MULS    #3,R0
  113.                  MOVE.W  R0,variable
  114.           L3:
  115. Instructions:  DBcc  (cc stands for Condition Code)
  116. Syntax:        DBcc  Dn,<address>
  117. Data sizes:    byte  or word.  This implicates that  the  branch-
  118.                instructions can branch in an area of 32K.  Dn  is 
  119.                considered to contain a word.
  120. Condition codes affected:
  121.                None
  122. Function:
  123.           The  group of Decrement and Branch (DBcc)  instructions 
  124.           provide  an efficient way of creating loops.  They  are 
  125.           nearly  always placed at the end of a loop.  First  the 
  126.           condition   is  tested,   then  the   dataregister   is 
  127.           decremented.  The  branch  is taken  in  the  following 
  128.           cases:
  129.           - Dn is -1;
  130.           - The condition cc in DBcc is satisfied.
  131.           There  are 16 possible variations of this  instruction. 
  132.           They  all are nearly the same as the  Bcc-instructions, 
  133.           with two exceptions. These are:
  134.           DBF or DBRA:
  135.                This  loop can only be terminated by  count  since 
  136.                the other condition can never be satisfied.
  137.           DBT: Only performs a decrement on the dataregister  and 
  138.                never branches.  To me this seems a pretty useless 
  139.                instruction,  which is only there to make the DBcc 
  140.                series logically complete.
  141. Example:
  142.           This  piece of code is an efficient  implementation  of 
  143.           the strcpy-function of the C-language.  A0 contains the 
  144.           address  of  the  source string  and  A1  contains  the 
  145.           address  of the destination string.  In C the end of  a 
  146.           string is marked by a byte containing 0.
  147.                  MOVE.W  #$ffff,D0
  148.           LOOP:  MOVE.B  (A0)+,(A1)+
  149.                  DBEQ    D0,LOOP
  150.           This  piece of code can easily be transformed into  the 
  151.           strncpy-function  by  loading D0 with  the  appropriate 
  152.           value.
  153.  
  154.  
  155. Instructions:  Scc  (cc stands for Condition Code)
  156. Syntax:        Scc  <address>
  157. Data sizes:    byte.
  158.  
  159. Condition codes affected:
  160.                None
  161. Function: Sets a byte to $ff if the condition codes satisfie.  If 
  162.           the  condition is not satisfied the byte is set  to  0. 
  163.           This  group of 16 instructions is rarely  used.  Nearly 
  164.           all forms are the same as the DBcc group except for the 
  165.           following two instructions:
  166.           SF:  the same as a CLR.B instruction
  167.           ST:  the same as a MOVE.B #$ff, <address>
  168. Example:
  169.           Be inventive, invent one yourself!
  170.  
  171.  
  172. Instruction:   BSR, JSR
  173. Syntax:        BSR  <address>
  174.                JSR  <address>
  175. Data sizes:    none
  176. Condition codes affected:
  177.                none
  178.  
  179.  
  180.  
  181. Addressing modes allowed (only for JSR):
  182. Destination:
  183.           (An)
  184.           w(An)
  185.           b(An,Rn)
  186.           w
  187.           l
  188.           w(PC)
  189.           b(PC,Rn)
  190. Function: The  BSR  (Branch  to  SubRoutine)  and  JSR  (Jump  to 
  191.           SubRoutine)   instructions   are   used   for   calling 
  192.           subroutines.  BSR  can branch in a range  of  32K.  JSR 
  193.           should  be  used when a jump out of the  32K  range  is 
  194.           needed.   Some   assemblers  optimize  JSR   into   BSR 
  195.           instructions  whenever  possible,  since  BSR  is  more 
  196.           efficient   than   JSR.   When  executing   a   BSR/JSR 
  197.           instruction,  the  68000 first pushes the PC  (program-
  198.           counter) on the stack and then load the PC with the new 
  199.           address. See below for the RTS (ReTurn from Subroutine) 
  200.           instruction.
  201.  
  202.  
  203. Instruction:   RTS
  204. Syntax:        RTS
  205. Data sizes:    none
  206. Condition codes affected:
  207.                none
  208. Function: Counterpart  of BSR/JSR instructions.  Reloads  the  PC 
  209.           with  the value on top of the stack.  This  value  will 
  210.           nearly  always have been put on top of the stack  by  a 
  211.           BSR/JSR instruction.
  212. Example:  * the strcpy function discussed before
  213.           STRCPY:
  214.                  MOVE.W  #$FFFF,D0
  215.           LOOP:  MOVE.W  (A0)+,(A1)+
  216.                  DBEQ    D0, LOOP
  217.                  RTS
  218.           * some other code
  219.           BEGIN:
  220.                  MOVE.L  #SOURCE,A0
  221.                  MOVE.L  #DEST,A1
  222.                  JSR     STRCPY
  223.                  RTS
  224.  
  225.           * the strings are put in a data area
  226.           .DATA
  227.           * 80 bytes for every string
  228.           SOURCE .DS.B   80
  229.           DEST   .DS.B   80
  230.           * .DS.B means Define Storage Byte
  231.           * so 80 bytes are define as storage for each string
  232.  
  233.  
  234. Instruction:   JMP
  235. Syntax:        JMP  <ea>
  236. Data sizes:    none
  237. Condition codes affected:
  238.                none
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247. Addressing modes allowed:
  248. Destination:
  249.           (An)
  250.           w(An)
  251.           b(An,Rn)
  252.           w
  253.           l
  254.           w(PC)
  255.           b(PC,Rn)
  256. Function: Transfer program control to another address.  The PC is 
  257.           loaded  with the specified address.  In fact this is  a 
  258.           variant  of  the MOVE instruction.  In  this  case  the 
  259.           destination register is inherently defined,  namely the 
  260.           PC-register.  Therefore we could translate JMP <ea>  to 
  261.           MOVE.L <ea>,PC .
  262.  
  263. Instruction:   RTR
  264. Syntax:        RTR
  265. Data sizes:    none
  266. Condition codes affected:
  267.                none
  268.  
  269. Function: ReTurn    and   Restore.    Counterpart   of    BSR/JSR 
  270.           instructions.  Reloads the PC with the value on top  of 
  271.           the stack.  This value will nearly always have been put 
  272.           on top of the stack by a BSR/JSR instruction.  The only 
  273.           difference  with the RTS is that with this  instruction 
  274.           also  the CCR is reloaded.  This instruction is  rarely 
  275.           used  but  comes  in  handy when  one  doesn't  want  a 
  276.           subroutine to influence the condition codes. Before the 
  277.           JSR instruction you should use the instruction:
  278.           MOVE.B CCR,-(A7)
  279.           which pushes the CCR on the stack
  280.  
  281. Next time:  The last part of the instruction set.  These are  the 
  282. instructions  which can only be executed when supervisor-mode  is 
  283. active.
  284.  
  285. Originally published in üST NEWSÇ Volume 2 Issue 8.