-
Notifications
You must be signed in to change notification settings - Fork 0
/
081.asm
112 lines (98 loc) · 2.79 KB
/
081.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
format ELF64 executable 9
segment readable
f: db "0081_matrix.txt", 0
segment readable writable
result: times 10 db 0 ;empty string for printing the result later
db 10, 0
buf: rb 32000
mat: rd 6400
segment readable executable
entry start
start:
mov eax, 5 ;file open
mov edi, f ;file name
mov esi, 0 ;read only
syscall
mov edi, eax ;file descriptor
mov eax, 3 ;read
mov esi, buf ;into buffer
mov edx, 32000 ;32000 bytes
syscall
mov edi, eax ;file descriptor
mov eax, 6 ;close
syscall
xor edi, edi ;reset registers for building the matrix
xor esi, esi
mov ebx, 10
xor ecx, ecx
next_n:
xor eax, eax ;reset eax, will hold the converted number
convert_n:
mov cl, [buf + esi]
cmp ecx, '0' ;is buf[esi] not a digit (i.e. ',' or '\n')?
jl n_done ;if yes, we are at the end of the number
mul ebx ;else convert string to number
sub ecx, '0'
add eax, ecx
inc esi
jmp convert_n
n_done:
mov [mat + 4 * edi], eax ;put current number in mat[edi]
inc esi ;increase edi and esi and continue
inc edi
cmp edi, 6400
jl next_n
mov edi, 6399 ;reset edi to last item in mat
mov esi, edi ;dito for esi
last_row_col:
mov eax, [mat + 4 * edi]
mov ebx, [mat + 4 * esi]
dec edi
sub esi, 80
add [mat + 4 * edi], eax ;add mat[edi] to mat[edi - 1]
add [mat + 4 * esi], ebx ;add mat[esi] to mat[esi - 80]
cmp esi, 159 ;finished?
jge last_row_col ;if not, repeat
mov edi, 6398 ;reset esi and edi again
mov esi, 6319
mov ecx, 80 ;for /mod 80
pathsum:
mov eax, [mat + 4 * edi] ;add min of right and below to mat[esi]
mov ebx, [mat + 4 * esi]
dec edi
dec esi
cmp eax, ebx
cmovg eax, ebx
add [mat + 4 * esi], eax
test esi, esi
jz finished
mov eax, esi
xor edx, edx
div ecx
test edx, edx
jnz pathsum
dec edi
dec esi
jmp pathsum
finished:
mov eax, [mat]
mov ebx, 10
mov ecx, 9
convert_result:
xor edx, edx
div ebx
add edx, '0'
mov [result + ecx], dl
dec ecx
test eax, eax
jnz convert_result
print:
mov eax, 4
mov edi, 1
mov esi, result
mov edx, 12
syscall
exit:
mov eax, 1
xor edi, edi
syscall