-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathmain_x86.asm
127 lines (108 loc) · 1.72 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
SYS_EXIT equ 1
SYS_WRITE equ 4
STDOUT equ 1
SUCCESS equ 0
N equ 440000000
section .text
global _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
loop:
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 loop
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], edx
add byte [char], '0'
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, char
mov edx, 1
int 0x80
pop edx
pop eax
ret
section .data
const10 dd 10
newline db 0xa
section .bss
i resd 1
cache resd 10
n resd 1
number resd 1
total resd 1
char resb 1