?? basic.html
字號:
<!doctype html public "-//W3C//DTD HTML 3.2//EN"><html><head><title>Writing assembler in BASIC</title><meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" /><meta http-equiv="content-language" content="en" /><meta name="resource-type" content="document"><meta name="copyright" content="This document copyright 2001 by Richard Murray. Use for non-profit and education purposes explicitly granted."><meta name="author" content="Richard Murray"><meta name="rating" content="general"></head><!-- /assembler/basic.html --><!-- --><!-- (C) Copyright 2001 Richard Murray --><!-- Designed by Richard Murray --><!-- rmurray@heyrick.co.uk --><!-- --><body bgcolor="#f0f0f0" text="#000000" link="#0022dd" vlink="#002288"><table border = "0" width="100%"> <tr> <td align=center width=100> <img src="arm3.gif" width=79 height=78 align = middle> </td> <td> <h1 align="center"><font color="#800080">Writing assembler in BASIC</font></h1> </td> <td align=center width=100> <img src="arm3.gif" width=79 height=78 align = middle> </td></table><p> <p><h2>Introduction</h2>BASIC is demeaned by many as being a rather useless language. Okay, it has the obvious limitationin that it is an interpreted language (so each line must be 'compiled' on the fly), and it hasno structure typing. Also, it is a boon for sit-and-type programmers; the sort that Pascalfanatics have nightmares about.<p> <p>But BASIC, for all its seemingly apparent limitations, is very powerful. It is an absolutedoddle to drop into assembler, and back to BASIC. You can, with cunning, intermix BASIC andassembler and even call BASIC from assembler called from BASIC. Uh, confused yet?<br>Actually, it is nothing that you can't do from C, with the right tools. But, unlike C, you don'tneed a "development environment" costing hundreds of pounds, nor one that is<i>huge</i> to download. In fact, all you need is provided right there in your computer. BASIC,in ROM, and !Edit, also in ROM.<p> <p>BASIC offers you quite a powerful assembler. It is notably missing floating point and ADRL,however Darren Salt has created a useful module that provides these facilities.<br><a href="http://www.youmustbejoking.demon.co.uk/progs.frameset.html">Follow this link to seeDarren Salt's programs, including a vastly extended debugger module.</a> <font color = "red" size = "-1">[EXTERNAL LINK]</font><p>Maybe you've asked BASIC for a little bit of help on it's assembler.<br>BASIC provides detailed help on it's commands, <code>COLOUR</code> for example...<pre>>HELP COLOURCOLOUR a [TINT t]: set text foreground colour [and tint] (background 128+a).COLOUR a,p: set palette entry for logical colour a to physical colour p.COLOUR r,g,b: set colour to r, g, b.COLOUR a,r,g,b: set palette entry for a to r, g, b physical colour.</pre>So it is a bit of a disappointment that the assembler is described correctly, but in a veryterse (and barely readable) form...<pre>>HELP [Assembly language is contained in [] and assembled at P%. Labels follow '.'.Syntax:SWI[<cond>] <expr>ADC|ADD|AND|BIC|EOR|ORR|RSB|RSC|SBC|SUB[<cond>][S] <reg>,<reg>,<shift>MOV|MVN[<cond>][S] <reg>,<shift>CMN|CMP|TEQ|TST[<cond>][S|P] <reg>,<shift>MLA[<cond>][S] <reg>,<reg>,<reg>,<reg>MUL[<cond>][S] <reg>,<reg>,<reg>LDR|STR[<cond>][B] <reg>, '[ <reg>[,<shift>] '] [,<shift>][!]LDM|STM[<cond>]DA|DB|EA|ED|FA|FD|IA|IB <reg>[!],{<reg list>}[^]B[L][<cond>] <label>OPT|=|DCB|EQUB|DCW|EQUW|DCD|EQUD|EQUS <expr>ADR[<cond>] <reg>,<label>ALIGNwhere <shift>=<reg>|#<expr>|<reg>,ASL|LSL|LSR|ASR|ROR <reg>|#<expr>|RRXand <cond>=AL|CC|CS|EQ|GE|GT|HI|HS|LE|LS|LT|LO|MI|NE|NV|PL|VC|VSand <reg>=R0 to 15 or PC or <expr></pre><p>But don't worry. This assembler area describes the ARM assembler, and this document tells youhow to get going.<br>Here, we discuss the basics. Other documents expand upon these themes.<p> <p><h2>The first step</h2>The first step is to reserve yourself a chunk of memory. All code must be assembled somewhere.You can choose to poke around in memory, or claim a chunk of RMA and assemble into that. Suchthings are regarded as <i>hacky</i>. The good way to program is to DIM a block of memory andassemble your (position independent?) code into it.<p>So to reserve ourselves some memory, we use DIM.<pre> DIM code% 4096</pre><p>You should reserve enough memory to hold your code. If you overshoot, the assembler will not stopunless you are using range checking. Without range checking, bizarre things may happen.<br>If you start getting errors that don't make sense, try doubling your memory allocation.<p>A useful trick, at the end of your assembly, is to use:<pre> PRINT P% - code%</pre>This prints the result of the current value of the <i>next instruction to be assembled</i>pointer with the <i>beginning of code memory</i> subtracted from it. The result is the amount ofspace used by your code.<br>Such a command helps you to optimise your use of memory. You don't need to claim 4096 bytes ifyour code only uses 124 bytes!<p> <p>This leads us to the <code>FOR</code>...<code>NEXT</code> loop.<br>Typically, assembler must be assembled twice. The first time, you want to whizz blindly throughand ignore all the errors. This may sound weird, but it will make a note of the labels in yourprogram, and where they are. So the second time around you can assemble and all the labelsreferred to will be known. Et voila!<p>The document <a href="opt.html">opt.html</a> describes all the available options, the values thatshould be used in your loop.<br>Typically, you will see...<ul> <li> <code>FOR loop% = 0 TO 2 STEP 2</code><br> The <i>de facto</i> way to assemble code, using two-pass assembly. <br> <br> <li> <code>FOR loop% = 0 TO 3 STEP 3</code><br> The standard two-pass assembly, but outputs a listing. Useful for debugging, but otherwise it just slows down the assembly. <br> <br> <li> <code>FOR loop% = 4 TO 6 STEP 2</code> <i>or</i> <code>FOR loop% = 4 TO 7 STEP 3</code> Offset two-pass assembly (without and with listing). The code is assembled in your memory block, to be executed at some other location. This is usually used for things like modules. Refer to <a href="exa4.html">example four</a> to see how this is used.</ul><p>We are going to use 8 and 10.<pre> FOR loop% = 8 TO 10 STEP 2</pre>This is just like the usual two-pass assembly, but it also uses range checking. It only needsone more bit of code to use, and the benefits are tremendous. You are assured that your code fitsinto the allocated space. If you should overshoot, you will receive a message such as:<pre> Assembler limit reached at line 100</pre><p>Following your <code>FOR</code> loop, you need to tell the assembler where to put your code.This is done by setting <code>P%</code>.<pre> P% = code%</pre>Do NOT forget this, the results of forgetting it can range from amusing to tragic, depending onwhat P% pointed to. It is worth noting that judicious use of P% can allow you to patch otherapplications while they are running. But I'm guessing only a geek like me would bother to try todo such a thing!<p>The next step is to set up your range check. To do this, you set <code>L%</code> to the end ofyour allocated memory.<pre> L% = code% + 4096</pre>It is useful if you set your allocation amount (the value 4096) to a constant variable, then youonly need to change the one thing.<p>With your locations set up, you can enter the assembler. This is done using the left squarebracket.<pre> [ OPT loop%</pre>Following your square bracket, you need to set the OPTion, as shown.<br><code>OPT</code> is not an opcode understood by the processor. It is simply a pseudo-opcodedesigned to tell the assembler how to behave. It is not provided in APCS assemblers.<p> <p>Okay.<br>You are in the assembler. Set up, ready to roll.<br>Let's stick in some code.<pre> ; example code ADR R0, message SWI "OS_PrettyPrint" MOV PC, R14 .message EQUS "Wheeee! Isn't this fun?" EQUB 13 EQUB 0 ALIGN</pre><p>Finally, we need to close our assembler code and end the loop.<pre> ] NEXT</pre><p> <p>What comes next depends upon what you plan to do with the code. You can CALL it, USR it, save itto disc, or simply leave it to CALL/USR it at a later time in your program.<br>Here, we shall CALL it.<p> <p> <p>Your completed program should look like this.<pre> DIM code% 40 FOR loop% = 8 TO 10 STEP 2 P% = code% L% = code% + 40 [ OPT loop% ; example code ADR R0, message SWI "OS_PrettyPrint" MOV PC, R14 .message EQUS "Wheeee! Isn't this fun?" EQUB 13 EQUB 0 ALIGN ] NEXT IF INKEY(-1) THEN PRINT P%-code% CALL code%</pre>Immediately you should see two changes. The most obvious is the addition of the INKEY line. Thiswill report the size of the code if you hold down Shift when you run the program. Because of thisI was able to reduce the memory required to 40 bytes instead of four thousand.<p> <p>Let's look at some other things.<br>Did you notice the comment? You can use <code>;</code> or <code>\</code> to start comments,though it is convention to use the semi-colon. Anything following the semi-colon is a comment,until a colon is reached.<br><i>Note - this is different to BASIC</i><p>The following will not work:<pre> ; this is a comment :-)</pre>Because the following will work:<pre> MOV R0, R1, ASL #4 ; load R1<<4 into R0, now store it : STR R0, [R2]</pre>That example isn't good code, you shouldn't have code following comments unless it is VERY clearwhat is going on. But it will work. The assembler treats the colon like BASIC treats a newline,and it'll keep on assembling.<p> <p>Also notice the ALIGN. The string is 25 bytes ( <i>Wheeee! Isn't this fun?</i> plus newline plusterminator). You cannot follow this with anything in a word-aligned system until it isword-aligned. The ALIGN command will skip forward until P% is word-aligned. If you do not takecare to word-align your strings, you will need to use ALIGN after them. It is recommended to useALIGN anyway instead of lots of EQUB 0's because hardwiring the string lengths removesflexibility. But, it's your preference. I used to hardwire as the ALIGN built into BASIC does notnull the padding bytes, making your executable untidy. Such things bother me, but hey, I'vealready said I'm a geek!<p> <p>Well. It might not be much, but it is your first simple assembler program. There is a hell of alot more that you can do - <i>ChangeFSI</i> is testament to BASIC and assembler working together.This is only a start.<p> <p> <p><h2>Variations on a theme</h2>If you have no forward references, you can dispose of the FOR loop.<p><pre>DIM code% 16P% = code%[ OPT 2 MOV R0, #0 CMP PC, PC MOVEQ R0, #&FF MOV PC, R14]PRINT USR(code%)</pre>This snippet of code is a processor-independent way of determining if the system is in 26bit modeor 32bit mode. <a href="32bit.html">Read this if you are interested in the difference.</a><br>In 26bit mode, when R15 is the first operand, only the PC (<a href="regs.html">ProgramCounter</a>) part is available. The PSR (<a href="psr.html">Processor Status Register</a>) isstripped.<br>As the second operand, all 32bits are available. Thus, on a 26bit system the comparison fails.<br>In 32bit mode, R15 is the PC and only the PC. There is no PSR. This, the comparison passes andR0 is updated to be 255.<p> <p><hr size = 3><a href="index.html#04">Return to assembler index</a><hr size = 3><address>Copyright © 2001 Richard Murray</address></body></html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -