University of Electronic Science and Technology of China Microprocessor and Embedded Experiment 1

ARM Basic Programming Experiment

Table of contents

ARM Basic Programming Experiment

1. Purpose of the experiment

2. Experimental content

3. Experimental steps

4. Experimental results

5. Experimental summary

6. Experimental thinking questions


1. Purpose of the experiment

1. Familiar with and master the common ARM assembly instructions 2. Familiar with and master the "C + assembly" mixed programming technology 3. Familiar with the ARM software development and debugging tool Keil

2. Experimental content

1. Learn to use Keil development tools 2. Realize the cumulative operation function

3. Realize the string copy function 4. Realize the sum operation function

5. Implement the bubble sort algorithm

3. Experimental steps

Experiment 1.1: Run Keil, create a project file, run and debug the sample program in a single step, deeply understand each instruction, and observe the changes of registers and memory space.

Here are six example experiments:

Among them, the ARM data processing instruction addressing mode experiment and the ARM memory access instruction addressing mode experiment are compulsory experiments.

ARM stack pointer register experiment, ARM program counter experiment, ARM program status register experiment, and ARM working mode switching experiment are optional parts, and you are encouraged to use the remaining time in class or complete this part after class.

The specific steps of using Keil are as follows:

1. Click "Keil uVision5" to open the main window of the software.

2. Click Project->New uVision Project to create a new project.

3. Name the new project.

4. Select the target device for the project: Samsung S3C2440A.

5. Choose whether to add the S3C2440.s startup file: select "Yes" for C source program projects, and "No" for assembly source program projects.

6. Right-click on the project Source Group and select Add New Project File.

7. Select the source file type.

8. Enter the source code and save it.

9. Build or rebuild the project.

10. Click the debug button to enter/exit debugging, ignoring the code size limit. Use single-step debugging to carefully watch key register values ​​change during the process.

Experiment 1.2: Use ARM assembly to realize the accumulation operation of 1+2+...+N

1. Add the sum.s file in the Experiment 1.2 folder to the project.

2. In the file, use assembly to implement the core part of the algorithm, and the reference flow chart of the code to be added is as follows:

3. Run Debug to debug.

Experiment 1.3: Understand C and assembly, and use the assembler to copy strings, and call the assembler in the C program.

1. Add the testfile.s and main.c files in the experiment 1.3 folder to the project.

2. Add two lines of assembly code in the assembly file testfile.s to achieve:

1) Copy a byte of the source string to R2;

2) Copy the copied bytes to the target space.

3. Run Debug to debug.

Experiment 1.4: Calling a C function in assembly.

1. Add the sum.c and testfile.s files in the Experiment 1.4 folder to the project.

2. Add the assembly code at the corresponding position in the assembly file testfile.s, and realize 1+2+3+glovb1 by calling the c function g(), and the result is stored in R8.

3. Run Debug to debug and observe the experimental phenomenon.

Experiment 1.5: Implement the bubble algorithm with ARM assembly. (optional)

1. Add the maopao.s file in the experiment 1.5 folder to the project.

2. Add assembly code at the corresponding position in the assembly file maopao.s to realize bubble sorting.

3. Run Debug to debug.

4. On the debug interface, click Debug →Memory Map to modify the address segmentation properties.

5. Observe the experimental phenomenon

4. Experimental results

1.1 Experiment 1

ARM data processing instruction addressing mode

ARM memory access instruction addressing mode experiment

ARM stack pointer register experiment

ARM Program Counter Experiment

ARM program status register experiment

ARM working mode switching experiment

1.2 The key code of the experimental part:

ADD R0,R1

BVS SUM_END

CMP R1,R2

BHS SUM_END

ADD R1,#1

B SUN_L1

1.2 Experimental results:

It can be seen that the value in R0 and R8 is 13BA, which is in line with the experimental results of 1+2+...+100

 

1.3 The key code of the experimental part:

LDRB R2,[R1],#1    ;Copy a byte of the source string

STRB R2,[R0],#1 ;Copy the copied bytes to the target space

CMP   R2,#0

BNE strcopy

MOV   PC,LR

;------------------------------------------

END          ; end of file

1.3 Experimental results:

The string variable is copied from 'bbbb' to 'aaaa'

 

1.4 The key code of the experimental part:

MOV R0,#1;

MOV R1,#2;

MOV R2,#3;

MOV R3,#10;

BL g

MOV R8,R0;

1.4 Experimental results:

可以看到R0中的值再相加之后变为了0x10,结果为16,符合实验的预期。

1.5冒泡排序实验部分关键代码:

LDR R2,[R1]

 LDR R3,[R1,#4]

 CMP R2,R3

 STRGT R3,[R1]

 STRGT R2,[R1,#4]

 ADD R1,R1,#4

 CMP R1,R6

 BLT inner

 ADD R4,R4,#4

 CMP R4,#len

 SUBLE R6,R6,#4

 BLE outer

1.5实验结果:

 

 

五、实验总结

1.此次实验用汇编和C语言实现了1~N的累加、字符串的拷贝、数字求和以及冒泡法排序这几项功能,了解了汇编语言的基础指令及其意义,同时初步入门了Keil编译器的使用与程序编写调试。

2.知道了C程序与汇编语言相互调用的规则:

a)寄存器的使用规则:“子程序间”通过寄存器R0~R3来传递参数。在“子程序中”,使用寄存器R4~R11来保存局部变量。

b)寄存器R12用于子程序间scratch寄存器(用于保存SP,在函数返回时使用该寄存器出桟),记作IP。

c)寄存器R13用于数据栈指针,记作SP。寄存器SP在进入子程序时的值和退出子程序时的值必须相等。

d)寄存器R14称为链接寄存器,记作LR。它用于保存子程序的返回地址。

e)寄存器R15是程序计数器,记作PC。

六、实验思考题

1、ADD替换成ADDS ,SUB替换成SUBS有什么影响?替换后,运算结果会影响标志寄存器。因为ADD、SUB不带进位与借位,ADDS、SUBS带有进位与借位,运算完成要置符号位。

2、MOV替换成MOVNE有什么影响?替换后只有在上一步计较结果为不相等的时候才会执行该指令。

3、STMIA换成STMIB ,STMIA换成STMDA有什么区别?换成STMIB是将每次传送后的地址+4变成每次传送前地址+4,换成STMDA是将每次传送后的地址+4变成每次传送后地址-4。

4、思考用ARM汇编实现1+3+5+….+(2n+1)或者2+4+6+…..+2n。实现前者,将ADD R1,#1改为ADD R1,#2,并设置R1初始值为1。

实现后者,将ADD R1,#1改为ADD R2,#2,设置R1初始值为2。

5、实验3中如果去除汇编代码中的“EXPORT strcopy”会有什么现象,为什么?若去除汇编代码中的‘EXPORT strcopy’,则在C语言里面无法调用该函数,因为EXPORT用于声明strcopy为全局标号。

6、实验4中如果去除汇编代码中的“IMPORT …..” 会有什么现象,为什么?   

会无法调用C语言的main函数,因为该伪指令用于通知编译器要使用的标号或者变量在其他的源文件定义。

Guess you like

Origin blog.csdn.net/weixin_53284122/article/details/129261709