In fact, it really feels like I have to start from scratch again. X86 assembly language has been completely returned to my beloved microcomputer principle teacher.
I saw a widely circulated bubble sort on the Internet. Since I can't write it, I will learn its essence first, maybe I can use it. I
looked at the code in detail. The specific translation is as follows (I am not very familiar with the arm assembly instructions, so I will review them at that time):
AREA Sort, CODE, READONLY: First, use the AREA pseudocode plus CODE to indicate that what is followed will be a code segment (corresponding to this is the data segment DATA), ENTRY and END appear in pairs, indicating that the code between them is the main body of the program
ENTRY: indicates the beginning of the code
Start MOV r4, #0: assign 0 to r4
LDR r6, = src: When using LDR, when the format is LDR r0, = 0x022248, the second parameter represents the address, that is, 0x022248. Similarly, when the src variable represents an array, the r0 register needs to point to src, then the value needs to be assigned like this: LDR r0, = src When the format is LDR r0, [r2], the second parameter represents the register. My understanding is that the [] symbol means to fetch content, r2 itself represents a register address, and when the content is fetched, it is stored in the r0 register and jumped to LDR R0,[R1] reads the word data at memory address R1 into register R0 LDR R0,[R1,#8] reads the word data at memory address R1+8 into register R0, and writes the new address R1+8 into R1. This sentence assigns the first address of data src to R6
ADD r6,r6,#len: R6 is the address of the last array element of the array. The array is allocated continuously at this time.
Outer LDR r1,=src: assign src address, i.e. the first address of the array element, to R1
Inner LDR r2,[r1]: read the word data at memory address r1 into r2
LDR r3,[r1,#4]: read the word data at memory address R1+4 into R3
CMP r2,r3: compare R2,R3
STRGT r3,[r1]: if (signed number) is greater than, write the word data in R3 to the memory with R1 as the address
STRGT r2,[r1,#4]: if greater than, write the word data in R2 to the memory with R1+4 as the address
ADD r1,r1,#4: assign R1+4 to R1
CMP r1,r6: compare R1 with R6
BLT Inner: if the signed number is less than, i.e. R1
ADD r4,r4,#4: assign R4+4 to R4
CMP r4,#len: R4 is compared with len
SUBLE r6,r6,#4: If the signed number is less than or equal to, R6-4 is assigned to R6
BLE Outer: If the signed number is less than or equal to, jump to Outer
Stop MOV r0,#0x18, the stop section is used to exit the program. The first statement "MOV r0,#0x18" assigns r0 to 0x18. This immediate value corresponds to the macro angel_SWIreason_ReportException. It indicates the execution status stored in r1. The statement "LDR r1,=0x20026" sets the value of r1 to ADP_Stopped_ApplicationExit, which indicates that the program exits normally. Then use SWI, the statement "SWI 0x123456" ends the program and returns the control of the CPU to the debugger.
LDR r1,=0x20026
SWI 0x123456
AREA Array,DATA,READWRITE
src DCD 2,4,10,8,14,1,20: Data definition pseudo instruction DCD is used to allocate a continuous word storage unit and initialize
len with specified data EQU 7*4: 4 here means 4 bytes. That is, 32 bits. Because ARM7TDMI is a 32-bit ARM instruction set.
END: End code
The code is very clear. The idea is right. The execution result seems to be right.
But it is not right after a detailed look. The starting address of the array is 0x00008058 (the value of r6 at this time. The initial value of r6 is the starting address plus len, and it becomes the starting address after being decremented by the program). So after bubbling, 0x00008058 should store 01 00 00 00 (according to the memory content display format in ADS), but it stores 00 E8 00 E8 at this time. The result after bubble sorting is from 0x0000805c. There must be something wrong with the program. Since we want to find the problem, let's debug it step by step.
It forgot to introduce how to view the content stored in the memory. I couldn't find it before. Because when I clicked the memory box, I didn't notice that there was a start adreess. For example, the starting address of the array in this program is 0x0000805c, so we set the start address to 0x8000 by clicking increment.
Then debug it step by step.
This is the result of the first sort. Obviously wrong. Because the largest number in the array is sunk to the bottom, but the number 0x00E800E8 is also involved and bubbles up to the front of the largest number. This is wrong. That is, the inner loop control of this program is wrong. It is
actually very simple to correct. The inner loop in the program compares once. The method I use here is to define len as 6*4. It should be more appropriate to understand len as an offset. The address of the first number is 0x00008058, so the address of the last number plus the offset of 6*4 = 24 should be 0x00008070. This is the correct array address.
Previous article:ARM-2410 interrupt control LED bare metal program
Next article:The most important issues to pay attention to during ARM development
Recommended ReadingLatest update time:2024-11-15 13:56
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- How haptic technology can enhance driving safety
- Let’s talk about the “Three Musketeers” of radar in autonomous driving
- Why software-defined vehicles transform cars from tools into living spaces
- How Lucid is overtaking Tesla with smaller motors
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Sharing of key points on the use of ADI passive components
- Four design considerations for telematics hardware in connected cars
- Wide voltage input USB-C, 5V/3A output adapter reference design
- When the machine is turned off, the LDO output is normally 3V. Why does the voltage reach 3.5V after the machine is turned on? What is the reason?
- 【Smart Sports Watch】3. Build Development Environment 2
- History of the Development of Microwave Circuits
- 6657Statically configure serial port general interrupt in sys/bios
- Understanding Nginx Architecture in One Article
- Highly recommended! ADI's latest Chinese information is here
- Urgently looking for expert advice AD15 can not open the PCB after cracking