通过ARM程序集实现冒泡排序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通过ARM程序集实现冒泡排序相关的知识,希望对你有一定的参考价值。

我希望用ARM来编译我的树莓派终端的功能:输入一些Numbers,然后在程序中进行冒泡排序,并输出排序后的结果。编译并链接没问题,但执行目标文件时屏幕显示“非法指令”(ARM GNU样式)

 .globl _start
 _start:
     mov r4,#0
     ldr r6,=src
     add r6,r6,#len
outer:
     ldr r1,=src

inner:
     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

stop:
    mov r0,#0x18
    ldr r1,=0x20026
    swi 0x123456


.section .data
src:
.long 2,4,10,8,14,1,20

.equ len, 4
答案

现在,我可以使用ARM汇编程序在没有输入字符的Raspberry饼上实现冒泡排序,代码如下:

.section .text
.global _start

_start: 
    mov r2,#10  @The number of characters output
    mov r4,r2   
    b loop      @Enter the first cycle
loop:
    mov r1,pc   @Point to the first position of the string
    ldr r1,=str 
    sub r4,r4,#1
    cmp r4,#0   @R4 is compared to 0, and if r4 is equal to 0, 
                @end up,print result 
                @greater than it goes into the second cycle
    beq stop    
    mov r5,#0   
    b loop1     @The entrance to the second cycle
    b loop


loop1:

    ldrb r3,[r1]    @R1 is pointing to the memory address of the value of 
                    @the assignment to the r3 register
    ldrb r6,[r1,#1] 
    cmp r3,r6       @r3 r6 compared
    strgtb r3,[r1,#1]   @If greater than r6, their values are exchanged
    strgtb r6,[r1]
    add r1,r1,#1        @R1 points to the next character
    add r5,r5,#1        
    cmp r5,r4       @r5 r4 compared
    bne loop1       @r5<r4 next loop1
    b loop          @r5=r4,Jump out of the second cycle and return to the 
                    @first cycle
stop:
    @printf str
    add r2,r2,#1
    mov r0,#0x1
    mov r1,pc
    ldr r1,=str
    mov r7,#0x4
    svc 1

    @exit
    mov r0,#0x0
    mov r7,#0x1
    svc 1
@Define data segment
.data
str:
.ascii "7543216890
"

其结果是:0123456789enter image description here

以上是关于通过ARM程序集实现冒泡排序的主要内容,如果未能解决你的问题,请参考以下文章

冒泡排序及其复杂度

排序算法之冒泡选择插入排序(Java)

排序算法之冒泡选择插入排序(Java)

排序算法的程序实现——冒泡排序复习

冒泡排序算法原理和代码实现,就是这么简单。。。

冒泡排序史上最经典的实现