-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathmain_x86.asm
128 lines (108 loc) · 1.76 KB
/
main_x86.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
format ELF executable 3
define SYS_EXIT 1
define SYS_WRITE 4
define STDOUT 1
define SUCCESS 0
define N 440000000
segment readable executable
entry start
start:
mov eax, 0
mov [i], eax
prepare_cache:
mov eax, [i]
mov ebx, [i]
mov ecx, 1
exponentiation:
mul ebx
inc ecx
cmp ecx, [i]
jl exponentiation
mov ebx, cache
mov ecx, [i]
mov [ebx+4*ecx], eax
inc dword [i]
cmp dword [i], 10
jl prepare_cache
mov eax, 0
mov [i], eax
main_loop:
mov eax, [i]
call is_munchausen
cmp eax, 0
jne yes_it_is
jmp main_loop_end
yes_it_is:
mov eax, [i]
call print_number
call print_newline
main_loop_end:
inc dword [i]
cmp dword [i], N
jl main_loop
mov eax, SYS_EXIT
mov ebx, SUCCESS
int 0x80
is_munchausen:
mov [number], eax
mov [n], eax
mov eax, 0
mov [total], eax
loop0:
mov eax, [n]
mov edx, 0
div dword [const10]
mov [n], eax
mov ebx, cache
mov ecx, edx
mov eax, [ebx+4*ecx]
add [total], eax
mov eax, [number]
cmp [total], eax
jg ret_0
cmp dword [n], 0
jg loop0
mov eax, [number]
cmp dword [total], eax
je ret_1
ret_0:
mov eax, 0
ret
ret_1:
mov eax, 1
ret
print_newline:
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, newline
mov edx, 1
int 0x80
ret
print_number:
push eax
push edx
xor edx, edx
div dword [const10]
cmp eax, 0
je l1
call print_number
l1:
mov [char], dl
add byte [char], '0'
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, char
mov edx, 1
int 0x80
pop edx
pop eax
ret
segment readable writable
const10 dd 10
newline db 0xa
i rd 1
cache rd 10
n rd 1
number rd 1
total rd 1
char rb 1