問題描述
emu8086 : ARR 不包含任何值 (emu8086 : ARR do not contain any value)
我試圖用彙編語言實現 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
為了進行測試,我在其後添加了另一個字符串數組,該數組甚至沒有顯示在變量窗口中。
誰能指出我的錯誤在哪裡?
參考解法
方法 1:
It appears you are trying to generate a COM program where the DS and CS segment are the same. The problem is that a COM program needs to start at offset 100h. So your ORG 1000H
should be ORG 100H
. Don't use a second ORG
in your code so you should remove the line ORG 1500H
. These two problems will confuse EMU8086 and when you go to display the variable it will be in the wrong place and will likely show data (like zeroes) that you don't expect.
The code would look like this:
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
(by Anklon、Michael Petch)