2011-12-24 99 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