【问题标题】:Assembly language shell sort?汇编语言shell排序?
【发布时间】:2015-11-17 03:13:18
【问题描述】:

我一直在做一个 ARM 汇编语言程序,现在它已经接近尾声了。 但是,需要将C语言的shell排序程序转换为我的代码,以便完成最后一部分,但是我不太了解这部分。所以我想在这里发布我的代码,并请你们提供一些关于 ARM 汇编语言

中的 shell 排序的指导

也感谢任何对我的代码的建议。

    AREA ShellSort, CODE, READONLY

destinationArray EQU 0x40000000

    ENTRY

    LDR    r1,=sourceArray
    LDR    r0,=destinationArray
    MOV     r2, #0              ; pointer to the original
    MOV     r3, #0              ; pointer to the destination array
    MOV     r4, #19             ; loop counter (20 elemenets so 0-19 counts as 18)
    MOV     r5, #1              ; size of copyArray

    LDR     r3, [r1], #1        ; copy element
    STR     r3, [r0]    

    BL     copyArray
    LDR    r0,=sourceArray
    MOV    r1,#arraySize
    BL     shellSort

stop    B         stop


val
    SUB    r3,#1              ;Address of source array passed in R0
    LDR    r3, [r0], #1       ;Address of destination array passed in R1
    LDR    r0, =destinationArray                      ;Number of array elements pass



copyArray

    STR r3, [r0]
    ADD     r5, #1
    MOV     r6, r5
    CMP     r4, #0
    BNE     val
    MOV PC,LR    ;Return to calling subroutine      

shellSort

    MOV PC,LR  ;Return to calling subroutine


arraySize       EQU 20
sourceArray     DCD -9,23,-100,675,2,98,13,-4,1000,23,5,234,45,67,12,-2,54,2,17,99

    END                                                                 

【问题讨论】:

  • 理解算法,在ASM中实现,简单。你真正想知道什么?你被困在哪里了?
  • 我的意思是 shell 排序似乎需要嵌套循环,对吧?我看到了一个while循环和一个for循环来实现,这是我遇到的问题。
  • @enhzflep 排序算法中的标志也是我不知道如何实际转换为汇编语言的另一部分......
  • ShellSort 我帮不了你,我从来没有费心去实现它。当您说您不知道如何将其转换为汇编语言时,我不太确定该怎么做。你需要一个理由来做这件事——这似乎要么是家庭作业,要么是分配给没有能力执行它的人的错误任务。如果您只想要一个汇编列表而不关心理解,如果您用 C 编写函数并使用 -S 选项编译,您可以从 GCC 获得输出,这将为您留下汇编列表。做不到这一点,你需要努力学习
  • 由于 shell 排序真的很像一些古老的东西所以我真的无法处理它所以这就是我寻求帮助的原因。但是感谢您的提示,我得到的 GCC 东西完全是一团糟。但肯定会从那里尝试。

标签: sorting assembly arm keil


【解决方案1】:

我的环境和你的不一样。我已经重做你的代码以适应我的测试系统。我已经搜索了一个好的手臂组装排序,但没有找到任何。冒泡排序适用于短排序。我发现'Linux qsort' 非常好,并已将其插入您的程序中。我的更改是小写的,您可以重新调整它以适应您的环境。

.data

.equ arraySize, 20

sourceArray:
    .word   -9,23,-100,675,2,98,13,-4,1000,23,5,234,45,67,12,-2,54,2,17,99

.equ numbytes, (arraySize * 4)
destinationArray:       @ Memory is 1 byte and an integer is 4 bytes
    .skip  numbytes     @   (1 word) on my Raspbian Jessie RPi3,
                        @   adjust for your environment.
.balign 4
format:
    .asciz " %4d  %4d  %4d  %4d  %4d  %4d  %4d  %4d  %4d  %4d\n"

.balign 4
fmt:
    .asciz "\n"

.text

.global main
main:
@   ENTRY

    push    {r4-r10, lr}        @ save registers and return value
    LDR     r1, =sourceArray
    LDR     r0, =destinationArray
    MOV     r2, #0              @ pointer to the original
    MOV     r3, #0              @ pointer to the destination array
    MOV     r4, #19             @ loop counter (20 elemenets so 0-19 counts as 18)
    MOV     r5, #1              @ size of copyArray

@   LDR     r3, [r1], #1        @ copy element
@   STR     r3, [r0]    

    BL     copyArray
    LDR    r0, =sourceArray
@   MOV    r1, #arraySize
    ldr    r1, =arraySize
    BL     shellSort

stop:
@   B      stop
    mov    r0,  #0
    pop    {r4-r10, lr}        @ restore registers and return to caller
    bx     lr


val:
    SUB    r3,#1              @Address of source array passed in R0
    LDR    r3, [r0], #1       @Address of destination array passed in R1
    LDR    r0, =destinationArray          @Number of array elements pass



copyArray:
    ldr     r3, [r1], #4      @ index source, int takes 4 bytes
@   STR     r3, [r0]
    str     r3, [r0], #4      @ index destination
    ADD     r5, #1
    MOV     r6, r5
    subs    r4, #1            @ counter--
    bgt     copyArray         @ continue copying array
@   CMP     r4, #0
@   BNE     val
    MOV     PC, LR    @Return to calling subroutine      

shellSort:

@   MOV     PC,LR  @Return to calling subroutine


@ arraySize       EQU 20
@ sourceArray     DCD -9,23,-100,675,2,98,13,-4,1000,23,5,234,45,67,12,-2,54,2,17,99

@   END                                                                 

@ ---- All below this line is added for qsort (not shellsort) ----

@ http://www.tutorialspoint.com/c_standard_library/c_function_qsort.htm
@
@ http://man7.org/linux/man-pages/man3/qsort.3.html
@
@ void qsort(void *base, size_t nmemb, size_t size,
@            int (*compar)(const void *, const void *));
@
@      qsort(r0 =array, r1 =numelements, r2 = 4 bytes,
@            r0=(r3 =cmpfunc){ r0=[r0] - r1=[r1] })

qsort_setup:
    push  {r0-r12, lr}            @ save environment
    ldr   r0,  =destinationArray  @ sorting destinationArray
    ldr   r1,  =arraySize         @ size of array elements
    mov   r2,  #4                 @ size of integer
    ldr   r3,  =cmpfunc           @ compare function for int
    bl    qsort                   @ Linux OS build in sort 

    b     print_sourceArray

cmpfunc:                          @ This work per specs above
    ldr   r0,  [r0]
    ldr   r1,  [r1]
    sub   r0,  r0, r1
    mov   pc,  lr

print_sourceArray:
    ldr   r0,  =fmt
    bl    printf
    ldr   r0,  =sourceArray
    bl    prt_half_array
    ldr   r0,  =sourceArray
    ldr   r1,  =numbytes
    add   r0,  r1, lsr #1
    bl    prt_half_array


print_destinationArray:
    ldr   r0,  =fmt
    bl    printf
    ldr   r0,  =destinationArray
    bl    prt_half_array
    ldr   r0,  =destinationArray
    ldr   r1,  =numbytes
    add   r0,  r1, lsr #1
    bl    prt_half_array
    ldr   r0,  =fmt
    bl    printf

    pop   {r0-r12, lr}            @ restore enviroment
    mov   pc, lr                  @ return

prt_half_array:
    push  {r0, lr}
    ldm   r0,  {r1-r10}
    ldr   r0,  =format
    push  {r4-r10}
    bl    printf
    add   sp,  #28
    pop   {r0, pc}
.end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 2021-03-16
    • 2015-08-06
    • 2016-09-27
    • 1970-01-01
    • 2010-12-24
    相关资源
    最近更新 更多