1

我试图用汇编语言实现 QUICK SORT。当我在模拟器中运行代码时,数组“ARR”只包含零,没有加载任何值。我不知道我做错了什么。(ARR 在代码末尾定义。)

CODE SEGMENT
ASSUME CS:CODE,DS:CODE   
ORG 1000H

MOV DI,05H ; LAST INDEX (6-1)
XOR SI,SI; INNITAL INDEX
XOR BX,BX; PIVOT INDEX 
XOR BP,BP;

CALL QSORT
HLT

QSORT:

PUSH BP
PUSH DI
PUSH SI
CALL PARTITION
MOV SI,BP;
INC SI ; PIVOT INDEX +1 = INITIA IDEX 
CMP SI,DI

JNL SKIP_CALL  

 CALL QSORT
SKIP_CALL:
POP SI;
PUSH SI; 
MOV DI,BP;
DEC DI;
CMP SI,DI;

JNL SKIP_AGAIN

CALL QSORT    
SKIP_AGAIN:

POP SI;
POP DI;
POP BP;

RET

PARTITION:

PUSH SI 
PUSH DI
MOV BP,SI ;PIVOT INDEX
DEC SI; // TO INVALIDATE FIRST INCREMENT
FOR_1:
    INC SI
    CMP SI,DI
    JGE END_FOR_1
    MOV AL,ARR[SI]
    CMP AL,ARR[DI];      COMPAREING TWO INDEXVAL
    JL NO_SWAP 
                ;SWAP OPERATION
     PUSH AX; 
     XOR AX,AX;                     ENSURING ZEROS IN AH  
     MOV AL,ARR[BP];        SAVING ARR[SI] CONTENT
     MOV ARR[SI],AL; 
     POP AX;
     MOV ARR[BP],AL;        SAVING BP'S CONTENT IN SI :P
     INC BP;

  NO_SWAP:
     JMP FOR_1;

END_FOR_1: 
    MOV AL,ARR[DI];
    MOV AH,ARR[BP];
    MOV ARR[DI],AH;
    MOV ARR[BP],AL; 

 POP DI;
 POP SI;

RET 


ORG 1500H

ARR  DB 01H,02H,0AH,03H,00H,0CH; RANDOM ARRAY   
NAME DW 'ANKLON'  

ENDS

为了测试,我在变量窗口中添加了另一个字符串数组,它甚至没有显示。 var 的屏幕截图

谁能指出我的错误在哪里?

4

1 回答 1

1

您似乎正在尝试生成DSCS段相同的 COM 程序。问题是 COM 程序需要从偏移量100h开始。所以你ORG 1000H应该是ORG 100H。不要ORG在您的代码中使用第二个,因此您应该删除该行ORG 1500H。这两个问题会使EMU8086 感到困惑,当您显示变量时,它会出现在错误的位置,并且可能会显示您不期望的数据(如零)。

代码如下所示:

CODE SEGMENT
ASSUME CS:CODE,DS:CODE   
ORG 100H

MOV DI,05H ; LAST INDEX (6-1)
XOR SI,SI; INNITAL INDEX
XOR BX,BX; PIVOT INDEX 
XOR BP,BP;

CALL QSORT
HLT

QSORT:

PUSH BP
PUSH DI
PUSH SI
CALL PARTITION
MOV SI,BP;
INC SI ; PIVOT INDEX +1 = INITIA IDEX 
CMP SI,DI

JNL SKIP_CALL  

 CALL QSORT
SKIP_CALL:
POP SI;
PUSH SI; 
MOV DI,BP;
DEC DI;
CMP SI,DI;

JNL SKIP_AGAIN

CALL QSORT    
SKIP_AGAIN:

POP SI;
POP DI;
POP BP;

RET

PARTITION:

PUSH SI 
PUSH DI
MOV BP,SI ;PIVOT INDEX
DEC SI; // TO INVALIDATE FIRST INCREMENT
FOR_1:
    INC SI
    CMP SI,DI
    JGE END_FOR_1
    MOV AL,ARR[SI]
    CMP AL,ARR[DI];      COMPAREING TWO INDEXVAL
    JL NO_SWAP 
                ;SWAP OPERATION
     PUSH AX; 
     XOR AX,AX;                     ENSURING ZEROS IN AH  
     MOV AL,ARR[BP];        SAVING ARR[SI] CONTENT
     MOV ARR[SI],AL; 
     POP AX;
     MOV ARR[BP],AL;        SAVING BP'S CONTENT IN SI :P
     INC BP;

  NO_SWAP:
     JMP FOR_1;

END_FOR_1: 
    MOV AL,ARR[DI];
    MOV AH,ARR[BP];
    MOV ARR[DI],AH;
    MOV ARR[BP],AL; 

 POP DI;
 POP SI;

RET 

ARR  DB 01H,02H,0AH,03H,00H,0CH; RANDOM ARRAY   
NAME DW 'ANKLON'  

ENDS
于 2015-11-15T19:47:26.140 回答