2014-12-23 91 views
0

我試過在互聯網上閱讀這些內容,但這裏是我的問題。我給了一串雙字。x86 Asm插入排序

我在遞減順序從這些雙字的低字(至少顯著)將字符串訂購。高級詞彙保持不變。 對於例如:DY 12345678h 1256ABCDh,12AB4344h ,結果將是1234ABCDh,12565678h,12AB4344h。

現在我盡我所能寫了一些代碼,但它不能正常工作,我的插入過程。如果你可以看一看,並告訴我我做錯了什麼,我會很棒。 我試着在TD模式下運行它,但我無法弄清楚。

assume cs:code, ds:data 
data segment 
    s dd 12345678h, 1256ABCDh, 12AB4344h 
    ls equ ($-s)/4 ;this is supposed to be the length of my source string 
    d dd ls dup (?) ;this is my destination string 
    aux dw ? 
    aux2 dw ? 
data ends 

code segment 
insert proc 
    push di ;here I use the stack to get more free registers 
    push cx 
    cmp di, offset d ;if di=offset d it means that I didn't store any number yet 
    je addPrim 
    std ;we plan on working form right to left on the string for the next part 
    mov cx, di 
    sub cx, offset d ;here I find out with how many words I have to compare the word from AX 
    dec di 
    dec di ;since I work with doublewords, for some reason I thought I should decrease di 
    dec di ;3 times but here my procedure gets fuzzy and doesn't work properly anymore 
    repeta1: ;this repeat is supposed to compare the word from AX with the rest of the least 
     scasw ;significant words from es:di 
     jge DIplus2 ;if my number from AX is bigger or equal than what's in es:di, I increment 
;di twice and store it 
     mov bx, word ptr es:[di+1] ;this part is supposed to interchange words but it's not 
;working how I planned so I don't know how to change it 
     mov word ptr es:[di+2], bx 
     loop repeta1 

    jmp DIplus1 

    DIplus2: 
     inc di 

    DIplus1: 
     inc di 

    addPrim: ;this label just adds the first word in the destination string 
     stosw 

    pop cx 
    pop di 
    inc di 
    inc di 
    cld 
    ret 
    insert endp 

start: 
    mov ax, data 
    mov ds, ax 
    mov es, ax 
    mov si, offset s 
    mov di, offset d 
    mov cx, ls ; store in cx the length of the strings 
    jcxz exit 

repeta: 
    lodsw ;because of little endian, my first word will be my least significant word in the 
;in the doubleword so right after it is moved in ax, i apply the procedure insert 
    call insert 
    lodsw ;here it moves in ax my most significan word in the dd, so i auto store it 
    stosw ;in my destination string 
    loop repeta 

exit: 
    mov ax, 4c00h 
    int 21h 

code ends 
end start 

回答

0
ls equ ($-s)/4 ;this is supposed to be the length of my source string 

這實際上計算元件的數量。

mov cx, di 
sub cx, offset d ;here I find out with how many words ... 

在你插入PROC的第二次調用這將設置CX = 4只給出3個值的列表,這是太大了。我建議你用CX除以4.

dec di 
dec di ;since I work with doublewords... 
dec di ;3 times but here my procedure gets fuzzy 

這肯定是錯誤的。 SCASW表示你要麼減少4,要麼不減少!

mov bx, word ptr es:[di+1] ;this part is supposed to interchange words... 
mov word ptr es:[di+2], bx 

這是行不通的,因爲偏移量只有1個字節的距離!

jmp DIplus1 

這會產生DI的單個增量,因此會產生錯誤,因爲您想在該位置存儲單詞。

+0

Ooo謝謝!我修好了,它現在工作正常^ _ ^是的,我以前用scasw工作過,但是我把它改成了cmp,然後不再需要DI增量了。非常感謝。 – Katie44