2

現在アセンブリ言語を勉強中です。2 つの数値を最小から最大に交換する簡単な短い関数を作成できました。3 つの数値でこれを行うために同じ基本的な基盤を適用していますが、比較を実行するたびに無限ループに入ります。を使用してこの関数を宣言してい*60ます。3 つの数値を最小から最大に正しく並べ替えるにはどうすればよいですか? また、追加の変更を加えずに、同じ関数で 2 と 3 の数字の並べ替えを実行する方法はありますか?

いくつかのアセンブリ プログラムでは、構文がわずかに変更されています。HEREは、私が現在使用している教育アセンブリーのリトルマン コンピューター シミュレーターのリンクです。

2 つの数値を使用したワーキング スワップ:

INP     //Input x number
STO 99      //Store x number
INP     //Input y number 
STO 98      //Store y number
BR 60       //Jump to function *60
HLT     //End run
*60         //Number SWAP function
LDA 99      //Load x
STO 87      //Move x to mailbox 87
LDA 98      //Load y
STO 86      //Move y to mailbox 86
SUB 87      //Subtract y - x 
BRP 71      //Branch to line 71 if result is positive
LDA 86      //SUB 87 gives a negative result- then y is smallest number. Load y
OUT     //Display y
LDA 87      //Load x- the greater number. 
OUT     //Display x
HLT     //End here since y > x
LDA 87      //BRP 71 branches here- then x is smallest number 
OUT         //Display x
LDA 86      //y is the greater number
OUT *       //display y 
4

1 に答える 1

3

これはバブル ソートです。https://en.wikipedia.org/wiki/Bubble_sortは、スワップ コードを 3 回使用するためです。

私はリトルマンを学んだことはありませんが、https://en.wikipedia.org/wiki/Little_man_computerに基づいて、これはうまくいくはずです. 特定の行番号への分岐については何も見ませんでしたが、最初の実際の例に基づいて、それを理解して、うまくいけばラベルを適切に翻訳できるようです。(成功を祈っている)

ウィキペディアのエントリには疑似コードのコピーがいくつかありますが、ウィキペディアのエントリの最初の「最適化バブル ソート」疑似コードからループを展開したかったのは、インデックスを使用して必要なメモリ位置にアクセスする方法についてリトルマンで何も見なかったからです。配列の場合。配列が正しいかどうかを確認するチェックはありません。

Load the three values into registers r91-r93

// loop 1 step 1
if r92-r91>0 then    
    do nothing
else      // swap them, using a temp register  
    temp=r92
    r92=r91
    r91=temp
end if
// loop 1 step 2
if r93-r92>0 then 
    do nothing
else      // swap them, using a temp register 
    temp=r93
    r93=r92 
    r92=temp
end if 
// loop 2 step 1
if r92-r91>0 then 
    do nothing
else      // swap them, using a temp register  
    temp=r92
    r92=r91
    r91=temp
end if

Write out the registers in order:  r91, r92, r93

ウィキペディアの記事によると、リトルマンの私の最良の近似である同じコードは次のとおりです。ラベルを修正する必要がある場合があります。

         INP        // Read in the first value
         STA 91     // store it
         INP        // Read in the second value
         STA 92     // store it
         INP        // Read in the third value
         STA 93     // store it
         LDA 92     // LOOP 1, STEP 1:  
         SUB 91     // 
         BRP STEP2  // if r91 and r92 are in order, don't swap them
         LDA 92     // Begin swapping registers
         STA 99     // temp = r92
         LDA 91
         STA 92     // r92 = r91
         LDA 99
         STA 91     // r91 = temp
STEP2    LDA 93     // LOOP 1, STEP 2
         SUB 92
         BRP STEP3  // If r92 and r93 are in order, don't swap them
         LDA 93     // Begin swapping registers
         STA 99     // temp = r93
         LDA 92
         STA 93     // r93 = r92
         LDA 99
         STA 92     // r92 = temp
STEP3    LDA 92     // LOOP 2, STEP 1
         SUB 91
         BRP STEP4  // if r91 and r92 are in order, don't swap them
         LDA 92     // Begin swapping registers
         STA 99     // temp = r92
         LDA 91
         STA 92     // r92 = r91
         LDA 99
         STO 91     // r91 = temp
STEP4    LDA 91     // Write out the sorted values 
         OUT
         LDA 92
         OUT
         LDA 93
         OUT
         HLT        // stop
于 2013-03-03T09:30:01.243 に答える