Matrix given in memory; the objective is to print the spiral in opposite direction from clockwise (left column down the right lower range, right up column, a series of upper left, etc. until you get to the environment). This works just for dimension 3x3. It should works for MxN dimension, but I don't know how. Any suggestions?

I need code in C++ for this matrix:

1, 2, 3,
4, 5, 6, 
7, 8, 9

the result is 1, 4, 7, 8, 9, 6, 3, 2, 5

If you have it working for a 3 x 3, post the code for that and perhaps people can offer advice for how to generalize to M x N.

I wrote code in assembly, and ask for help, but just few people know how to read code in assembly 8086. so i don't get the advice :( That is why i ask for C++ help... I will try to do that in assembly. is it useful to paste the code in assembly?thanks in advance

; 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

I know that is C++, and I need to write the code in C++, Could someone help?

I know that is C++, and I need to write the code in C++, Could someone help?

First post quote...

This works just for dimension 3x3. It should works for MxN dimension

That implies you have it working in C++ for 3 x 3 and need help generalizing it. No, it doesn't work for 3 x 3. If it worked for 3 x 3, you would have posted the C++ code for 3 x 3, not the Assembly code. It may work just great for 3 x 3 in Assembly, but who cares in the C++ forum? Do you know how to code at all in C++?

It is useful for me, if you write me, code in C++ for this task, and I translate in assembly... Please help me if you know, it is very important

It is useful for me, if you write me, code in C++ for this task, and I translate in assembly... Please help me if you know, it is very important

People here will give you suggestions to help you but I don't think that anyone will do the work for you

Edited 4 Years Ago by zeroliken: n/a

This article has been dead for over six months. Start a new discussion instead.