2011-12-24 96 views
0

我在程序集8086中做了一個代碼。我在內存中加載矩陣(數組),尺寸爲3x3。但是這個代碼僅適用於矩陣3x3的這個維度。有人能給我一個想法,我怎麼能使它與尺寸m x n一起工作?該數組加載到內存中,最後只是打印結果,另一個數組。在給定的存儲器由於矩陣在程序集mxn尺寸

; multi-segment executable file template. 

data segment 
matrix db 1, 2, 3, 4, 5, 6, 7, 8, 9 ; load matrix in memory 

ends 

stack segment 
dw 128 dup(0) 
ends 

code segment 
start: 
; set segment registers: 
mov ax, data 
mov ds, ax 
mov es, ax 



mov bx, matrix ; move matrix to offset bx 
mov ch, 3 
mov cl, 0 


COLUMNAHEAD: 

mov dl, [bx] ; get the first element of matrix in dl 
add dl, 30h ; add to show number 
mov ah, 02h 
int 21h ; print first number 
inc cl 

cmp cl, ch ; compare if the counter is at the end of column 


jge ROWAHEAD ; if greater go to row 
add bx, 3 ; if not inc offset for 3 
jmp COLUMNAHEAD 





ROWAHEAD: 
inc cl ; 1 element of roe 
inc bx ; inc offset for one place 
mov dl, [bx] ; get the number in dl 
add dl, 30h ; convert to number 
mov ah, 02h 
int 21h ; print the number 

cmp cl, 5 
je COLUMNAHEAD 
jne ROWAHEAD 


COLUMNBACK: 
inc cl 
sub bx, 3 
mov dl, [bx] 
add dl, 30h 
mov ah, 02h 
int 21h 
cmp cl, 7 
jne COLUMNBACK 
je ROWBACK 

ROWBACK: 
dec bx 
mov dl, [bx] 
add dl, 30h 
mov ah, 02h 
int 21h 
JMP MIDDLE 


MIDDLE: 
add bx, 3 
mov dl, [bx] 
add dl, 30h 
mov ah, 02h 
int 21h 

JMP END 

END: 


this is the code i wrote. it works for the matrix 
1, 2, 3, 
4, 5, 6, 
7, 8, 9 and print 1, 4, 7, 8, 9, 6, 3, 2, 5 

矩陣從順時針打印螺旋在相反的方向(左邊的列向下右下範圍,直到柱中,一系列左上等的直到到達的環境中)。這適用於維度3x3。這應該適用於mxn維度。但我不知道如何,任何建議?

+0

你有什麼麻煩? – 2011-12-24 19:22:11

+0

我知道如何解決矩陣3x3,但我需要編寫與矩陣m =行和n =列一起工作的代碼....我上面寫的代碼只適用於矩陣3x3 .. – buuuuu 2011-12-24 19:26:08

+0

是的,但是部分是這個問題?看來你已經掌握了程序集。 – 2011-12-24 19:27:23

回答

0

當有矩陣M(行)x N(cols)時,所需算法的一圈打印矩陣的周長。下一回合得到(M-2)×(N-2)矩陣(以特殊方式存儲在存儲器中)。這個事實允許有效的迭代公式化。

讓我們利用上面的算法。然後轉彎看起來像:

COLUMNAHEAD(M); ROWAHEAD(N-1); COLUMNBACK(M-1); ROWBACK(N-2); 

這裏要打印的元素的數量顯示在括號中。

- pointer to the current element (your ds:bx) 
- values M and N, kept until algorithm stopped 
- shift parameter, let be D 

在更多的細節所需的算法的草圖看起來像:當任何這些數字達到0

所以,你需要的基質本身不談以下變量的算法應該停止

D := 0 
label: 
if (M-D==0) stop 
COLUMNAHEAD(M-D) 
inc D 
if (N-D==0) stop 
ROWAHEAD(N-D) 
if (M-D==0) stop 
COLUMNBACK(M-D) 
inc D 
if (N-D==0) stop 
ROWBACK(N-D) 
goto label 

特別要注意糾正指數(使用M和1的位移值)。簡單的優化,比如最小化由遞減變量(DEC/JZ或甚至LOOP)引導的循環中的指令數或者更好的寄存器使用(si而不是bx)也是可取的。還要注意,矩陣通常是按列存儲在內存中的(然後1和N變成正確的位移)。

+0

我試圖在我的代碼中應用您的代碼,但我根本不明白。你可以嘗試修復我的代碼?你的解釋很好,但我沒有太多經驗。如果你可以嘗試根據你的解釋編寫程序對我來說非常有用。在此先感謝Olion – buuuuu 2012-01-05 01:55:00