; prtmat.asm ; ; compile nasm -f elf64 -l prtmat.lst prtmat.asm ; link gcc -m64 -o prtmat prtmat.o ; run ./prtmat ; extern printf ; the C function to be called %macro prtm 1 ; print macro, arg1 is string, name of matrix section .data ; for inside macro .fmt db "%s[%d][%d]=%e", 10, 0 .str db %1,0 ; %1 is macro call first actual parameter section .text mov rdi, .fmt ; address of format string mov rsi, .str ; string passed to macro mov rdx, [i] mov rcx, [j] movq xmm0, qword [x] ; first floating point in fmt mov rax, 1 ; 1 floating point arguments to printf call printf ; Call C function %endmacro section .data ; initialized msg: db "prtmat.asm runnung", 0 msg2: db "prtmat.asm finished", 0 msg3: db "using macro to print", 0 msg4: db "development with debug print", 0 fmt: db "%s", 10, 0 fmtbl: db " ", 10, 0 ; for blank line m1: dq 1.1, 1.2, 1.3, 2.1, 2.2, 2.3 ; row major 2 by 3 matrix m1name: db "m1", 0 ncol1: dq 2 nrow1: dq 3 m2: dq 1.1, 2.1, 3.1, 1.2, 2.2, 3.2 ; coloum major 3 by 2 matrix m2name: db "m2", 0 ncol2: dq 3 nrow2: dq 2 i8: dq 8 ; 8 bytes in qword 4*2 fmtm: db "%s[%d][%d]=%e", 10, 0 fijkl: db "i=%d, j=%d, k=%d, l=%d", 10, 0 section .bss i: resq 1 ; reserve space, 1 is one quad word j: resq 1 k: resq 1 l: resq 1 lm resq 1 ; address of matrix item x: resq 1 ; float type or any type section .text ; Code section. global main main: push rbp ; set up stack frame, must be aligned mov rdi,fmt ; address of format, required register rdi mov rsi,msg ; address of data mov rax,0 ; no float to print call printf ; Call C function mov rdi,fmt ; address of format, required register rdi mov rsi,msg3 mov rax,0 ; no float to print call printf ; Call C function mov rax,0 ; first print matrix mov [i],rax ; i=0 loopi: mov rax,[i] mov rbx,0 mov [j],rbx ; j=0 loopj: mov rax,[i] mov rbx,[j] imul rax,[ncol1] ; i*ncol1 add rax, rbx ; i*ncol1+j [i][j] mov [k],rax imul rax,[i8] ; byte address offset mov [l],rax ; (i*ncol1+j)*8 mov rbx,[l] movq xmm0,qword[m1+rbx] movq qword[x],xmm0 prtm "m1" mov rbx,[j] inc rbx mov [j],rbx cmp rbx,[ncol1] ; j