home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / program / books / 68k_book / arp_doc / chap_03.doc < prev    next >
Text File  |  1985-11-20  |  52KB  |  1,150 lines

  1.    Atari ST Machine Specific Programming In Assembly
  2.  
  3. Chapter 3: Assembly and Addressing Modes
  4.                
  5.  
  6.      On April 1st, one year in the late 1970's, the 
  7. announcement of a revolutionary semiconductor device, the 
  8. write only memory (WOM), provided engineers the laugh of the 
  9. day.  Imagine my mirth when I discovered that Motorola was 
  10. apparently manufacturing a microprocessor that is 
  11. "compatible" with that device--a read only processor (ROP).  
  12. Imagine my chagrin when I realized that I had purchased a 
  13. computer designed around that microprocessor.
  14.      In the early stages of my ST education, the dilemma I 
  15. faced was this: the ST's operating system requires position 
  16. independent programs (My definition of a position 
  17. independent program is this: it is a program for which the 
  18. validity of execution results does not depend on the 
  19. location of the program and its data in ram.); according to 
  20. the references, this attribute is bestowed on programs which 
  21. use the MC68000 pc-relative addressing mode to access label 
  22. operands; however, this addressing mode cannot be used when 
  23. the operand is a destination.  How, then, does one store 
  24. data in program variables during program execution?
  25.  
  26. The ST Relocator
  27.   
  28.      Well, of course, I soon learned, via page 79 of the 
  29. AssemPro manual, that the ST has a built in program 
  30. relocator that nullifies the MC68000's pc-relative 
  31. addressing mode limitation, by producing position 
  32. independent code for programs that are relocatable (My 
  33. definition of a relocatable program is this: it is a program 
  34. to which the attribute position independence may or may not 
  35. initially be applicable.  Regardless, however, the relocator 
  36. will position the program in ram in a manner which 
  37. guarantees that the validity of execution results does not 
  38. depend on its location); and, fortunately, AssemPro provides 
  39. relocatable code when a source file is assembled with the 
  40. Relocatable assembly option checked.  For a time, therefore, 
  41. the dilemma seemed moot.
  42.      But the process is not as automatic as it seems.  The 
  43. AssemPro manual attempts to provide an example, on pages 79 
  44. and 80, to indicate a type of expression which the relocator 
  45. would find unpalatable.  I have tried to deduced Mr. 
  46. Schulz's intent, because it is not clearly stated.  
  47. Furthermore, the example does not seem to contain enough 
  48. information to render it informative.  I don't think that he 
  49. intended to indicate that files containing these types of 
  50. expressions will not be assembled, because I have assembled 
  51. my own example.  The source file is program 6a.
  52.      I have included various statements which use the 
  53. declared labels in this program so that you can see what 
  54. works and what doesn't.  Statement #5 is the only one that 
  55. causes a run time error.  And the reason is not a mystery, 
  56. as you can see in the disassembly listing shown in figure 
  57. 3.2.  The statement assembles into an instruction that 
  58. attempts to store the contents of address -4 in register D0.
  59.  
  60. Program 6a. My source file for the discussion on pages 79 
  61. and 80 of the AssemPro manual.
  62.  
  63.  ; Program Name: PRG_2AR.S
  64.  ;      Version: 1.002
  65.  
  66.  ; Assembly Instructions:
  67.  
  68.  ;    Assemble in AssemPro Relocatable mode and save the assembled program
  69.  ; with a PRG extension.
  70.  
  71.  ; Program Function:
  72.  
  73.  ;    Serves as a source file for the discussion on pages 79 and 80 of the
  74.  ; AssemPro manual.
  75.  
  76.  move.l    up_1, d0
  77.  move.l    up_2, d0
  78.  move.l    #-4, d0        ; The statement below will resemble this one
  79.                           ; after assembly.  Both are relocatable.
  80.  move.l    #up_1-up_2,d0  ; This is Mr. Schulz's example.
  81.  move.l    up_1-up_2, d0  ; I think he meant to use this type of example,
  82.  data                     ; because this one does cause a problem.
  83. pointer:   dc.l  up_1
  84. up_1:      dc.l  5
  85. up_2:      dc.l  3
  86.  end
  87.  
  88.  
  89.      This program was assembled in AssemPro's Relocatable 
  90. mode, with the Listing option checked.  The listing was 
  91. directed to the file shown in figure 3.1.  As you shall see 
  92. in figure 3.2, a disassembled listing of program 6a, the 
  93. expression #up_1-up_2 is evaluated to -4 during assembly.  
  94. This fact is also evident in the listing of figure 3.1, 
  95. where you are able to see that the object code on line 6 is 
  96. actually that for the instruction: move.l #-4, d0, identical 
  97. to the statement on line 5.  This is the instruction given 
  98. as an example of one that is not relocatable in the AssemPro 
  99. manual.  But I found that this instruction does not cause 
  100. any problems.  In fact, I included the statement on line 5 
  101. of the program to illustrate that it works whether it is 
  102. introduced specifically into the program or it is assembled 
  103. into that format by the assembler.  It is the statement on 
  104. line 19 which causes a run time error.
  105.   
  106. Figure 3.1. Assembly listing for program 6a.
  107.  
  108.  Address Objectcode   Line    Sourcetext Assembly listing for PRG_2AR.S                                          
  109.  
  110. 000000 :                 1   ; Program Name: PRG_2AR.S
  111. 000000 :                 2   ;      Version: 1.002
  112. 000000 :                 3
  113. 000000 :                 4   ; Assembly Instructions:
  114. 000000 :                 5
  115. 000000 :                 6   ;    Assemble in AssemPro Relocatable mode and save the assembled program
  116. 000000 :                 7   ; with a PRG extension.
  117. 000000 :                 8
  118. 000000 :                 9   ; Program Function:
  119. 000000 :                10
  120. 000000 :                11   ;    Serves as a source file for the discussion on pages 79 and 80 of the
  121. 000000 :                12   ; AssemPro manual.
  122. 000000 :                13
  123. 000000 :203900000022    14   move.l    up_1, d0
  124. 000006 :203900000026    15   move.l    up_2, d0
  125. 00000C :203CFFFFFFFC    16   move.l    #-4, d0        ; The statement below will resemble this one
  126. 000012 :                17                            ; after assembly.  Both are relocatable.
  127. 000012 :203CFFFFFFFC    18   move.l    #up_1-up_2,d0  ; This is Mr. Schulz's example.
  128. 000018 :2039FFFFFFFC    19   move.l    up_1-up_2, d0  ; I think he meant to use this type of example,
  129. 00001E :                20   data                     ; because this one does cause a problem.
  130. 00001E :00000022        21  pointer:   dc.l  up_1
  131. 000022 :00000005        22  up_1:      dc.l  5
  132. 000026 :00000003        23  up_2:      dc.l  3
  133. 00002A :                24   end
  134.  
  135.  
  136.      The file shown in figure 3.2 was produced by clicking 
  137. on the Disassembling option under the Debugger menu, then by 
  138. typing $5D7B4 as the from address and $5D7DE as the to 
  139. address.  When disassembling a program with this AssemPro 
  140. function, you must always specify a to address that is 
  141. beyond the last address you want to appear in the listing; 
  142. furthermore, the address you specify must be beyond the 
  143. address of the last item of data you wish to appear in the 
  144. listing.  For example, the last address shown in the listing 
  145. of figure 3.2 is $05D7DA.  The data located at that address 
  146. is a longword (4 bytes) in length, therefore, the to address 
  147. specified was $05D7DA + 4 = $05D7DE.
  148.  
  149.  
  150. Figure 3.2. Disassembly listing for program 6a.
  151.  
  152.  Disassembled listing of PRG_2AR.PRG
  153.  
  154. 05D7B4 20390005D7D6  MOVE.L $5D7D6,D0
  155. 05D7BA 20390005D7DA  MOVE.L $5D7DA,D0
  156. 05D7C0 203CFFFFFFFC  MOVE.L #-4,D0
  157. 05D7C6 203CFFFFFFFC  MOVE.L #-4,D0
  158. 05D7CC 2039FFFFFFFC  MOVE.L -4,D0
  159. 05D7D2 0005D7D6      ORI.B #-$2A,D5
  160. 05D7D6 00000005      ORI.B #5,D0
  161. 05D7DA 00000003      ORI.B #3,D0
  162.  
  163.  
  164.      The disassembly listing clearly shows that an error 
  165. will occur when an attempt is made to execute the fifth 
  166. statement because that statement has been assembled into one 
  167. which attempts to load the contents of a negative address 
  168. into D0.  See figure 3.3.  The -4 produced in this statement 
  169. (as is the -4 in the fourth statement) is the difference 
  170. between the addresses of labels up_1 and up_2 during 
  171. assembly.
  172.  
  173.   
  174. Figure 3.3. Attempting to execute the fifth statement of 
  175. figure 3.2 in the AssemPro debugger will cause an alert box 
  176. similar to this one to appear.
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.    
  188.      If the intent of a programmer is to store the 
  189. difference between the contents of the run time addresses of 
  190. labels up_1 and up_2 in D0, then, clearly, that does not 
  191. happen.  I believe that this is the point which Mr. Schulz 
  192. intended to make with his example.  Even if this was not his 
  193. intent, the example still illustrates that assembly using 
  194. AssemPro's Relocatable mode does not guarantee a position 
  195. independent program, unless the elements of each statement 
  196. in the program are congruous with relocatability.  As we 
  197. have seen, it is possible to load and execute a position 
  198. dependent program which has been assembled in the 
  199. Relocatable mode.
  200.  
  201. A Few More Comments About the AssemPro Manual
  202.   
  203.      The paragraph concerning the advantage of a relocatable 
  204. program on page 80 has not been correctly written.  Instead 
  205. of "The advantage of a relocatable program...", the 
  206. paragraph should merely indicate that labels can be used as 
  207. destination operands in a program that is assembled in 
  208. AssemPro's Relocatable mode.  Nothing else that is implied 
  209. by that paragraph can be confirmed.  That the use of labels 
  210. in destination operands is advantageous is a subjective 
  211. opinion at best.  In fact, I shall show that the requisite 
  212. memory of a program assembled in AssemPro's Relocatable mode 
  213. can never be as low as if it were assembled in the PC-
  214. relative mode; and, furthermore, the loading times required 
  215. for programs assembled in the Relocatable mode are much 
  216. greater than they are for those assembled in the PC-relative 
  217. mode.
  218.      The following characteristics are the major differences 
  219. between the three AssemPro modes.  Some of these 
  220. characteristics are not evident during the assembly process.
  221.  
  222.      1. When a program is assembled in PC-relative mode the 
  223.         assembler automatically attaches a (PC) suffix to 
  224.         labels used as source operands.  If the program were 
  225.         assembled in the Absolute mode, the programmer would 
  226.         have to manually attach the (pc) suffix to each 
  227.         label.  During assembly, the assembler will issue an 
  228.         error report, declaring that the effective address 
  229.         is not acceptable, for each label used as a 
  230.         destination operand.
  231.  
  232.      2. In a program assembled in the Relocatable mode, 
  233.         labels are accepted as legal destination operands.  
  234.         If used, the (pc) suffix must be inserted where 
  235.         desired by the programmer.  As in item 1, above, the 
  236.         assembler will flag instructions which contain a 
  237.         label(pc) in a destination operand.
  238.  
  239.      3. Programs assembled in the Absolute or PC-relative 
  240.         modes inherently require less memory and loading 
  241.         time than do programs assembled in the Relocatable 
  242.         mode.
  243.  
  244. Concept Definitions
  245.   
  246.      The concepts with which we must be concerned during 
  247. this discussion involve assembly modes, run-time memory 
  248. addresses and assembly-time addresses.  During program 
  249. execution, addresses are referenced as data locations or as 
  250. locations which must be forced into the program counter in 
  251. order to effect a transfer of execution flow.  I will now 
  252. define the necessary concepts and the conditions under which 
  253. I will apply the terminology connected with each.  My 
  254. comments concerning things to which the word absolute 
  255. applies will be brief because our interests are in things to 
  256. which the words program counter relative and relocatable 
  257. apply.  Remember that PC-relative assembly is simply 
  258. Absolute assembly with (PC) automatically inserted for 
  259. convenience.  But remember also that there may be times when 
  260. absolute assembly would be preferable to PC-relative 
  261. assembly because of the 16-bit branch limitation associated 
  262. with pc-relative addressing.  When that limitation can't be 
  263. tolerated, automatic insertion of the (PC) suffix becomes an 
  264. inconvenience.
  265.  
  266. Concept One
  267.   
  268.      The AssemPro assembler has three assembly options.  
  269. These are PC-relative, Relocatable and Absolute.  For our 
  270. convenience only, I will use the name of the mode in which a 
  271. program was assembled as an adjective to describe a program 
  272. assembled in that particular mode.  For example, I will say 
  273. things such as "this is a PC-relative program; that is a 
  274. Relocatable program.
  275.      In addition to the AssemPro modes, I add one of my own, 
  276. the Combination (Combo) mode.  A Combo program will be one 
  277. that is assembled in AssemPro's Relocatable mode, but one in 
  278. which the location of labels used as source operands in 
  279. instructions are specified using the MC68000 Program Counter 
  280. With Displacement addressing mode.  The Combo mode produces 
  281. programs that take advantage of the faster execution speed 
  282. and smaller memory requirements of the program counter with 
  283. displacement addressing mode, and the mode also permits 
  284. labels to be used in destination operands.
  285.  
  286. Concept Two
  287.   
  288.      The MC68000 microprocessor has among its addressing 
  289. modes one which is called program counter with displacement, 
  290. one which is called program counter with index, one which is 
  291. called absolute short and one which is called absolute long.  
  292. We will be interested in the absolute short and absolute 
  293. long addressing modes when we wish to reference operating 
  294. system variables while the processor is in its supervisor 
  295. mode.  For the discussion in this chapter, we are primarily 
  296. interested in the program counter with displacement mode.  
  297. All of the references that I have seen replace this rather 
  298. cumbersome phrase with pc-relative, and refer to the 
  299. addressing mode as pc-relative addressing.  Now that I have 
  300. pointed out the difference between PC-relative assembly and 
  301. pc-relative addressing, we should be able to use this common 
  302. notation with no problem.
  303.  
  304. The Rub
  305.   
  306.      Before I go further, because I am going to say what 
  307. needs to be said about the MC68000, I must clarify my 
  308. position.  I consider Motorola microprocessors to be the 
  309. finest available.  And I speak from experience with their 
  310. devices and Intel's.  Yet, were it not for a serious flaw 
  311. (one of two) in the design of the MC68000 (My opinion, of 
  312. course, but the only viewpoint from which a sane discussion 
  313. of the processor's limitations can proceed.), this 
  314. discussion would not be taking place.
  315.      However else are we to view the obstacle to storing 
  316. data that is inherent in the chip's design?  Are we to 
  317. believe that someone would deliberately incorporate an 
  318. addressing scheme which attempts to thwart data storage in 
  319. the design of a computer's processor?  That is the snow job 
  320. that most of the references parrot.  No, the extra coding 
  321. that is necessary to overcome the limitation of the 
  322. MC68000's pc-relative addressing mode makes sense, and is 
  323. justified, only when that limitation is viewed as a design 
  324. error.
  325.      The flaw, of which I now speak, is the microprocessor's 
  326. inability to accept pc-relative destination operands.  
  327. Ostensibly, according to the references, Motorola chose this 
  328. method to protect us from mad dog, crazy-running-amok, self-
  329. modifying code producing programmers.  I shall prove to you 
  330. that the design of the pc-relative addressing mode does not 
  331. prevent the writing of data to destination operands; thus, I 
  332. must conclude that, if the limitation was designed 
  333. deliberately, it is a failure; therefore it is useless and 
  334. only hinders programming effort.
  335.      On the other hand, if the limitation was an error in 
  336. design, I must conclude that it was not deliberate, and, 
  337. being otherwise satisfied with the microprocessor, I choose 
  338. to accept the task of designing my programs to work around 
  339. the flaw.  That is this chapter's raison d'être: designing 
  340. PC-relative programs that work in spite of the flaw.  The 
  341. extra effort that must be exerted is justified as 
  342. corrections to an honest mistake in design.  Furthermore, I 
  343. will try to show you that there exists a viewpoint from 
  344. which we can consider the flaw to be an incentive to 
  345. inspirational algorithmic development, the result of which 
  346. shall be algorithms that are faster and consume less memory 
  347. than they might have were we not forced to use the 
  348. corrections.
  349.  
  350. PC-Relative Addressing: The Microprocessor Option
  351.   
  352.      I have decided that the most coherent explanation of 
  353. MC68000 pc-relative addressing is contained in the Kelly-
  354. Bootle book.  See pages 59-70, 91-92, 112-115  and 186-196.  
  355. But, of course, Mr. Kelly-Bootle's explanation is not Atari 
  356. St specific; he did not intend that it be.  My explanation 
  357. will fill in the blanks, but I do advise you to read Mr. 
  358. Kelly-Bootle's pc-relative material, and, if you've the 
  359. time, the other sections of chapters 1 through 7 you find 
  360. pertinent.  I am especially fond of the quotation at the 
  361. beginning of his introduction.
  362.      The importance attached to pc-relative addressing on 
  363. page 61 of the Kelly-Bootle book do not apply to the ST 
  364. because of its built in relocator.  The assembly directive 
  365. RORG does not apply either, because AssemPro replaces that 
  366. directive with the PC-relative assembly option under the 
  367. Assembler menu.  The importance of the LEA and PEA 
  368. instructions in PC-relative programs is amply documented in 
  369. this book, but there is a compound error on page 113.  The 
  370. first part of the error is semantical.  MOVEA.Z #<label>, A0 
  371. cannot be both equivalent and faster than LEA <label>, A0; 
  372. if you don't see that, look up the definition of 
  373. equivalence.
  374.      The second part of the error is factual.  These 
  375. instructions execute at identical speeds according to the 
  376. Instruction Execution Times tables in the Motorola M68000 
  377. Programmer's Reference Manual.  Furthermore, after I have 
  378. covered sufficient introductory material, I will present a 
  379. program which provides data which supports the Motorola 
  380. manual.  In fact, if you think about it, why should the 
  381. times be different.  Both instructions accomplish the same 
  382. task.
  383.  
  384. PC-Relative Mode: The Assembler Option
  385.   
  386.     Only programs which contain no destination operands that 
  387. are labels can be assembled with the assembler's PC-relative 
  388. mode asserted.  This means that you cannot write 
  389. instructions that store data in program variables by 
  390. referencing the labels of dc and bss declarations.  Instead, 
  391. you must store data in those locations via indirect 
  392. reference.  You do that by storing the location of the label 
  393. in an address register, then by using one of the MC68000's 
  394. Address Register Indirect addressing modes in the 
  395. destination operand portion of a data storing instruction.
  396.      This is the extra effort that must be expended to 
  397. nullify the flaw in the MC68000's Program Counter addressing 
  398. modes.  However, this effort does not always represent an 
  399. expense in speed and requisite memory.  Actually, expense is 
  400. incurred only when a single reference to a label is required 
  401. in a program, or when heavy register usage does not permit a 
  402. register to be assigned to a variable during a significant 
  403. portion of the program in which the variable must be 
  404. referenced.
  405.      When a program is assembled in the PC-relative mode, 
  406. the assembler forces the addressing mode of every label used 
  407. in an instruction operand into the MC68000 Program Counter 
  408. With Displacement addressing mode.  Of course, an error will 
  409. be reported for every instruction which contains a label in 
  410. the destination operand.  While it is true that programs 
  411. assembled in this mode are position independent, we are not 
  412. as interested in this attribute of PC-relative programs as 
  413. we are interested in their faster execution speed and lesser 
  414. requisite memory, because we can obtain position 
  415. independence simply by using the Relocatable mode.
  416.  
  417. Relocatable Mode: The Assembler Option
  418.   
  419.      As we have seen, it is possible to construct statements 
  420. which the ST relocator cannot handle properly, even though 
  421. the assembler will not sense an error.  I avoid such 
  422. problems because I do not include mathematical expressions 
  423. in my instruction operands.  I have enough problems with my 
  424. programs without trying to outguess the relocator and the 
  425. assembler.
  426.      It may not be readily apparent that AssemPro's 
  427. Relocatable mode does not preclude the use of the MC68000's 
  428. program counter with displacement addressing mode.  I have 
  429. decided to discuss such usage as though it were another 
  430. assembly mode.  I do this only because Mr. Schulz stressed 
  431. the automatic inclusion of (PC) for labels used as source 
  432. operands when assembly takes place in his PC-relative mode, 
  433. and I want to stress the advantage of using the processor's 
  434. pc-relative mode in programs that are to be assembled in 
  435. AssemPro's Relocatable mode, even though (pc) must be 
  436. manually typed after ever label in a source operand.
  437.  
  438. Combination Mode: The Option Mr. Schulz Forgot
  439.  
  440.      If AssemPro had been provided with an assembly mode 
  441. that inserted the (pc) for labels used as source operands, 
  442. but which simply ignored destination operands, then 
  443. assembled in Relocatable format, we would not have to type 
  444. (pc) behind each label in our source operands to take 
  445. advantage of the pc-relative addressing mode in Relocatable 
  446. programs.  As you shall see, by using the (pc) suffix 
  447. judiciously, we shall be able to reduce both execution time 
  448. and requisite memory.
  449.  
  450. Comparing Assembler Modes
  451.   
  452.      The introduction of the program 6a provides me with the 
  453. opportunity to begin a comparative analysis of the 
  454. consequences of each Assembler mode.  You have already seen 
  455. three forms of a program assembled in the Relocatable mode.  
  456. I would like you to obtain one piece of information about 
  457. PRG_2AR.PRG from the desktop.  By clicking one on the 
  458. program's icon, then by selecting Show Info under the File 
  459. menu, you will be able to observe and record the disk space 
  460. occupied by the program.  It is 78 bytes.
  461.      You can obtain a PC-relative version of the program, 
  462. simply by loading PRG_2AR.S into the editor, clicking on 
  463. AssemPro's PC-relative Assembly option and assembling.  
  464. Change the file name to PRG_2AP and save the assembled 
  465. program to disk with a PRG extension.  From the desktop, 
  466. obtain the disk space occupied by the program (PRG_2AP.PRG).  
  467. It is 64 bytes.
  468.      Program 6b is a listing of PRG_2AR.S transformed into a 
  469. program that uses pc-relative addressing for the labels in 
  470. the source operands.  You should assemble this program in 
  471. the Relocatable mode, change the file name to PRG_2AC and 
  472. save the assembled code to disk as PRG_2AC.PRG.  From the 
  473. desktop, obtain the disk space occupied by the program.  It 
  474. is 72 bytes.
  475.   
  476. Program 6b. PRG_2AR.S altered to produce PRG_2AC.S, a Combo 
  477. program.
  478.  
  479.  ; Program Name: PRG_2AC.S
  480.  ;      Version: 1.002
  481.  
  482.  ; Assembly Instructions:
  483.  
  484.  ;    Assemble in AssemPro Relocatable mode and save the assembled program
  485.  ; with a PRG extension.
  486.  
  487.  ; Program Function:
  488.  
  489.  ;    Serves as a source file for the discussion on pages 79 and 80 of the
  490.  ; AssemPro manual.
  491.  
  492.  move.l    up_1(pc), d0
  493.  move.l    up_2(pc), d0
  494.  move.l    #-4, d0
  495.  
  496.  move.l    #up_1-up_2,d0  ; (pc) can't be used with the labels here.
  497.  move.l    up_1-up_2, d0  ; (pc) can't be used with the labels here.
  498.  
  499.  ; NOTE: If the (pc) suffix is attached to both labels in the above
  500.  ;       statement, the assembler will flag the first as an error.  If the
  501.  ;       (pc) suffix is attached only to the second label, the instruction
  502.  ;       will be assembled into a statement that moves a portion of the 
  503.  ;       the preceding statement into register D0.
  504.  
  505.  data
  506. pointer:   dc.l  up_1
  507. up_1:      dc.l  5
  508. up_2:      dc.l  3
  509.  end
  510.  
  511.  
  512.      The disassemby listing of PRG_2AR.S assembled in the 
  513. PC-relative mode is shown in figure 3.5.  There you can see 
  514. one of the reasons that PRG_2AP.PRG has a lower requisite 
  515. memory than PRG_2AR.PRG.  The first, second and fifth lines 
  516. of the disassembly listing show that the labels in these 
  517. instructions have been cast in the pc-relative addressing 
  518. mode by the assembler, therefore, these source operands 
  519. require only one word of extension, whereas those 
  520. instructions in PRG_2AR.PRG require two words of extension.
  521.      Six bytes of requisite memory are saved via the use of 
  522. the pc-relative addressing mode, two for each of the three 
  523. instructions.  The additional eight byte differential 
  524. between the requisite memory for PRG_2AR.PRG and PRG_2AP.PRG 
  525. is due to the information which must be stored for the 
  526. relocator in PRG_2AR.PRG, as discussed on page 80 of the 
  527. AssemPro manual.
  528.  
  529. Figure 3.5. Disassembly listing for program 6a assembled in 
  530. the PC-relative mode.
  531.  
  532.  Disassembly listing of PRG_2AP.PRG
  533.  
  534. 06D5DA 203A001A      MOVE.L $6D5F6(PC),D0
  535. 06D5DE 203A001A      MOVE.L $6D5FA(PC),D0
  536. 06D5E2 203CFFFFFFFC  MOVE.L #-4,D0
  537. 06D5E8 203CFFFFFFFC  MOVE.L #-4,D0
  538. 06D5EE 203AFFE6      MOVE.L $6D5D6(PC),D0
  539.  
  540.  NOTE: Although the above statement has been assembled
  541.        without an error indication, the effective address
  542.        is not within the program's boundaries.
  543.  
  544. 06D5F2 0000001C      ORI.B #$1C,D0
  545. 06D5F6 00000005      ORI.B #5,D0
  546. 06D5FA 00000003      ORI.B #3,D0
  547.  
  548.  
  549.      The disassembly listing for PRG_2AC.PRG, shown in 
  550. figure 3.6, illustrates that the extension word savings can 
  551. be obtained when the pc-relative addressing mode is applied 
  552. to appropriate instructions in programs that are assembled 
  553. in the Relocatable mode.  In PRG_2AC.PRG, the pc-relative 
  554. mode can be applied to the first and second instructions.  
  555. This yields a four-byte savings.  An additional 2 bytes are 
  556. saved because less information must be stored for the 
  557. relocator in this program.
  558.  
  559. Figure 3.6. Disassembly listing for program 6b.
  560.  
  561.  Disassembly listing of PRG_2AC.PRG
  562.  
  563. 06D4F6 203A001C      MOVE.L $6D514(PC),D0
  564. 06D4FA 203A001C      MOVE.L $6D518(PC),D0
  565. 06D4FE 203CFFFFFFFC  MOVE.L #-4,D0
  566. 06D504 203CFFFFFFFC  MOVE.L #-4,D0
  567. 06D50A 2039FFFFFFFC  MOVE.L -4,D0
  568. 06D510 0006D514      ORI.B #$14,D6
  569. 06D514 00000005      ORI.B #5,D0
  570. 06D518 00000003      ORI.B #3,D0
  571.   
  572.   
  573.      These somewhat trivial comparisons have been introduced 
  574. in order to prepare you for the more complicated comparisons 
  575. to follow.  Besides the requisite memory differentials 
  576. between the three programs, by referring to the disassembly 
  577. listings, you should notice that the correct run-time 
  578. address of label up_1 is stored at the label pointer when 
  579. the example program is assembled in the Relocatable or Combo 
  580. modes, but when it is assembled in the PC-relative mode, the 
  581. value stored at pointer is the assembly-time location of the 
  582. label up_1.  See figure 3.7.
  583.   
  584. Figure 3.7. Assembly listing for program 6a assembled in the 
  585. PC-relative mode.  Compare the assembly-time address of up_1 
  586. to the run-time address stored at pointer in figure 3.5 and 
  587. notice that they are the same.  That is not what was 
  588. desired.
  589.  
  590. Address Objectcode   Line    Sourcetext PRG_2AR.S assembled in PC-relative mode                                 
  591.  
  592. 000000 :                1   ; Program Name: PRG_2AR.S
  593. 000000 :                2   ;      Version: 1.002
  594. 000000 :                3
  595. 000000 :                4   ; Assembly Instructions:
  596. 000000 :                5
  597. 000000 :                6   ;    Assemble in AssemPro Relocatable mode and save the assembled program
  598. 000000 :                7   ; with a PRG extension.
  599. 000000 :                8
  600. 000000 :                9   ; Program Function:
  601. 000000 :               10
  602. 000000 :               11   ;    Serves as a source file for the discussion on pages 79 and 80 of the
  603. 000000 :               12   ; AssemPro manual.
  604. 000000 :               13
  605. 000000 :203A001A       14   move.l    up_1, d0
  606. 000004 :203A001A       15   move.l    up_2, d0
  607. 000008 :203CFFFFFFFC   16   move.l    #-4, d0        ; The statement below will resemble this one
  608. 00000E :               17                            ; after assembly.  Both are relocatable.
  609. 00000E :203CFFFFFFFC   18   move.l    #up_1-up_2,d0  ; This is Mr. Schulz's example.
  610. 000014 :203AFFE6       19   move.l    up_1-up_2, d0  ; I think he meant to use this type of example,
  611. 000018 :               20   data                     ; because this one does cause a problem.
  612. 000018 :0000001C       21  pointer:   dc.l  up_1
  613. 00001C :00000005       22  up_1:      dc.l  5
  614. 000020 :00000003       23  up_2:      dc.l  3
  615. 000024 :               24   end
  616.  
  617.  
  618.      In addition to the incompatibility between the 
  619. declarations and the PC-relative mode just discussed, 
  620. certain move instructions are not compatible with assembly 
  621. in the PC-relative mode.  Programs 7a-7d are sources 
  622. prepared for assembly in each of the four possible assembly 
  623. modes.  The name of each source listing indicates the mode 
  624. of assembly.  The comments in each program convey assembly 
  625. and run-time expectations and the suitability or 
  626. unsuitability of pertinent instructions relative to the mode 
  627. of assembly.  Program 8 is a final example that stresses the 
  628. differences between instructions which are compatible with 
  629. PC-relative assembly and those which aren't.  This program 
  630. can be assembled in Relocatable or PC-relative mode.  But 
  631. when it is assembled in PC-relative mode, the code for 
  632. portions of it is not what we want.
  633.  
  634. Program 7a. ABSOLUTE.S
  635.  
  636.  ; Program Name: Absolute.s
  637.  ;      Version: 1.001
  638.  
  639.  ; Assembly Instructions:
  640.  
  641.  ;      Assemble in Absolute mode.
  642.  
  643.  ; Execution Instructions:
  644.  
  645.  ;      Go to the debugger and single step through the first 3 instructions.
  646.  ; When you attempt to single step the 4th instruction a BUS ERROR will occur.
  647.  ; Press the Return key when the alert box appears.  Then move the mouse arrow
  648.  ; to the 5th instruction in the debugger output field and, while holding the
  649.  ; Control key pressed, click on the left mouse button to move the program
  650.  ; counter cursor to the 5th instruction.  Finally, single step the 5th
  651.  ; instruction.
  652.  
  653.  ; Record the values that are stored in each of the four registers D0, D1,
  654.  ; A0 and A1.
  655.  
  656.  lea       label(pc), a0        ; Label's run time address is moved into A0.
  657.  move.l    label(pc), d0        ; Label's contents are moved into D0.
  658.  movea.l   #label, a1           ; Label's assembly time add is moved into A1.
  659.  
  660.  ; NOTE:  The MC68000 does not permit the (PC) suffix to be attached to a
  661.  ;        label that is preceded by a # sign.
  662.  
  663.  move.l    #$0, label
  664.  
  665.  ; Result of executing the above instruction is a BUS ERROR because label's
  666.  ; address is its assembly time value, and that value is an address that can
  667.  ; only be accessed while the processor is in supervisor mode.  You can prove
  668.  ; that this is true by clicking on the status register S box in the debugger
  669.  ; screen before executing the instruction again.  After that, I suggest that
  670.  ; you reset the computer because storing $0 at that address may not be a
  671.  ; healthy thing to do.
  672.  
  673.  ; NOTE:   Manually move the program counter cursor to the next instruction.
  674.  
  675.  move.l    label(pc), d1        ; Label's contents are moved into D1.
  676.  
  677. label:     dc.l $FFFFFFFF       ; Label's contents initialized to -1.
  678.  
  679.  end
  680.  
  681.   
  682. Program 7b. COMBO.S
  683.  
  684.  ; Program Name: Combo.s
  685.  ;      Version: 1.001
  686.  
  687.  ; Assembly Instructions:
  688.  
  689.  ;      Assemble in Relocatable mode.
  690.  
  691.  ; Execution Instructions:
  692.  
  693.  ;      Go to the debugger and click on the relocate button.  Single step
  694.  ; through the 5 instructions.  Record the values that are stored in each
  695.  ; of the four registers D0, D1, A0 and A1.
  696.  
  697.  lea       label(pc), a0        ; Label's run time address is moved into A0.
  698.  move.l    label(pc), d0        ; Label's contents are moved into D0.
  699.  movea.l   #label, a1           ; Label's run time address is moved into A1.
  700.  move.l    #$0, label           ; Label's contents are changed to $00000000.
  701.  
  702.  ; NOTE:  If you want to see the new contents of label after the above
  703.  ;        instruction is executed, click twice on the disassembled button
  704.  ;        located in the lower right hand corner of the debugger screen.
  705.  
  706.  move.l    label(pc), d1        ; Label's contents are moved into D1.
  707.  
  708. label:     dc.l $FFFFFFFF       ; Label's contents initialized to -1.
  709.  end
  710.  
  711. Program 7c. RELATIVE.S.
  712.  
  713.  ; Program Name: Relative.s
  714.  ;      Version: 1.001
  715.  
  716.  ; Assembly Instructions:
  717.  
  718.  ;      Attempt to assemble in PC-relative mode.  The assembler will not
  719.  ; accept the 4th instruction because of the attempt to use a label as a
  720.  ; destination operand.  Type a semicolon in front of the instruction in
  721.  ; order to comment it out, then assemble the program.
  722.  
  723.  ; Execution Instructions:
  724.  
  725.  ;      Go to the debugger and note that the assembler has attached a (PC)
  726.  ; suffix to the source operand "label" in the 1st, 2nd and 4th instructions.
  727.  ; Single step through the 4 instructions.  Record the values that are stored
  728.  ; in each of the four registers D0, D1, A0 and A1.
  729.  
  730.  lea       label, a0            ; Label's run time address is moved into A0.
  731.  move.l    label, d0            ; Label's contents are moved into D0.
  732.  movea.l   #label, a1           ; Label's assembly time address is moved into A1.
  733.  
  734.  ; NOTE:  The assembler will not attach a (PC) suffix to the source operand
  735.  ;        "label" in the above instruction.
  736.  
  737.  move.l    #$0, label 
  738.  
  739.  ; Assembler will not accept "label" as a destination operand because AssemPro
  740.  ; attaches a (PC) suffix to every label which is not preceded by a # sign in
  741.  ; a program that is assembled in the PC-relative mode.  The MC68000 does not
  742.  ; permit the (PC) suffix attachment to labels that are destination operands.
  743.  
  744.  move.l    label(pc), d1        ; Label's contents are moved into D1.
  745.  
  746. label:     dc.l $FFFFFFFF       ; Label's contents initialized to -1.
  747.  
  748.  end
  749.  
  750. Program 7d. RELOCATE.S
  751.  
  752.  ; Program Name: Relocate.s
  753.  ;      Version: 1.001
  754.  
  755.  ; Assembly Instructions:
  756.  
  757.  ;      Assemble in Relocatable mode.
  758.  
  759.  ; Execution Instructions:
  760.  
  761.  ;      Go to the debugger and click on the relocate button.  Single step
  762.  ; through the 5 instructions.  Record the values that are stored in each
  763.  ; of the four registers D0, D1, A0 and A1.
  764.  
  765.  lea       label, a0            ; Label's run time address is moved into A0.
  766.  move.l    label, d0            ; Label's contents are moved into D0.
  767.  movea.l   #label, a1           ; Label's run time address is moved into A1.
  768.  move.l    #$0, label           ; Label's contents are changed to $00000000.
  769.  
  770.  ; NOTE:  If you want to see the new contents of label after the above
  771.  ;        instruction is executed, click twice on the disassembled button
  772.  ;        located in the lower right hand corner of the debugger screen.
  773.  
  774.  move.l    label, d1            ; Label's contents are moved into D1.
  775.  
  776. label:     dc.l $FFFFFFFF       ; Label's contents initialized to -1.
  777.  
  778.  end
  779.  
  780.  
  781. Program 8. Instructions that are compatible with PC-relative 
  782. assembly and those that aren't.
  783.  
  784.  ; Program Name: PRG_2BR.S
  785.  
  786.  ; Assembly Instructions:
  787.  
  788.  ;    The algorithms in this program can be assembled in Relocatable or
  789.  ; PC-relative mode.  But when they are assembled in PC-relative mode, the
  790.  ; code is not always what we want.
  791.  
  792.  ; Experiment 1.
  793.  
  794.  ;    Shows that a pointer, declared in the data section, to a variable
  795.  ; declared in the bss section will contain the correct address when
  796.  ; assembly is in Relocatable mode; but when assembled in PC-relative mode,
  797.  ; the pointer will contain the location at which the variable resided
  798.  ; during the assembly process.
  799.  
  800.  movea.l   _variable, a0       ; A pointer to a variable is loaded into
  801.                                ; an address register.
  802.  ; End of Experiment 1.
  803.  
  804.  ; Experiment 2.
  805.  
  806.  ;    Illustrates that the instructions
  807.  
  808.  ;         move.l #label, -(sp)
  809.  ;         move.l #label, An
  810.  
  811.  ; are not compatible with assembly in the PC-relative mode, and that
  812.  ; the following instructions must be used instead.
  813.  
  814.  ;         pea    label
  815.  ;         lea    label.
  816.  
  817.  move.l    #label_1, -(sp)
  818.  move.l    #label_1, a0
  819.  move.l    #label_2, -(sp)
  820.  move.l    #label_2, a1
  821.  
  822.  pea       label_1
  823.  lea       label_1, a0
  824.  pea       label_2
  825.  lea       label_2, a1
  826.  
  827.  ; End of Experiment 2.
  828.  
  829.  data
  830. label_1:   dc.l      1
  831. _variable: dc.l variable       ; _variable is a pointer to variable.
  832.  bss
  833. label_2:   ds.l      1
  834. variable:  ds.l      1         ; During loading, we want the address of
  835.                                ; this variable to be stored in the
  836.                                ; location addressed by the pointer.
  837.  end
  838.  
  839.  
  840.      You should assemble this program in both the 
  841. Relocatable and PC-relative modes so that you can compare 
  842. the assembly and disassembly listings.  My assembly listings 
  843. appear in figures 3.8 and 3.9.  The disassembly listings are 
  844. shown in figures 3.10 and 3.11.  The assembly-time addresses 
  845. of the variables declared in the data and bss segments of 
  846. the program are evident in figures 3.8 and 3.9.  These 
  847. should be compared to the addresses that would be loaded 
  848. into the arrays and the stack at run-time, as shown in 
  849. disassembly listings.
  850.   
  851. Figure 3.8. Assembly listing for PRG_2BR.S assembled in the 
  852. PC-relative mode.
  853.  
  854.  Address Objectcode  Line    Sourcetext PRG_2BR.S Assembled in PC-relative mode                                
  855.  
  856. 000000 :                1   ; Program Name: PRG_2BR.S
  857. 000000 :                2
  858. 000000 :                3   ; Assembly Instructions:
  859. 000000 :                4
  860. 000000 :                5   ;    The algorithms in this program can be assembled in Relocatable or
  861. 000000 :                6   ; PC-relative mode.  But when they are assembled in PC-relative mode, the
  862. 000000 :                7   ; code is not always what we want.
  863. 000000 :                8
  864. 000000 :                9   ; Experiment 1.
  865. 000000 :               10
  866. 000000 :               11   ;    Shows that a pointer, declared in the data section, to a variable
  867. 000000 :               12   ; declared in the bss section will contain the correct address when
  868. 000000 :               13   ; assembly is in Relocatable mode; but when assembled in PC-relative mode,
  869. 000000 :               14   ; the pointer will contain the location at which the variable resided
  870. 000000 :               15   ; during the assembly process.
  871. 000000 :               16
  872. 000000 :207A002E       17   movea.l   _variable, a0       ; A pointer to a variable is loaded into
  873. 000004 :               18                                 ; an address register.
  874. 000004 :               19   ; End of Experiment 1.
  875. 000004 :               20
  876. 000004 :               21   ; Experiment 2.
  877. 000004 :               22
  878. 000004 :               23   ;    Illustrates that the instructions
  879. 000004 :               24
  880. 000004 :               25   ;         move.l #label, -(sp)
  881. 000004 :               26   ;         move.l #label, An
  882. 000004 :               27
  883. 000004 :               28   ; are not compatible with assembly in the PC-relative mode, and that
  884. 000004 :               29   ; the following instructions must be used instead.
  885. 000004 :               30
  886. 000004 :               31   ;         pea    label
  887. 000004 :               32   ;         lea    label.
  888. 000004 :               33
  889. 000004 :2F3C0000002C   34   move.l    #label_1, -(sp)
  890. 00000A :207C0000002C   35   move.l    #label_1, a0
  891. 000010 :2F3C00000034   36   move.l    #label_2, -(sp)
  892. 000016 :227C00000034   37   move.l    #label_2, a1
  893. 00001C :               38
  894. 00001C :487A000E       39   pea       label_1
  895. 000020 :41FA000A       40   lea       label_1, a0
  896. 000024 :487A000E       41   pea       label_2
  897. 000028 :43FA000A       42   lea       label_2, a1
  898. 00002C :               43
  899. 00002C :               44   ; End of Experiment 2.
  900. 00002C :               45
  901. 00002C :               46   data
  902. 00002C :00000001       47  label_1:   dc.l      1
  903. 000030 :00000038       48  _variable: dc.l variable       ; _variable is a pointer to variable.
  904. 000034 :               49   bss
  905. 000034 : ^     4       50  label_2:   ds.l      1
  906. 000038 : ^     4       51  variable:  ds.l      1         ; During loading, we want the address of
  907. 00003C :               52                                 ; this variable to be stored in the
  908. 00003C :               53                                 ; location addressed by the pointer.
  909. 00003C :               54   end
  910.  
  911.  
  912. Figure 3.9. Assembly listing for PRG_2BR.S assembled in the 
  913. Relocatable mode.
  914.  
  915.  Address Objectcode  Line    Sourcetext PRG_2BR.S Assembled in Relocatable mode                                
  916.  
  917. 000000 :                1   ; Program Name: PRG_2BR.S
  918. 000000 :                2
  919. 000000 :                3   ; Assembly Instructions:
  920. 000000 :                4
  921. 000000 :                5   ;    The algorithms in this program can be assembled in Relocatable or
  922. 000000 :                6   ; PC-relative mode.  But when they are assembled in PC-relative mode, the
  923. 000000 :                7   ; code is not always what we want.
  924. 000000 :                8
  925. 000000 :                9   ; Experiment 1.
  926. 000000 :               10
  927. 000000 :               11   ;    Shows that a pointer, declared in the data section, to a variable
  928. 000000 :               12   ; declared in the bss section will contain the correct address when
  929. 000000 :               13   ; assembly is in Relocatable mode; but when assembled in PC-relative mode,
  930. 000000 :               14   ; the pointer will contain the location at which the variable resided
  931. 000000 :               15   ; during the assembly process.
  932. 000000 :               16
  933. 000000 :20790000003A   17   movea.l   _variable, a0       ; A pointer to a variable is loaded into
  934. 000006 :               18                                 ; an address register.
  935. 000006 :               19   ; End of Experiment 1.
  936. 000006 :               20
  937. 000006 :               21   ; Experiment 2.
  938. 000006 :               22
  939. 000006 :               23   ;    Illustrates that the instructions
  940. 000006 :               24
  941. 000006 :               25   ;         move.l #label, -(sp)
  942. 000006 :               26   ;         move.l #label, An
  943. 000006 :               27
  944. 000006 :               28   ; are not compatible with assembly in the PC-relative mode, and that
  945. 000006 :               29   ; the following instructions must be used instead.
  946. 000006 :               30
  947. 000006 :               31   ;         pea    label
  948. 000006 :               32   ;         lea    label.
  949. 000006 :               33
  950. 000006 :2F3C00000036   34   move.l    #label_1, -(sp)
  951. 00000C :207C00000036   35   move.l    #label_1, a0
  952. 000012 :2F3C0000003E   36   move.l    #label_2, -(sp)
  953. 000018 :227C0000003E   37   move.l    #label_2, a1
  954. 00001E :               38
  955. 00001E :487900000036   39   pea       label_1
  956. 000024 :41F900000036   40   lea       label_1, a0
  957. 00002A :48790000003E   41   pea       label_2
  958. 000030 :43F90000003E   42   lea       label_2, a1
  959. 000036 :               43
  960. 000036 :               44   ; End of Experiment 2.
  961. 000036 :               45
  962. 000036 :               46   data
  963. 000036 :00000001       47  label_1:   dc.l      1
  964. 00003A :00000042       48  _variable: dc.l variable       ; _variable is a pointer to variable.
  965. 00003E :               49   bss
  966. 00003E : ^     4       50  label_2:   ds.l      1
  967. 000042 : ^     4       51  variable:  ds.l      1         ; During loading, we want the address of
  968. 000046 :               52                                 ; this variable to be stored in the
  969. 000046 :               53                                 ; location addressed by the pointer.
  970. 000046 :               54   end
  971.  
  972.  
  973. Figure 3.10. Disassembly listing for PRG_2BR.S assembled in 
  974. the PC-relative mode.
  975.  
  976.  PRG_2BR.S assembled in PC-relative mode
  977.  
  978. 05A1E6 207A002E      MOVEA.L $5A216(PC),A0
  979. 05A1EA 2F3C0000002C  MOVE.L #$2C,-(A7)
  980. 05A1F0 207C0000002C  MOVEA.L #$2C,A0
  981. 05A1F6 2F3C00000034  MOVE.L #$34,-(A7)
  982. 05A1FC 227C00000034  MOVEA.L #$34,A1
  983. 05A202 487A000E      PEA $5A212(PC)
  984. 05A206 41FA000A      LEA $5A212(PC),A0
  985. 05A20A 487A000E      PEA $5A21A(PC)
  986. 05A20E 43FA000A      LEA $5A21A(PC),A1
  987. 05A212 00000001      ORI.B #1,D0         ; location of label_1.
  988. 05A216 00000038      ORI.B #$38,D0       ; location of _variable.
  989. 05A21A 542E5300      ADDQ.B #2,$5300(A6) ; location of label_2.
  990. 05A21E 4620          NOT.B -(A0)         ; location of variable.
  991. 05A220 5259          ADDQ.W #1,(A1)+
  992.  
  993.  
  994.      Looking at the first instruction in figure 3.10, you 
  995. can see that the value $38, which is the contents of memory 
  996. location $05A216, will be loaded into register A0.  With 
  997. this instruction, the value we wanted to store in A0 is the 
  998. run-time address of variable.  Referring to figure 3.11, you 
  999. can see that this desire is fulfilled when the program is 
  1000. assembled in the Relocatable mode.  This is a good time to 
  1001. point out that, although the disassembler indicates the 
  1002. actual address from which the value will be taken in the 
  1003. first instruction of figure 3.10, the object code itself 
  1004. references the location relatively.
  1005.      The object code at address $05A1E6 can be divided into 
  1006. the instruction word and the extension word as shown below.
  1007.  
  1008.                    Instruction word: 207A
  1009.  
  1010.                    Extension Word:   002E
  1011.  
  1012. The instruction word can be divided into its component bits 
  1013. as shown below:
  1014.  
  1015.             Component  1  2  3   4   5   6
  1016.             
  1017.                       00 10 000 001 111 010 
  1018.               
  1019.      The first component of the instruction word indicates a 
  1020. movea operation; the second component indicates that the 
  1021. operation is word size.  Remember, our instruction indicated 
  1022. that the operation was to be longword size.  The operation 
  1023. size is reduced automatically because the effective address 
  1024. is now specified in the pc-relative addressing mode.  Even 
  1025. so, the word size operand will be extended to a 32 bit 
  1026. quantity before the operation is done, because the 
  1027. destination is an address register.
  1028.      The third component of the instruction word indicates 
  1029. that the destination register is 0, and the fourth component 
  1030. indicates that the destination is an address register.  The 
  1031. fifth and sixth components indicate that effective address 
  1032. of the source operand is specified by pc-relative 
  1033. addressing.  The displacement to be used in calculating the 
  1034. address of the source operand is specified in the extension 
  1035. word; that value is $2E.
  1036.      The actual address of the source operand is calculated 
  1037. by adding $2E to the contents of the program counter when it 
  1038. contains the address of the extension word.  That is, 
  1039. $05A1E8 + $2E = $5A216.  The disassembler kindly shows us 
  1040. the results of the computation in the disassembly listing.  
  1041. Referring to the disassembly listing for the Relocatable 
  1042. program, in figure 3.11, you can see that no such 
  1043. computation is necessary because the actual address of the 
  1044. source operand is included in the object code for the first 
  1045. instruction.
  1046.      Figure 3.10 also shows that the values pushed onto the 
  1047. stack or loaded into the address registers  by the next four 
  1048. instructions of the program will produce equally undesirable 
  1049. results.  Then, the four instructions which follow those 
  1050. four illustrate the use of lea and pea to load the correct 
  1051. run-time addresses into address registers and the stack in a 
  1052. PC-relative program.  For example, the first pea instruction 
  1053. will push the address equal to the value in the extension 
  1054. word, $E, plus the address of the extension word, $5A204, 
  1055. which is $5A212, the address of label_1.
  1056.  
  1057. Figure 3.11. Disassembly listing for PRG_2BR.S assembled in 
  1058. the Relocatable mode.
  1059.  
  1060.  PRG_2BR.S assembled in Relocatable mode
  1061.  
  1062. 05A1E6 20790005A220  MOVEA.L $5A220,A0
  1063. 05A1EC 2F3C0005A21C  MOVE.L #$5A21C,-(A7)
  1064. 05A1F2 207C0005A21C  MOVEA.L #$5A21C,A0
  1065. 05A1F8 2F3C0005A224  MOVE.L #$5A224,-(A7)
  1066. 05A1FE 227C0005A224  MOVEA.L #$5A224,A1
  1067. 05A204 48790005A21C  PEA $5A21C
  1068. 05A20A 41F90005A21C  LEA $5A21C,A0
  1069. 05A210 48790005A224  PEA $5A224
  1070. 05A216 43F90005A224  LEA $5A224,A1
  1071. 05A21C 00000001      ORI.B #1,D0         ; location of label_1.
  1072. 05A220 0005A228      ORI.B #$28,D5       ; location of _variable.
  1073. 05A224 00000002      ORI.B #2,D0         ; location of label_2.
  1074. 05A228 06060606      ADDI.B #6,D6        ; location of variable.
  1075.  
  1076.  
  1077.      So far, I have concentrated on the requisite memory 
  1078. advantage of pc-relative addressing, whether is is 
  1079. accomplished via the PC-relative assembly mode or by forcing 
  1080. the mode on labels which are source operands in programs 
  1081. assembled in the Relocatable mode.  In chapter 6 I will 
  1082. focus attention on the execution speed advantage of pc-
  1083. relative addressing and the loading speed advantage of 
  1084. programs assembled in AssemPro's PC-relative mode.
  1085.  
  1086. Emphasizing Speed
  1087.   
  1088.      Writing algorithms that execute rapidly requires 
  1089. discipline and dedication to the task of learning which 
  1090. instructions execute fastest and a somewhat fanatical desire 
  1091. to seek out those less obvious instruction combinations 
  1092. which execute faster than those which seem to flow from the 
  1093. intellect much faster than their execution time.  Magazine 
  1094. articles are probably the best reference material for coding 
  1095. that concentrates of speed improvement.
  1096.      An excellent example of such an article, which 
  1097. discusses speed and traps (not the exception kind), is 68000 
  1098. Tricks and Traps, by Mike Morton, published in Byte, 
  1099. September, 1986, pages 163-172.  This is article is so 
  1100. valuable, I suggest that you write to Byte for a reprint, or 
  1101. try to obtain one from a library.  During your search for 
  1102. the fastest algorithms, I caution you to believe only what 
  1103. you prove with your own programs.  Some references advance 
  1104. claims that seem to be untried and unproven.
  1105.      For example, Mr. Morton advances the following 
  1106. conclusion: multiplying a word by 4 is faster with
  1107.  
  1108.                       add.w dn,dn
  1109.   
  1110.                       add.w dn,dn
  1111.   
  1112. than                  asl.w #2, dn.
  1113.   
  1114. I will present a program which refutes this assertion in 
  1115. chapter 6.  Also, in his section on Fast subroutine calls, 
  1116. Mr. Morton seems to indicate that the LEA instruction always 
  1117. references labels with pc-relative addressing; this is not 
  1118. true for programs assembled in AssemPro's Relocatable mode, 
  1119. unless the pc-relative addressing is forced with label(pc).
  1120.      The first stage of increasing a program's execution 
  1121. speed is getting it into ram as fast as possible.  Some ways 
  1122. of doing this is by moving from a ram disk to main ram, 
  1123. using utilities such as Switch/Back, produced by Alpha 
  1124. Systems (I use this product.), and by reducing the loading 
  1125. time.  This latter method will be discussed in chapter 6.  
  1126. Before I can introduce the algorithm that I shall use to 
  1127. measure a program's load and execute time, I must present 
  1128. the introductory material which makes the algorithm 
  1129. comprehensible.
  1130.  
  1131. Conclusion
  1132.   
  1133.      In this chapter my intent was to show you the 
  1134. difference between programs produced by four assembly modes.  
  1135. In future chapters, the emphasis will be placed on programs 
  1136. assembled in the PC-relative mode.  In addition, I have 
  1137. distinguished pc-relative addressing from AssemPro's PC-
  1138. relative assembly mode.
  1139.      The basics of pc-relative addressing is covered in the 
  1140. references, most comprehensively in the Kelly-Bootle book.  
  1141. But you will see more examples of its use in the programs to 
  1142. be presented in future chapters of this book.  I have 
  1143. assumed the task of explaining the significance of 
  1144. AssemPro's PC-relative and Relocatable assembly modes, and I 
  1145. have suggested that programs which explicitly use pc-
  1146. relative addressing and are assembled in the Relocatable 
  1147. mode are best understood when their mode of assembly is 
  1148. referenced as if it were a fourth option.
  1149.  
  1150.