Graded ARM Assembly Language Examples - AlanClements

4m ago
20 Views
0 Downloads
1.40 MB
24 Pages
Transcription

Graded ARM assembly language ExamplesThese examples have been created to help students with the basics of Keil’s ARM development system. I am providinga series of examples that demonstrate the ARM’s instruction set.These begin with very basic examples of addition. If any reader has difficulties with this material or can suggestimprovements or corrections, please email me at [email protected] and I will do my best to make theappropriate changes.In most examples, I present the problem in words, the basic code, and then the assembly language version. I also showthe output of the simulator at various stages during the simulation. When writing assembly language I use bold font toindicate the destination operand (this will be in the code fragments as the assembler does not support this).Quick Guide to Using the Keil ARM Simulator1.2.3.4.5.6.7.8.Run the IDE package. I am using µVision V4.22.22.0Click Project, select New Microvision Project Note that bold blue font indicates your input to thecomputer and bold blue indicates the computer’s response (or option).Enter filename in the File name box. Say, MyFirstExampleClick on Save.This causes a box labelled Select Device for Target ‘Target 1’ to pop up. You now have to say whichprocessor family and which version you are going to use.From the list of devices select ARM and then from the new list select ARM7 (Big Endian)Click on OK. The box disappears. You are returned to the main µVision window.We need to enter the source program. Click File. Select New and click it. This brings up an edit boxlabelled Text1. We can now enter a simple program. I suggest:S9.10.11.12.13.14.15.16.17.AREA MyFirstExample, CODE, READONLYENTRYMOV r0,#4;load 4 into r0MOV r1,#5;load 5 into r1ADD r2,r0,e1;add r0 to r1 and put the result in r2BS;force infinite loop by branching to this lineEND;end of programWhen you’ve entered the program select File then Save from the menu. This prompts you for a Filename. Use MyFirstExample.s The suffix .s indicates source code.This returns you to the window now called MyFirstExample and with the code set out using ARM’sconventions to highlight code, numbers, and comments.We now have to set up the environment. Click Project in the main menu. From the pulldown list selectManage. That will give you a new list. Select Components, Environment,Books.You now get a form with three windows. Below the right hand window, select Add Files.This gives you the normal Windows file view. Click on the File of type expansion arrow and selectAsm Source file (*.s*; *.src; *.a*). You should now see your own file MyFirstExample.s appear inthe window. Select this and click the Add tab. This adds your source file to the project. Then clickClose. You will now see your file in the rightmost window. Click OK to exit.That’s it. You are ready to assemble the file.Select Project from the top line and then click on Built target.In the bottom window labelled Build Output you will see the result of the assembly process.You should see something like:Build target 'Target 1'assembling MyFirstExample.s.linking.Program Size: Code 16 RO-data 0 RW-data 0 ZI-data 0"MyFirstExample.axf" - 0 Error(s), 0 Warning(s).18. The magic phrase is “0 Error(s)”. If you don’t get this you have to re-edit the source file. And then goto Project and Build target again.Alan Clements ARM simulator notesPage 1

Example 1 ADDITIONThe problem: P Q R SLet Q 2, R 4, S 5. Assume that r1 Q, r2 R, r3 S. The result Q will go in r0.The CodeADD r0,r1,r2ADD r0,r0,r3The programAREAADDADDStopBEND;add Q to R and put in P;add S to P and put the result in PExample1, CODE, READONLYr0,r1,r2r0,r3StopNotes:1.The semicolon indicates a user-supplied comment. Anything following a semicolon on the same line isignored by the assembler.2. The first line is AREA Example1, CODE, READONLY is an assembler directive and is required to set upthe program. It is a feature of the development system and not the ARM assembly language. An assemblerfrom a different company may have a different way of defining the start of a program. In this case, AREArefers to the segment of code, Example1 is the name we’ve given it, CODE indicates executable code ratherthan data, and READONLY state that it cannot be modified at run time.3. Anything starting in column 1 (in this case Stop) is a label that can be used to refer to that line.4. The instruction Stop B Stop means ‘Branch to the line labelled Stop’ and is used to create an infiniteloop. This is a convenient way of ending programs in simple examples like these.5. The last line END is an assemble directive that tells the assembler there is not more code to follow. It ends theprogram.Figure Example 1.1 shows the screed after the program has been loaded and the simulation mode entered. You canconfigure this window. We have included the disassembly window that shows the code in memory and converts it intoinstructions. This feature can be confusing because it will take data and try and convert it into instructions. However,the feature is useful because it shows the actual ARM instructions that are created by your instructions. We will see thatsome instructions that you write are pseudo instructions that are translated into appropriate ARM instructions.Alan Clements ARM simulator notesPage 2

Figure Example 1.1 The state of the system after loading the code for Example 1Because there is no means of entering initial data into registers, you have to do that manually. Simply double-click aregister and then modify its value.Alan Clements ARM simulator notesPage 3

Figure Example 1.2 The state of the system after funning the code.Note that the contents of r0 are 2 4 5 11 0x0B. This is the result we expected.Alan Clements ARM simulator notesPage 4

Running a Program in the SimulatorHaving loaded a program as code and assembled it, you need to run it. You can either continue from where youleft off after assembling the code with Build target, or you can start afresh and load the code.If you load the ARM simulator, it will open in the same state you closed it (i.e., the project and source fileloaded). If the project is not open, select the Project tag, and then select Open Project. from the pull downwindow. If you are not in the correct directory, select the appropriate directory in the normal way. Then click onMyFirstExample.uvproj which is the name of the project we set up, and then click on the Open tab. This loadsyour project and you are ready to go.To run the code select Debug from the top menu. From the pull down menu select Start/Stop Debug Session.This brings up a message telling you that you are in the EVALUATION MODE and you click OK to dismissit. You should see a screen like the one below. You can operate on it exactly like any other Windows applicationand use the View tab to open other Windows (such as display memory).Now you can execute code. We are interested in the instruction-by-instruction mode that lets you execute aninstruction at a time. If you click on the step-in button you will execute a single instruction.This is the step-in button.You will be able to see any changes in the registers on the left. You will also be able to see the value of theprogram counter, PC, and the status register, CPSR. Note that function key F11 performs the same operation.When you’ve finished, you must click on the Start/Stop Debug Session item under the Project menu. Thisreturns you to the source code which you can change as necessary. After you’d changed it you must use theBuild target command again to perform a re-assembly.Alan Clements ARM simulator notesPage 5

Example 2 ADDITIONThis problem is the same as Example 1. P Q R SOnce again, let Q 2, R 4, S 5 and assume r1 Q, r2 R, r3 S. In this case, we will put the data in memory inthe form of constants before the program runs.The ;load Q into;load R into;load S into;Add Q to R;Add S to (Qr1r2r3 R)Here we use the instruction MOV that copies a value into a register. The value may be the contents of another register ora literal. The literal is denoted by the # symbol. We can write, for example, MOV r7,r0, MOV r1,#25 orMOV r5,#TimeWe have used symbolic names Q, R and S. We have to relate these names to actual values. We do this with the EQU(equate) assembler directive; for example,QEQU2Relates the name Q to the value 5. If the programmer uses Q in an expression, it is exactly the same as writing 2. Thepurpose of using Q rather than 2 is to make the program more readable.The programStopQRSAREAMOVMOVMOVADDADDBExample2, CODE, READONLYr1,#Q;load r1 with the constant ate the symbolic name Q to the value 2;;Figure Example 2.1 shows the state of the system after the code has been loaded. If you look at the disassemblywindow, you will see that the constants have been replaced by their actual values.Figure Example 2.2 shows the situation after the code has been executed.Alan Clements ARM simulator notesPage 6

Figure Example 2.1 The state of the system after loading the code.Figure Example 2.2 The state of the system after running the code.Alan Clements ARM simulator notesPage 7

Example 3 ADDITIONThe problem once again is P Q R S. As before, Q 2, R 4, S 5 and we assume that r1 Q, r2 R, r3 S.In this case, we will put the data in memory as constants before the program runs. We first use the load register,LDR r1,Q instruction to load register r1 with the contents of memory location Q. This instruction does not exist and isnot part of the ARM’s instruction set. However, the ARM assembler automatically changes it into an actual instruction.We call LDR r1,Q a pseudoinstruction because it behaves like a real instruction. It is indented to make the life of aprogrammer happier by providing a shortcut.The r0,Q;load r1 with;load r2 with;load r3 with;add Q to R;add in S;store pCODE, READWRITE;load r1 with Q;load r2 with R;load r3 with S;add Q to R;add in S;store result in Qin QThe programStopPQRSAREALDRLDRLDRADDADDSTRBAREA Example3, CODE, READWRITESPACE 4;save one word of storageDCD2;create variable Q with initial value 2DCD4;create variable R with initial value 4DCD5;create variable S with initial value 5ENDNote how we have to create a data area at the end of the program. We have reserved spaces for P, Q, R, and S. We usethe SPACE directive for S to reserve 4 bytes of memory space for the variable S. After that we reserve space for Q, R,and S. In each case we use a DCD assembler directive to reserve a word location (4 bytes) and to initialize it. Forexample,QDCD2;create variable Q with initial value 2means ‘call the current line Q and store the word 0x00000002 at that location.Figure Example 3.1 shows the state of the program after it has been loaded. In this case we’ve used the view memorycommand to show the memory space. We have highlighted the three constants that have been pre-loaded into memory.Take a look at the disassembled code. The pseudoinstruction LDR r1,Q was actually translated into the real ARMinstruction LDR r1,[PC,#0x0018]. This is still a load instruction but the addressing mode is register indirect. Inthis case, the address is the contents of the program counter, PC, plus the hexadecimal offset 0x18. Note also that theprogram counter is always 8 bytes beyond the address of the current instruction. This is a feature of the ARM’spipeline.Consequently, the address of the operand is [PC] 0x18 8 0 18 8 0x20.If you look at the memory display area you will find that the contents of 0x20 are indeed 0x00000002.Alan Clements ARM simulator notesPage 8

Figure Example 3.1 The state of the system after loading the programThe code generated bythe pseudoinstructionLDR r1,Q.The address of the first data element onthis line is 0x0000001C. The firstelement of the next word (i.e., the fifthbyte in the sequence) is at address0x0000001C 4 0x00000020.These are the three data values we’vestored in memory at locations0x000000200x000000240x00000028These locations are chosen by theassembler automatically.Alan Clements ARM simulator notesPage 9

Figure Example 3.2 The state of the system after loading the programAlan Clements ARM simulator notesPage 10

Figure Example 3.3 The state of the system after loading the programAfter executing the program the sum ofQ, R, and S has been stored in location Pin memory.Alan Clements ARM simulator notesPage 11

Example 4 ADDITIONThe problemP Q R S where Q 2, R 4, S 5. In this case we are going to use register indirect addressing to access thevariables. That is, we have to set up a pointer to the variables and access them via this pointer.The 4,#R]r3,[r4,#S]r0,r1,r2r0,r0,r3r0,[r4,#P];r4 points to the data area;load Q into r1;load R into r2;load S into r3;add Q and R;add S to the total;save the result in memoryThe programExample4, CODE, 0,r1,r2r0,r0,r3r0,[r4,#P]Stop;r4 points to the data area;load Q into r1;load R into r2;load S into r3;add Q and R;add S to the total;save the result in orPQRSAREA Example4, CODE, READWRITETheData SPACE 4;save one word of storage for PDCD2;create variable Q with initial value 2DCD4;create variable R with initial value 4DCD5;create variable S with initial value 5ENDFigure Example 4.1 shows the state of the system after the program has been loaded.I have to admit, that I would not write this code as it is presented. It is far too verbose. However, it does illustrateseveral concepts.First, the instruction ADR r4,TheData loads the address of the data region (that we have labelled TheData intoregister r4. That is, r4 is pointing at the d