2012-11-15 9 views
1

我遇到了這個選擇排序的問題,問題是當vecnums [j]小到(<)vecnums [min],我必須把j以min爲單位,然後將j遞增1,之後當我在SI中加載j時,j不再是它必須的值(使用調試器時我發現它應該是0102,應該是0002)。我不知道爲什麼會發生,如果有人可以幫助我,我會非常感激:)選擇分類程序集x86 [納斯姆]:奇怪的增量值j

我很抱歉,如果我沒有表達自己,不要懷疑在問我任何你不明白的地方。

謝謝你的時間!

Vecnums是一個數組的數組(2字節大小),我加載它:5,-11,3,-4,10,1005,0,5,-1,23,-34,85, - 30,-82,1

i  resb  1 
j  resb  1 
min  resb  1 
     db  0 
vecnums  times 60 db 0 
nlog  resb  1 ;for the example is 15 

ssort:  
     mov  byte[i],0 
     mov  ch,0 
     mov  cl,[nlog] 
     sub  cl,1 
cicloi:  
     mov  ah,0 
     mov  al,[i] 
     mov  [min],al ; min=i 

     push  cx 
     mov  cl,[i] 
     add  cl,1 
     mov  byte[j],cl 
     mov  cl,[nlog] 
     sub  cl,[j] 
cicloj: 
     mov  si,[j] 
     imul  si,2 
     mov  ax,word[vecnums+si] 
     mov  si,[min] 
     imul  si,2 
     mov  dx,word[vecnums+si] 
     cmp  ax,dx ;ax=vecnums[j] dx=vecnums[min] 
     jnl  noMenor 
     mov  ah,0 ;vecnums[j] < vecnums[min] 
     mov  al,[j] 
     mov  [min],al ; min=j 
noMenor:          ; vecnums[j] > vecnums[min] 
     inc  byte[j] 
     loop  cicloj 
     mov  si,[j] 
     imul  si,2 
     mov  ax,word[vecnums+si] 
     mov  si,[min] 
     imul  si,2 
     mov  dx,word[vecnums+si] 
     mov  si,[j] 
     imul  si,2 
     mov  word[vecnums+si],dx 
     mov  si,[min] 
     imul  si,2 
     mov  word[vecnums+si],ax 
     inc  byte[i] 
     pop  cx 
     loop  salto 
     jmp  finrut 
salto:  jmp  cicloi ;the reason for this is that the jump is too long to do it with loop (couldn't assemble if I do it directly with loop) 
finrut: 
    ret 

回答

1

j你變量是字節大小(這同樣適用於imin)。但是,當您說​​您正在加載2個字節時,因此高字節將來自以下變量(如果是j,則爲min)。你應該修理你的負載,例如通過使用movzx si, byte [j]

附註:通常使用移位乘以2的乘方。

+0

你是對的!我忘了這一點,謝謝你的幫助(我在代碼的另一部分有這個問題,我解決了它,但在這種情況下我沒有意識到)。 我有另一個錯誤是,在這部分代碼: '環cicloj' 'MOV SI,[j]的'' SI IMUL,2' 'MOV AX,字[vecnums + SI]'' MOV的Si,[分鐘]'' SI IMUL,2' 'MOV DX,字[vecnums + SI]'' MOV SI,[j]的'' SI IMUL,2' Redithion