section .data
fmt_d db "%d", 0
fmt_out db "%d ", 0
nl db 10, 0
section .bss
N resd 1
tmp resd 1
cur_sz resd 1
cur_ptr resd 1
best_sz resd 1
best_ptr resd 1
best_hi resd 1
best_lo resd 1
tr_hi resd 1
tr_lo resd 1
section .text
extern scanf, printf, malloc, free
global main
main:
push ebp
mov ebp, esp
and esp, -16
push ebx
push esi
push edi
mov dword [best_hi], 0x80000000
mov dword [best_lo], 0
mov dword [best_ptr], 0
; read N
sub esp, 8
push N
push fmt_d
call scanf
add esp, 16
xor ebx, ebx
.matrix_loop:
cmp ebx, [N]
jge .output
inc ebx
; read size
sub esp, 8
push tmp
push fmt_d
call scanf
add esp, 16
mov esi, [tmp]
mov [cur_sz], esi
; malloc
mov eax, esi
imul eax, esi
shl eax, 2
sub esp, 12
push eax
call malloc
add esp, 16
mov [cur_ptr], eax
; trace = 0
mov dword [tr_hi], 0
mov dword [tr_lo], 0
xor ecx, ecx ; i = 0
xor edi, edi ; j = 0
.read_loop:
cmp ecx, esi
jge .read_done
xor edi, edi
.col_loop:
cmp edi, esi
jge .next_row
; addr = cur_ptr + (i*n + j)*4
mov eax, ecx
imul eax, esi
add eax, edi
shl eax, 2
add eax, [cur_ptr]
; read value
sub esp, 8
push eax
push fmt_d
call scanf
add esp, 16
; если i == j → добавляем в trace
cmp ecx, edi
jne .skip_add
mov eax, [eax]
cdq
add [tr_lo], eax
adc [tr_hi], edx
.skip_add:
inc edi
jmp .col_loop
.next_row:
inc ecx
jmp .read_loop
.read_done:
; compare trace
mov eax, [tr_hi]
cmp eax, [best_hi]
jg .update_best
jl .no_update
mov eax, [tr_lo]
cmp eax, [best_lo]
ja .update_best
.no_update:
sub esp, 12
push dword [cur_ptr]
call free
add esp, 16
jmp .matrix_loop
.update_best:
cmp dword [best_ptr], 0
je .skip_free
sub esp, 12
push dword [best_ptr]
call free
add esp, 16
.skip_free:
mov eax, [tr_hi]
mov [best_hi], eax
mov eax, [tr_lo]
mov [best_lo], eax
mov eax, [cur_ptr]
mov [best_ptr], eax
mov eax, [cur_sz]
mov [best_sz], eax
jmp .matrix_loop
.output:
mov esi, [best_sz]
xor ebx, ebx
.print_row:
cmp ebx, esi
jge .done
xor edi, edi
.print_col:
cmp edi, esi
jge .print_nl
mov eax, ebx
imul eax, esi
add eax, edi
shl eax, 2
add eax, [best_ptr]
mov eax, [eax]
sub esp, 8
push eax
push fmt_out
call printf
add esp, 16
inc edi
jmp .print_col
.print_nl:
sub esp, 12
push nl
call printf
add esp, 16
inc ebx
jmp .print_row
.done:
pop edi
pop esi
pop ebx
mov esp, ebp
pop ebp
xor eax, eax
ret