; School of Computer Engineering
; K.N. Toosi University of Technology

%include "asm_io.inc"
segment .data

array:  dd    10, 20, 30, 40, 50, 60
        dd    11, 21, 31, 41, 51, 61
        dd    12, 22, 32, 42, 52, 62
        dd    14, 24, 34, 44, 54, 64
        
                
segment .text
        global asm_main

asm_main:
        pusha


        ; print2DArray(array, m, n)
        push 6                  ; no of columns
        push 4                  ; no of rows
        push array              ; address of array
        call print2DArray
        add  esp, 12
        
        popa
        ret

delim:  db   ", ", 0

%define ARRAY  [ebp+8]
%define M      [ebp+12]
%define N      [ebp+16]
        
; print2DArray(ARRAY, M, N)
print2DArray:
        push ebp
        mov  ebp, esp
        
        mov ebx, ARRAY
        mov esi, 0
loop1:
        cmp esi, M
        jge endloop1
                
        mov edi, 0
loop2:
        cmp edi, N
        jge endloop2

        ; index = esi*N+edi
        mov eax, N
        mul esi
        add eax, edi

        mov eax, [ebx+4*eax]
        call print_int
        mov eax, delim
        call print_string
        
        inc edi
        jmp loop2
endloop2:

        mov al, 10
        call print_char
        
        inc esi
        jmp loop1
endloop1:
        mov esp, ebp
        pop ebp
        ret