2013-03-03 49 views
2

我目前在學習彙編語言。我已經能夠創建一個快速簡短的功能,將兩個數字從最小值切換到最大值。我正在用三個數字來應用相同的基礎基礎,但每次我進行比較時,都會進入無限循環。我通過使用*60來聲明此功能。我如何正確地將三個數字從最小到最大排序?另外,有沒有一種方法可以讓同一個函數執行兩個和三個數字的排序,而無需進行任何其他更改?對三個數字中的最大值進行排序

幾個彙編程序的語法略有變化。 HERE是我目前使用的教育程序集小人電腦模擬器的鏈接。

兩個數字工作交換:

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 

回答

3

這是一個冒泡排序,http://en.wikipedia.org/wiki/Bubble_sort因爲它使用的交換代碼三次。

我從來沒有學過小男人,但基於http://en.wikipedia.org/wiki/Little_man_computer,這應該工作。我沒有看到關於分支到特定行號的任何內容,但它看起來像 - 基於您的第一個工作示例 - 您已經明白了這一點,並希望能夠適當地翻譯標籤。 (手指交叉)

維基百科條目有幾個僞代碼副本,但我想從第一個「優化氣泡排序」psuedocode中從維基百科條目中展開循環,因爲我沒有在小人物中看到關於使用索引訪問內存位置,就像你需要一個數組一樣。沒有檢查數組是否按順序排列。

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 
+0

你碰巧知道我是否可以將一個函數'內這兩個循環* 60'像我的'交換two'例子這樣,如果有需要訂購,將跳轉到函數,然後順序和跳回來繼續比較第二個數字和第三個數字? – CodingWonders90 2013-03-06 04:05:35

相關問題