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

时间:2017-12-25 08:32:24

标签: io arm bubble-sort

我希望用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

1 个答案:

答案 0 :(得分:0)

现在,我可以使用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\n"

结果是:0123456789 enter image description here

相关问题