名前を取り、名前を保存せずに出力する単純なループがあります。
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 番号を要求します。これらの値はすべて配列に保存され、各ループで、新しい入力が「新しい」最小値または「新しい」最大値であるかどうかがチェックされ、適切なレジスタに設定されます。
【著作権の都合上削除したコード】
ソート アルゴリズムを追加する必要があるコードの最後に、文字だけの配列が残ります。
配列の各インデックスを調べて、文字を数値の順序 (最小から最大) に並べ替える必要があります。
皆様、色々とコツを教えていただき本当にありがとうございました。ソート アルゴリズムのページにリンクしてくれた @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
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加