簡體   English   中英

在8086中排序32位數字

[英]sorting 32 bit numbers in 8086

我寫了這段代碼,考慮了大約5個小時; 但是我停留在排序部分:它開始循環,永不停止! 我在哪里錯了???

看來陣列未正確獲取數字。 有人可以幫忙嗎?

  page 110,100 Title 'decimal sorting' Data_here segment A DD Dup 20(?) msg1 DB 'Enter a maximum 6 digits number : ',0DH,0AH,'$' msg2 DB 0DH,0AH,'The sorted results are : ',0DH,0AH,'$' Data_here Ends Stack_here segment DW 10 DUP(?) Stack_here Ends Code_here segment Assume CS:code_here , DS:Data_here ,SS:Stack_here Main Proc near mov AX,Data_here mov DS,AX mov AX,Stack_here mov SS,AX Call decimal_input call sorting call decimal_output mov AH,4CH int 21H main Endp decimal_input proc near PUSH A mov AH,09H Lea DX,msg1 int 21H mov CH,20 next_number: mov BH,6 mov DX,0 mov si,4 next_digit: mov AH,07H int 21H CMP AL,0DH JNE check_digit CMP BH,6 JE next_digit DEC CH JZ end mov DX,0DH mov AH,02H int 21H mov DX,0AH Int 21H jmp next_number check_digit: cmp AL,30H JB next_digit cmp AL,39H JA next_digit mov AH,02H mov DL,AL int 21H SUB AL,30H SHL DX,4 ADD DL,AL DEC SI JZ save DEC BH JNZ next_digit jmp remain save: mov A[DI],DX SHL A[DI],8 add si,4 DEC BH jmp next_digit remain: ADD byte ptr A[DI],DL mov DX,0DH mov AH,02H int 21H mov DX,0AH INT 21H INC DI DEC CH JNZ next_number end: pop A RET decimal_input ENDp sorting proc near Push A mov SI,DI check: mov AX,word ptr A[SI] mov BX,word ptr A[DI+2] CMP AX,BX JA change JE extra_check add SI,2 continue: add DI,2 CMP DI,38 JA finish JB check extra_check: mov CX,word ptr A[DI+1] mov DX,word ptr A[DI+3] cmp CX,DX JNA continue mov word ptr A[DI+1],DX mov word ptr A[DI+3],CX jmp continue change: xchg AX,BX mov CX,word ptr A[DI+1] mov DX,word ptr A[DI+3] xchg CX,DX mov word ptr A[DI],AX mov word ptr A[DI+1],CX mov word ptr A[DI+2],BX mov word ptr A[DI+3],DX jmp continue finish: Add sp,2 cmp Sp,40 JE ending mov DI,SP jmp check ending: POP A RET sorting ENDP decimal_output proc near PUSH A mov AH,09H lea DX,msg2 INT 21H next_no: mov DL,0 mov AL,0 mov AH,0 next: CMP AL,0 JE next1 mov DL,byte ptr A[DI] ADD DL,30H mov AL,1 int 21H next1: inc DI inc AH cmp AH,8 JB next CMP DI,80 JA THE_END mov DX,0DH mov AH,02H int 21H mov DX,0AH int 21H jmp next_no THE_END: pop A RET decimal_output ENDp code_here Ends END MAIN 

我想你的問題可能在這里

Add sp,2
cmp Sp,40

您將sp用作循環計數器,但從未對其進行初始化。 另外,我也不明白為什么首先要使用sp 當然,從理論上講這是可能的,但是在返回之前,您必須將其還原。 當您的proc返回時,它將被破壞,因為堆棧指針已損壞。 因此,您應使用其他一些寄存器或內存變量(代碼中未使用bp )。

我強烈建議您重新格式化您的代碼,以使其更具可讀性(當我查看該代碼時,我將其格式化為這樣的格式,以使其更具可讀性):

sorting proc near 
    Push A
    mov SI,DI

check:                     
    mov AX,word ptr A[SI]
    mov BX,word ptr A[DI+2]
    CMP AX,BX
    JA change
    JE extra_check
    add SI,2

continue:
    add DI,2
    CMP DI,38
    JA finish
    JB check
 ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM