Bubble sort of arm assembly

Publisher:平凡的梦想Latest update time:2016-08-14 Source: eefocusKeywords:arm Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
After I started writing programs in ADS, I found that my previous learning of X86 assembly language had been completely returned to my teacher. The most direct thing was to write a bubble sort from small to large. I don't think I need to elaborate on the idea of ​​bubble sort. I must have used C language a lot before. The algorithm is not a problem, but the data structure is an array, but how should the array be defined in arm assembly? 

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.
Keywords:arm Reference address:Bubble sort of arm assembly

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

Comparison of three Form Factors of ARM architecture core boards
Internationally, in the field of ARM core boards, the three most common Form Factors are SMARC, Qseven, and Apalis, each dominated by a different manufacturer. This article only analyzes the advantages and disadvantages of the three Form Factors from the perspective of the source and scalable interface of various speci
[Microcontroller]
Comparison of three Form Factors of ARM architecture core boards
The difference between S3C2440, S3C2450 and S3C6410 in arm920t
  Samsung currently launches the S3C6400 and S3C6410, both of which are based on the ARM architecture and have compatible hardware pins. It should be said that the general functions are basically the same. The obvious difference is that the S3C6410 comes with 2D/3D hardware acceleration.   S3C2440 is actually a very
[Microcontroller]
【ARM】Microprocessor operating mode
reward The difference between SPACE and DCD is: SPACE applies for a piece of memory space (in bytes), but does not assign an initial value DCD applies for a word (32 bits) of memory space and assigns an initial value For 32-bit ARM, one word is 32 bits = 4 Byte In the code UsrStackSpace SPACE USR_STACK_LEGTH
[Microcontroller]
【ARM】Microprocessor operating mode
【ARM】The function of BL0 in S5PV210 chip
The functions of BL0 in the S5PV210 chip are as follows: (1) Turn off the watchdog; (2) Clear the instruction register; (3) Initialize the stack area; (4) Initialize the heap area; (5) Initialize the block device copy function; (6) Initialize the PLL and set the system clock; (7) Copy BL1
[Microcontroller]
Integrating analog components with Arm microcontroller cores to solve embedded system problems
The design of embedded systems presents complex challenges as there are aggressive goals for advancements in performance, cost, power, size, new features, and efficiency. However, there is an emerging design alternative that can address these complexities – the intelligent integration of analog components with ARM® mi
[Microcontroller]
Integrating analog components with Arm microcontroller cores to solve embedded system problems
ARM bare board development configuration process 1
ARM bare board is under development: Can the C language program be run as soon as the development board is powered on? Answer: No, ATPCS (arm and thumb program calling specifications) uses the resources in ARM by default, and the ARM resources have not been initialized when power is turned on! Initialize the resour
[Microcontroller]
NXP LPC ARM must know knowledge
Registers and working modes: 7 working modes: fiq/irq/abt/und/sys/usr/svc. Switching by "MSRcpsr_c,#0xdx". Entering svc mode at power-on. The difference between svc and usr is: svc can be freely switched to any other mode by "MSR cpsr_c,#0xdx", but usr cannot. Each mode has its own stack. After the program starts, y
[Microcontroller]
Design and implementation of communication interface between S3C44B0X and multi-channel simulator control board based on ARM 7
  0 Introduction   ARM (Advanced RISC Machines) is a 32-bit microprocessor that has been increasingly widely used in various fields in recent years. It is not only the name of a company, but also the general name of a class of microprocessors, including ARM7, ARM9, ARM9E, ARM10E, SecurCore and other series. Among th
[Microcontroller]
Design and implementation of communication interface between S3C44B0X and multi-channel simulator control board based on ARM 7
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号