home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / programming / forthmacs / htmldocs / !Forthmacs / docs / html / armastut < prev    next >
Encoding:
Text File  |  1996-02-23  |  20.8 KB  |  458 lines

  1. <!-- Forthmacs Formatter generated HTML output -->
  2. <html>
  3. <head>
  4. <title>Assembler Tutorial</title>
  5. </head>
  6. <body>
  7. <h1>Assembler Tutorial</h1>
  8. <hr>
  9. <p>
  10. This chapter explains how to use the Risc-OS Forthmacs ARM assembler in order to 
  11. create short machine language code sequences.  This chapter is a companion to 
  12. the "ARM Assembler" chapter.  That chapter describes the syntax of individual 
  13. assembly language instructions.  This chapter addresses "higher level" issues, 
  14. such as how to begin and end the assembly process and how to communicate 
  15. arguments and result between Forth and assembly language.  
  16. <p>
  17. <p>
  18. <h2>Motivation</h2>
  19. <p>
  20. For nearly all debugging jobs, writing assembly language is unnecessary.  Test 
  21. loops can be usually be written more quickly and easily in high-level Forth, and 
  22. will execute quickly enough to get the job done.  
  23. <p>
  24. However, in some cases the ultimate in speed is needed for certain critical 
  25. operations, and assembly language may be the best way to go.  In other cases, 
  26. very specific combinations of machine instructions may exhibit problem behavior, 
  27. and those combinations may need to be reproduced.  Finally, some maintainers of 
  28. the Risc-OS Forthmacs system software itself may need to understand the 
  29. assembler.  
  30. <p>
  31. <p>
  32. <h2>Assumptions</h2>
  33. <p>
  34. The chapter assumes that you already understand the ARM instruction set, 
  35. including such issues as processor modes, interrupts and registers sets.  If 
  36. not, you should first study a ARM reference, such as the manual published by the 
  37. chip manufacturer.  
  38. <p>
  39. <p>
  40. <h2>Example: a simple "code word"</h2>
  41. <p>
  42. Here is a very simple assembly language program.  It adds "1" to the contents of 
  43. a register then returns to the Forth interpreter.  
  44. <p>
  45. <br><code>    code addone  ( n -- n+1 )</code><br>
  46. <br><code>    r10 r10  1 # add</code><br>
  47. <br><code>    c;</code><br>
  48. <p>
  49. To execute it and display the result, you would type, for example, 
  50. <p>
  51. <br><code>    5 addone .</code><br>
  52. <p>
  53. Here's what is happening, line by line: 
  54. <p>
  55. <br><code>    code addone  ( n -- n+1 )</code><br>
  56. <p>
  57.  <code><A href="_smal_AH#187"> code </A></code> is a "defining word"; it creates 
  58. a new command which can be executed by typing its name.  The name of the new 
  59. command in this case is <strong>addone</strong> .  The name could have been 
  60. anything; I have chosen the name <strong>addone</strong> because it describes 
  61. the action of the program.  You may already be familiar with another Forth 
  62. defining word " <strong>:</strong> or <strong>colon</strong>  <code><A href="_smal_AP#6f"> ". </A></code> 
  63. ":" also creates a new command; the difference between  <code><A href="_smal_AH#187"> code </A></code> 
  64. and ":" is that ":" creates a new command whose behavior is described by a 
  65. sequence of other Forth commands, whereas  <code><A href="_smal_AH#187"> code </A></code> 
  66. creates a new command whose behavior is described by a sequence of assembly 
  67. language instructions.  After  <code><A href="_smal_AH#187"> code </A></code> 
  68. creates the new command, it starts the assembler so that assembly language 
  69. instructions may be entered.  
  70. <p>
  71. The stuff inside the parentheses is a comment; this particular comment indicates 
  72. that the new command expects one argument ("n") on the stack before the word is 
  73. executed, and after the command is executed, one result ("n+1") is left on the 
  74. stack.  The comment is optional, but its inclusion is strongly recommended.  
  75. <p>
  76. <br><code>    r10 r10  1 # add</code><br>
  77. <p>
  78. This is the assembly language instruction which defines the action of the new 
  79. command.  As you will recall from the "ARM Assembler" chapter, the 
  80. Risc-OS Forthmacs assembler syntax has the destination register first, followed 
  81. by the source operand(s), followed by the operation name.  So, in this case, the 
  82. source operands are the global register r10 and the immediate number 1, the 
  83. destination operand is the global register r10, and the operation is add, i.e.  
  84. 1 is added to the contents of register r10, and the result is placed back in 
  85. register r10.  
  86. <p>
  87. <br><code>    c;</code><br>
  88. <p>
  89.  <code><A href="_smal_BJ#171"> c; </A></code> terminates the definition of a 
  90. code definition.  At the end of the instructions you have assembled,  <code><A href="_smal_BJ#171"> c; </A></code> 
  91. automatically appends one machine instruction, its effect is to return to Forth 
  92. after the user-specified instructions have been executed.  
  93. <p>
  94. <br><code>    5 addone .</code><br>
  95. <p>
  96. In order to invoke the new command, we enter the number 5 on the Forth stack, 
  97. type the name of the command <strong>addone</strong> , and then display the 
  98. result by typing the print command  <code>"<A href="_smal_AV#d5"> ." </A></code> 
  99. .  
  100. <p>
  101. Perhaps you now wonder how the number got off the Forth stack and into the 
  102. register r10, and afterwards how the number got out of r10 and back onto the 
  103. Forth stack.  The answer is simple: the top element of the Forth stack is always 
  104. (!) kept in r10 , so no movement was necessary.  That is why I chose r10 for the 
  105. register in this example.  
  106. <p>
  107. <p>
  108. <h2>Register Usage in Forth</h2>
  109. <p>
  110. To use the assembler effectively, you need to know which registers are available 
  111. for use, and which of them must be left alone.  Here are the rules: 
  112. <p>
  113. r8, r9, r12, and r14 are used internally by the Forth interpreter or operating 
  114. system, their values must be left alone (otherwise the system will crash).  
  115. <p>
  116. r10 contains the top of the Forth stack.  It is used for passing arguments and 
  117. results back and forth between Forth and assembly language.  
  118. <p>
  119. r13 contains a pointer to a memory area containing the rest of the Forth stack 
  120. (all elements other than the topmost one).  That stack area is used for extra 
  121. arguments and results.  The section entitled "Stack Usage" tells you more about 
  122. managing the stack area.  
  123. <p>
  124. r0 - r6 may be used freely within assembly language code sequences.  Forth does 
  125. not depend on the contents of these registers.  However, some Forth commands  <code><A href="_smal_AT#1c3"> do </A></code> 
  126. use these registers as scratch registers, so your code should not attempt to 
  127. keep important values in these registers from one time to the next.  While your 
  128. code is being executed, Forth will not change the contents of any of these 
  129. registers, so you can depend on them for the duration of your assembly language 
  130. sequence.  When your code finishes and returns to Forth, the next time that you 
  131. execute your code the register values may have changed.  
  132. <p>
  133. While your machine code is executing, it will run at the full speed of the 
  134. system, without any interference or overhead imposed by Risc-OS Forthmacs.  
  135. Risc-OS Forthmacs does not itself use interrupts, so the processor will execute 
  136. exactly the sequence of instructions which you have coded.  It is possible that 
  137. other software in the system may have set up some interrupts, but that is beyond 
  138. the control of Risc-OS Forthmacs.  
  139. <p>
  140. <p>
  141. <h2>Disassembler</h2>
  142. <p>
  143. The Risc-OS Forthmacs disassembler may be used to review the assembly language 
  144. you have created: 
  145. <p>
  146. <br><code>    see addone</code><br>
  147. <p>
  148. The result will look something like this: 
  149. <br><code>    code addone</code><br>
  150. <br><code>    (  1e878 )  add     r10,r10,#1</code><br>
  151. <br><code>    (  1e87c )  ldr     pc,[r8],#4</code><br>
  152. <p>
  153. <p>
  154. The numbers along the left hand side are the addresses at which the various 
  155. instructions appear.  The addresses shown here will almost certainly be 
  156. different from the addresses that you see.  
  157. <p>
  158. You will notice that even though our example contained only one assembly 
  159. language instruction the disassembler shows 1 extra instruction.  This extra 
  160. instruction was automatically assembled by the  <code><A href="_smal_BJ#171"> c; </A></code> 
  161. command.  Their purpose is to return control to Forth after the assembly 
  162. language sequence has finished its execution (this is called the  <code><A href="_smal_AK#3a"> next </A></code> 
  163. instruction).  
  164. <p>
  165. The  <code><A href="_smal_BT#2cb"> see </A></code> command reads the name of a 
  166. Forth command (in this case "addone"), determines what type of command it is (in 
  167. this case "code  <code><A href="_smal_AO#6e"> ", </A></code> meaning that the 
  168. command's behavior was defined by the assembler), and then displays a 
  169. reconstruction of the source code for that command.   <code><A href="_smal_BT#2cb"> see </A></code> 
  170. also works for "colon" definitions, whose behaviour is defined in Forth instead 
  171. of in assembly language.  For an example of this, type "see find".  
  172. <p>
  173. Many of the normal Forth commands are defined in assembly language, and  <code><A href="_smal_BT#2cb"> see </A></code> 
  174. can be used to look at how they are implemented.  For example, type "see @" to 
  175. see how the Forth "@" operator works (pronounced fetch, this operator takes an 
  176. address from the top of the stack, reads the 32-bit contents of that address, 
  177. and puts those contents back on top of the stack).  You should try this right 
  178. now and make sure you understand how it works.  Note that the last instructions 
  179. of "@" is exactly the same as the last instruction of "addone".  Every code 
  180. definition in Risc-OS Forthmacs ends with these same three instructions.  
  181. <p>
  182.  <code><A href="_smal_BT#2cb"> see </A></code> automatically locates the address 
  183. where the code for particular command begins.  That address was allocated by  <code><A href="_smal_AH#187"> code </A></code> 
  184. when the new command was defined.  The disassembler can also be used to inspect 
  185. machine code beginning at arbitrary addresses, not only that code which is 
  186. created by  <code><A href="_smal_AH#187"> code </A></code> .  Suppose that you 
  187. know there is some code starting at address 100000 and you wish to look at it: 
  188. <p>
  189. <br><code>    100000 dis</code><br>
  190. <p>
  191. On your system, this example probably won't work exactly as shown because your 
  192. system may not have any code at address 100000 (in fact, it may not even have 
  193. any memory there.  The main point, though, is that you type the address of the 
  194. code you wish to disassemble, followed by "dis".  
  195. <p>
  196. The disassembler will continue until it reaches a "definition ending" 
  197. instruction, or until you stop it by typing the character "q", for "quit".  It 
  198. will also pause at the end of a screen and prompt you for a continuation 
  199. character.  
  200. <p>
  201. After the disassembler has stopped, you can make it continue where it left off 
  202. by typing  <code><A href="_smal_AL#cb"> +dis </A></code> 
  203. <p>
  204. <p>
  205. <h2>Setting the Starting Address</h2>
  206. <p>
  207. In most cases, you won't need to specify a starting address for the code you 
  208. assemble.  When you use the  <code><A href="_smal_AH#187"> code </A></code> 
  209. defining word to begin assembling, Risc-OS Forthmacs will find some appropriate 
  210. memory for you and assemble your code there ( at  <code><A href="_smal_AM#21c"> here) </A>.</code> 
  211. You can then locate the memory Risc-OS Forthmacs has chosen by using the  <code><A href="_smal_BT#2cb"> see </A></code> 
  212. command to disassemble the code, looking at the addresses displayed alongside 
  213. the machine instructions.  
  214. <p>
  215. If you really need to assemble at a specific address, you can do so as follows 
  216. (Note: in nearly all cases, this technique is unnecessary; very rarely does it 
  217. matter where exactly you locate a bit of code, and allowing Risc-OS Forthmacs to 
  218. allocate the memory for you is sufficient and convenient).  
  219. <p>
  220. Set the  <code><A href="_smal_BH#1cf"> dp </A></code> by 
  221. <br><code>    here @</code><br>
  222. <br><code>    your-adr dp !</code><br>
  223. <br><code>    code demo</code><br>
  224. <br><code>           ...... c;</code><br>
  225. <br><code>    here !</code><br>
  226. <p>
  227. <p>
  228. <h2>Conditional branches</h2>
  229. <p>
  230. In order to implement conditional operations and loops, most assemblers provide 
  231. branch instructions and labels.  Risc-OS Forthmacs has branches and labels too, 
  232. but it also has a much better way, which eliminates most of the troublesome 
  233. aspects of coding conditionals and loops in assembly language.  The 
  234. Risc-OS Forthmacs way is called "structured conditionals".  For example, suppose 
  235. we want to test a condition and execute some code only if the condition is true.  
  236. Specifically, we want to compare r0 and r1, and execute some code only if r0 is 
  237. less than r1 .  
  238. <p>
  239. <br><code>    Traditional assembler:</code><br>
  240. <br><code>    </code><br>
  241. <br><code>                 cmp   r0, r1</code><br>
  242. <br><code>                 bge   temp</code><br>
  243. <br><code>                    ..some code we want to conditionally execute</code><br>
  244. <br><code>         temp:</code><br>
  245. <p>
  246. <br><code>         Forthmacs assembler with structured conditionals:</code><br>
  247. <br><code>    </code><br>
  248. <br><code>                 r0 r1  cmp</code><br>
  249. <br><code>                 < if</code><br>
  250. <br><code>                    ..some code we want to conditionally execute..</code><br>
  251. <br><code>                 then</code><br>
  252. <p>
  253. As you can see, Risc-OS Forthmacs eliminates the need to mentally reverse the 
  254. sense of the comparison, eliminates the need to invent and keep track of label 
  255. names, and uses conventional mathematical comparison symbols (e.g.  "<"), 
  256. rather then alphabetic mnemonics.  The complete set of comparison symbols is 
  257. given in the "ARM Assembler" chapter.  
  258. <p>
  259. The "if ..  then" construct can also include an "else" clause: 
  260. <p>
  261. <br><code>                 r0 r1 s cmp  \ the s is optional</code><br>
  262. <br><code>                 < if</code><br>
  263. <br><code>                    ..code to execute if r0 < r1..</code><br>
  264. <br><code>                 else</code><br>
  265. <br><code>                    ..code to execute if r0 >= r1..</code><br>
  266. <br><code>                 then</code><br>
  267. <p>
  268. Of course, the assembler actually generates conditional branch instructions 
  269. because that's what the hardware supports directly, but Risc-OS Forthmacs takes 
  270. care of the "bookkeeping" for you.  
  271. <p>
  272. Another way would be to use the conditional instructions offered by the ARM cpu.  
  273. <p>
  274. <br><code>                 r0  r1 cmp</code><br>
  275. <br><code>                 xx xx  lt xxx</code><br>
  276. <br><code>                 yy yy  ge xxx</code><br>
  277. <p>
  278. <p>
  279. <p>
  280. <h2>Delayed Branches</h2>
  281. <p>
  282. ARM doesn't uses delayed branches at all, so don't worry.  
  283. <p>
  284. <p>
  285. <h2>Loops</h2>
  286. <p>
  287. Risc-OS Forthmacs structured conditionals also have features for easily creating 
  288. loops.  Here is a loop which executes forever: 
  289. <p>
  290. <br><code>                 Source                  Generates</code><br>
  291. <br><code>    </code><br>
  292. <br><code>                 begin                   Label1:</code><br>
  293. <br><code>                    top  r0 ) ldr              ldr  r10,[r10,#0]</code><br>
  294. <br><code>                 again                         b Label1</code><br>
  295. <p>
  296. This code assumes that the r10 register (top of stack, remember?) contains the 
  297. address of a memory location, and the contents of that memory location is 
  298. continuously read into the r0 register.  This is an infinite loop; it won't stop 
  299. until the system is reset, or power cycled, or externally interrupted in some 
  300. way.  
  301. <p>
  302. Suppose we want the loop to execute 9 times then quit: 
  303. <p>
  304. <p><pre>
  305.   r1  9 #    mov
  306.   begin
  307.      r0   top ) ldr
  308.      r1   r1 1 # s sub
  309.   <= until
  310. </pre><p>
  311. <p>
  312. <p>
  313. We continue to loop "until" r1  <code><A href="_smal_BQ#118"> <= </A></code> 
  314. 1 .  
  315. <p>
  316. Finally, here's an example where we perform a test at the top of the loop rather 
  317. than at the bottom, illustrating "while": 
  318. <p>
  319. <p><pre>
  320.   r1  9 #     mov
  321.   begin
  322.       r1 r1 1 s sub
  323.   > while
  324.       r0  top ) ldr
  325.   repeat
  326. </pre><p>
  327. <p>
  328. <p>
  329. This loop continues to execute "while" r11 > 1, and the "repeat" sends it 
  330. back to the "begin".  
  331. <p>
  332. Structured conditionals and loops nest in the expected manner, to an arbitrary 
  333. depth.  For instance, a "begin ..  until" can be completely contained within an 
  334. "if ..  then", which itself may be contained within a "begin ..  while ..  
  335. repeat".  
  336. <p>
  337. <p>
  338. <h2>Scope Loops - Assembler vs. Forth</h2>
  339. <p>
  340. You can use assembly language for creating scope loops, but it is usually 
  341. preferable to write them in Forth, because the Forth version is usually easier 
  342. to write, easier to read, and easier to debug.  The one advantage of an assembly 
  343. language loop is that it is tighter.  However this rarely matters.  For 
  344. comparison, suppose that you want to continually read location 1000 so that you 
  345. can observe the action on an oscilloscope.  This is how you would do it in 
  346. assembly language: 
  347. <p>
  348. <br><code>         code test</code><br>
  349. <br><code>         r0 th 1000 # mov</code><br>
  350. <br><code>            begin</code><br>
  351. <br><code>              r1  r0 ) ldr</code><br>
  352. <br><code>            again</code><br>
  353. <p>
  354. Here's how you would do the same thing in Forth: 
  355. <p>
  356. <br><code>    begin  1000 @ drop  again</code><br>
  357. <p>
  358. Additionally, the Forth version may be easily adapted to stop looping as soon as 
  359. a key is typed: 
  360. <p>
  361. <br><code>    begin  1000 @ drop  key? until</code><br>
  362. <p>
  363. More importantly, many of today's complicated chips require fairly extensive 
  364. initialization sequences in order to configure them to the correct operating 
  365. mode.  Such code is much easier to write and debug in Forth, because you can 
  366. "try things out" by typing commands at the keyboard, the looking at the 
  367. registers to see what happened.  
  368. <p>
  369. A set of simple Forth commands sufficient to do most hardware debugging jobs can 
  370. easily be described on a single page, and many engineers and technicians have 
  371. learned enough Forth in 30 minutes to be able to write sophisticated diagnostics 
  372. for complicated hardware.  
  373. <p>
  374. <p>
  375. <h2>Stack Usage</h2>
  376. <p>
  377. A previous example has shown how to access the top element on the stack which is 
  378. stored in r10.  Things get a little more complicated if more than 1 stack 
  379. argument is needed.  Remember that the top of the stack is stored in r10, and 
  380. subsequent stack items are stored in a memory area whose address is contained in 
  381. r13.  For convenience, the assembler provides alternate names for r10 and r13, 
  382. reflecting the use of these registers for the stack.  r10 is also known as  <code><A href="_smal_BG#1e"> top </A></code> 
  383. (Top of Stack), and r13 is also known as  <code><A href="_smal_BB#19"> sp </A></code> 
  384. (Stack Pointer).  
  385. <p>
  386. The basic rules for the Forth stack are: 
  387. <p>
  388. a) Upon entry to a  <code><A href="_smal_AH#187"> code </A></code> definition 
  389. (assembly language), the top of the stack is contained in  <code><A href="_smal_BG#1e"> top </A>.</code> 
  390. The next item on the stack is in the memory location whose address is contained 
  391. in  <code><A href="_smal_BB#19"> sp </A>.</code> The item after that is in 
  392. memory at <strong>sp+4</strong> , the next at <strong>sp+8</strong> , etc.  Note 
  393. that successive stack items are 4 bytes (32-bits) apart.  
  394. <p>
  395. b) A definition may modify the stack contents, and upon exit from the definition 
  396. the new top of the stack should be in  <code><A href="_smal_BG#1e"> top </A>,</code> 
  397. and the next item should be in memory at that address contained in  <code><A href="_smal_BB#19"> sp </A>.</code> 
  398. <p>
  399. c) Assembly code should not access memory at negative offsets from  <code><A href="_smal_BB#19"> sp </A>.</code> 
  400. This restriction safeguards against problems in an interrupt-driven environment, 
  401. in case the same stack happens to be used for interrupt handlers.  
  402. <p>
  403. If items are removed from the stack by a code definition, care must be taken to 
  404. make sure the correct top of stack value is left in  <code><A href="_smal_BG#1e"> top </A>.</code> 
  405. Also remember that the Risc-OS Forthmacs assembler provides macros to assist in 
  406. managing the stack.  Here are some examples; study them carefully: 
  407. <p>
  408. <p><pre>
  409. code and        (s n1 n2 -- n3 )
  410.                 r0      sp      pop
  411.                 top     top     r0 and c;
  412. code min        (s n1 n2 -- n1|n2 )
  413.                 r0      sp      pop
  414.                 top     r0      cmp
  415.                 top     r0      gt mov c;
  416. code drop       (s n1 n2 -- n1 )
  417.                 top     sp      pop c;
  418. code dup        (s n1 -- n1 n1 )
  419.                 top     sp      push c;
  420. code 1+         (s n -- n+1 )    top 1     incr c;
  421. code @          (s a_adr -- n )
  422.                 top     top )   ldr c;
  423. \ a somewhat optimized fill
  424. code fill       (s adr cnt char -- )
  425.                 r2      top     top  8 #lsl orr
  426.                 r0 r1 top 3 sp ia!  ldm \ r0-cnt r1-adr r2-data
  427.                 r0      4 #     cmp
  428.   gt if
  429.         begin   r3      r1      3 # s and
  430.                 r0      1       ne decr
  431.                 r2      r1 byte )+ ne str
  432.         eq until
  433.                 r0      8       s decr
  434.                 r2      r2      r2  10 #lsl orr
  435.                 r3      r2      mov
  436.         begin   r2 r3 2 r1 ia!  ge stm
  437.                 r0      8       ge s decr
  438.         lt until
  439.                 r0      4       s incr
  440.                 r2      r1 )+   ge str
  441.                 r0      4       lt decr
  442.   then
  443.         begin   r0      1       s decr
  444.                 r2      r1 byte )+ ge str
  445.         lt until c;
  446. code >name      \ (s cfa -- nfa )
  447.                 top     1       decr    \ skip flag byte
  448.         begin   r0      top byte -( ldr
  449.                 r0      0 #     cmp
  450.         ne until
  451.         begin   r0      top byte -( ldr
  452.                 r0      20 #    cmp
  453.         lt until c;
  454. </pre><p>
  455. <p>
  456. </body>
  457. </html>
  458.