section .bss
syote resb 3000000
t resq 101010
valit resd 101010
p resd 101010
h resd 101010
aita resd 101010
edel resd 101010
seur resd 101010
puu resd 0x40000
vasen resq 101010
oikea resq 101010
tuloste resb 9000000
section .text
global _start
_start:
mov eax, 3
mov ebx, 0
mov ecx, syote
mov edx, 3000000
int 0x80
mov rsi, syote
call luku
mov r10, r9 ; n
xor rdx, rdx ; i
xor r14, r14 ; b
xor r15, r15 ; a
.lue:
call luku
inc rdx
mov [h+rdx*4], r9d
mov ebx, [h+rdx*4-4]
cmp rbx, r9
cmovg rbx, r9 ; rbx = max(h[i], h[i-1])
cmp r14, rbx
cmovl r14, rbx ; b=max(b, rbx)
cmp r15, r9
cmovl r15, r9 ; a=max(a, h[i])
cmp rdx, r10
jl .lue
mov rcx, r10
inc rcx
xor rbx, rbx
mov rdx, r10
inc rdx
.yhyy:
mov rax, rcx
dec rax
cmp ebx, [h+eax*4]
cmovg ebx, [h+eax*4]
cmp r14d, [h+eax*4] ; rbx= min(rbx, h[i])
jnl .k
mov esi, edx
sub esi, eax
mov [p+eax], esi
mov [valit+eax*4], ebx
and esi, 1
imul ebx, esi
add eax, 0x20000
mov [puu+eax*4], ebx ; puu[k] = rbx
call upd
mov ebx, r15d
mov rax, rcx
dec rax
mov [seur+eax*4], edx
mov [edel+rdx*4], eax
mov rdx, rax
mov r9d, [h+eax*4]
sal r9, 32
add r9, rax
mov [t+eax*8], r9
.k: dec rcx
cmp rcx, 0
jne .yhyy
mov r13d, [puu+4]
mov esi, 1
mov edi, r10d
inc edi
call mergesort
mov rdx, 1
.a:
mov rax, [t+rdx*8]
cmp rax, 0
je ._a
mov ecx, eax
mov ebx, [valit+eax*4]
mov edi, [p+eax*4]
mov esi, 0
add eax, 0x20000
mov [puu+eax*4], esi
call upd
mov eax, [edel+ecx*4]
cmp [valit+eax*4], ebx
jg .pyh
mov [valit+eax*4], ebx
.pyh:
inc edi
add [p+eax*4], edi
mov edi, [p+eax*4]
inc edi
and edi, 1
imul ebx, edi
add eax, 0x20000
mov [puu+eax*4], ebx
call upd
mov eax, [puu+4]
add eax, [h+ecx*4]
mov ebx, r13d
add ebx, r14d
cmp ebx, eax
cmovg r13d, [puu+4]
cmovg r14d, [h+ecx*4]
._a: inc rdx
cmp rdx, r10
jng .a
mov rdi, tuloste
mov rax, r15
;call tulosta
add rax, r14
;call tulosta
add rax, r13
call tulosta
mov rax, 3
call tulosta
call aitageneraattori
.lopeta:
mov rdx, rdi
sub rdx, tuloste
mov rax, 4
mov rbx, 1
mov rcx, tuloste
int 0x80
mov rax, 1
mov rbx, 0
int 0x80
luku:
xor r9, r9
.a:
xor rax, rax
lodsb
cmp al, 0x30
jl .mm
cmp al, 0x39
jg .mm
sub al, 0x30
imul r9, 10
add r9, rax
jmp .a
.mm:
cmp r9, 0
je .a
ret
upd:
.a:
sar eax, 1
sal eax, 1
mov r9d, [puu+eax*4]
inc eax
cmp r9d, [puu+eax*4]
cmovl r9d, [puu+eax*4]
sar eax, 1
mov [puu+eax*4], r9d
cmp eax, 0
jne .a
ret
mergesort:
push rbp
mov rbp, rsp
sub rsp, 12
cmp esi, edi
jnl .palaa
mov [rbp-4], esi
mov [rbp-8], edi
mov edx, esi
add edx, edi
sar edx, 1
mov [rbp-12], edx
mov edi, edx
call mergesort
mov esi, [rbp-12]
inc esi
mov edi, [rbp-8]
call mergesort
mov esi, [rbp-4]
mov edi, [rbp-8]
mov edx, [rbp-12]
call merge
.palaa:
leave
ret
merge:
mov ecx, edx
sub ecx, esi
inc ecx
xor r9, r9
mov r11d, esi
.a:
mov rax, [t+r11d*8]
mov [vasen+r9d*8], rax
inc r9d
inc r11d
loop .a
mov rax, 0x7000000000000000
mov [vasen+r9d*8], rax
mov ecx, edi
sub ecx, edx
xor r9, r9
mov r11d, edx
inc r11d
.b:
mov rax, [t+r11d*8]
mov [oikea+r9d*8], rax
inc r9d
inc r11d
loop .b
mov rax, 0x7000000000000000
mov [oikea+r9d*8], rax
mov ecx, edi
sub ecx, esi
inc ecx
xor r9, r9
xor r11, r11
mov r12d, esi
.c:
mov rax, [vasen+r9d*8]
cmp rax, [oikea+r11d*8]
jg .else
mov [t+r12d*8], rax
inc r12d
inc r9d
jmp .f
.else:
mov rax, [oikea+r11d*8]
mov [t+r12d*8], rax
inc r12d
inc r11d
.f loop .c
.palaa:
ret
tulosta:
mov r9, rax
add rdi, 18
std
.a:
mov rax, r9
mov r9, 0xa
xor rdx, rdx
idiv r9
mov r9, rax
mov rax, rdx
add al, 0x30
stosb
cmp r9, 0
jne .a
cld
add rdi, 19
mov al, 0x20
stosb
ret
aitageneraattori:
mov rcx, r10
.a1:
xor eax, eax
mov ebx, 1
cmp r14d, [h+ecx*4]
cmovl eax, ebx
mov [aita+ecx*4], eax
loop .a1
xor rcx, rcx
.a2:
inc rcx
xor eax, eax
cmp eax, [aita+rcx*4]
jne ._a2
cmp r13d, [h+rcx*4]
jnl ._a2
dec rcx
mov eax, 1
xor ebx, ebx
cmp eax, [aita+ecx*4]
mov eax, 2
cmove ebx, eax
cmp eax, [aita+ecx*4]
mov eax, 1
cmove ebx, eax
inc ecx
mov [aita+ecx*4], ebx
._a2: cmp rcx, r10
jl .a2
mov rcx, r10
.a3:
xor eax, eax
cmp eax, [aita+rcx*4]
jne ._a3
cmp r13d, [h+rcx*4]
jnl ._a3
inc ecx
inc eax
mov ebx, 1
cmp eax, [aita+ecx*4]
mov eax, 2
cmove ebx, eax
dec ecx
mov [aita+ecx*4], ebx
._a3: loop .a3
mov ecx, 0
.a4:
inc ecx
xor eax, eax
cmp eax, [aita+ecx*4]
jne ._a4
mov edx, ecx
dec edx
mov ebx, 1
mov eax, 2
cmp eax, [aita+edx*4]
je ._
add edx, 2
cmp eax, [aita+edx*4]
je ._
mov ebx, 2
jmp .__
._: mov eax, 1
xor edx, edx
dec ecx
cmp eax, [aita+ecx*4]
cmove ebx, edx
add ecx, 2
cmp eax, [aita+ecx*4]
cmove ebx, edx
dec ecx
.__ mov [aita+ecx*4], ebx
._a4: cmp rcx, r10
jl .a4
mov rcx, 0
.a5: inc rcx
xor rax, rax
mov eax, [aita+rcx*4]
inc eax
call tulosta
cmp rcx, r10
jl .a5
ret