【问题标题】:How to make a sorting algorithm in Assembly code, in LC3如何在汇编代码中制作排序算法,在 LC3
【发布时间】:2017-05-02 10:28:11
【问题描述】:

我有一个简单的循环,它接受一个名称并打印该名称而不保存它。

    looptext getc         ;starts text get loop for name
                          ;since name isn't re-used, we don't have to save it
    add r1, r0, -10       ;Test for enter character
    brz finishloop1       ;if enter, cancel the text loop
    OUT                   ;If it's not enter, print out the character typed
    br looptext           ;Go back to loop
finishloop1

然后程序会要求输入由空格分隔的 ID 号。所有这些值都保存到一个数组中,每个循环都会检查新输入是“新”最低值还是“新”最高值,并将其设置到相应的寄存器中。

[出于版权原因删除代码]

在代码的最后,我需要添加一个排序算法,我只剩下一个字符数组。

我需要遍历数组的每个索引,并按照数字顺序(从小到大)重新排列字符。

【问题讨论】:

  • 如果只允许从 1 到 9 的数字,则它们的 ASCII 值是从 48+1 到 48+9 的正确顺序。因此,如果您将字符而不是数字存储到数组中,您可以对字符进行排序,并且您将获得正确的顺序。然后您可以将它们输出为字符。保存 ASCII 字符串和数值之间的所有这些转换。这也适用于多位数字,只要两个数字具有相同的字符串长度。对于不同的字符串长度,当您将额外的前导零添加到较短的字符串时,词典比较将起作用。所以做一些 1 char 排序。
  • 好的.. 是的,我们只允许 1 到 9。我可能会这样做。摆脱所有转换并将它们保留为字符。但是“数字”排序它们的组装算法是什么?如,从最小到最大?
  • 有很多 sorting algorithms... 用汇编编写它们与任何其他编程语言都非常相似,只是需要更多的指令,因为您必须明确地编写从/到内存的所有数据传输进入 CPU 寄存器和 if 块通常需要比较(看起来 LC3 正在使用加法,例如您对“输入”的测试是 add r1, r0, -10,然后有条件地跳转“当零”为真),条件跳转和代码块根据检测到的条件执行/跳过。尝试想象值会发生什么
  • 顺便说一句,汇编中的“按字符排序”意味着仍然对数字进行排序,因为'1' 是字节值 49.. 所以只需对数组中的字节值进行排序,您的值将在 49-57 范围内(并且这些值在用于某些 ASCII 输出时,将显示为字符“1”到“9”的字体字形)。所以在排序过程中你会说:“49 比 54 小吗?在是/否时做这个/那个。”而不是“1 小于 6”。
  • 谢谢@Ped7g。通过您的链接,我设法找到了已经在汇编中编写了冒泡排序算法的人。

标签: algorithm sorting assembly lc3


【解决方案1】:

非常感谢大家提供的提示和技巧。特别感谢@Ped7g 链接我的排序算法页面。我最终四处搜索,实际上在 gitub 上找到了一个已经用汇编代码编写了气泡算法的人。所以谢谢你间接给我答案。

注意:对于任何未来来这里寻找答案的人,这里是冒泡排序算法代码的链接: https://github.com/oc-cs360/s2014/blob/master/lc3/bubblesort.asm。这是大学课程讲义的一部分。

; Implementing bubble sort algorithm
;   R0  File item
;   R1  File item
;   R2  Work variable
;   R3  File pointer
;   R4  Outer loop counter
;   R5  Inner loop counter


            .ORIG   x3000

; Count the number of items to be sorted and store the value in R7

            AND     R2, R2, #0  ; Initialize R2 <- 0 (counter)
            LD      R3, FILE    ; Put file pointer into R3
COUNT       LDR     R0, R3, #0  ; Put next file item into R0
            BRZ     END_COUNT   ; Loop until file item is 0
            ADD     R3, R3, #1  ; Increment file pointer
            ADD     R2, R2, #1  ; Increment counter
            BRNZP   COUNT       ; Counter loop
END_COUNT   ADD     R4, R2, #0  ; Store total items in R4 (outer loop count)
            BRZ     SORTED      ; Empty file

; Do the bubble sort

OUTERLOOP   ADD     R4, R4, #-1 ; loop n - 1 times
            BRNZ    SORTED      ; Looping complete, exit
            ADD     R5, R4, #0  ; Initialize inner loop counter to outer
            LD      R3, FILE    ; Set file pointer to beginning of file
INNERLOOP   LDR     R0, R3, #0  ; Get item at file pointer
            LDR     R1, R3, #1  ; Get next item
            NOT     R2, R1      ; Negate ...
            ADD     R2, R2, #1  ;        ... next item
            ADD     R2, R0, R2  ; swap = item - next item
            BRNZ    SWAPPED     ; Don't swap if in order (item <= next item)
            STR     R1, R3, #0  ; Perform ...
            STR     R0, R3, #1  ;         ... swap
SWAPPED     ADD     R3, R3, #1  ; Increment file pointer
            ADD     R5, R5, #-1 ; Decrement inner loop counter
            BRP     INNERLOOP   ; End of inner loop
            BRNZP   OUTERLOOP   ; End of outer loop
SORTED      HALT

FILE        .FILL   x3500       ; File location
            .END

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-20
    • 2016-03-13
    • 1970-01-01
    • 2014-11-10
    相关资源
    最近更新 更多