Archived
Практика
This commit is contained in:
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 1.8 MiB |
Executable
BIN
Binary file not shown.
@@ -0,0 +1,16 @@
|
||||
ifdef SystemRoot
|
||||
format = win32
|
||||
rm = del
|
||||
ext = .exe
|
||||
else
|
||||
format = elf
|
||||
rm = rm -f
|
||||
ext =
|
||||
endif
|
||||
|
||||
all: primes.o
|
||||
gcc primes.o -o primes$(ext)
|
||||
$(rm) primes.o
|
||||
|
||||
primes.o:
|
||||
nasm -f $(format) main.asm -o primes.o
|
||||
@@ -0,0 +1,229 @@
|
||||
;
|
||||
;
|
||||
; ÔÓÍÊÖÈÈ
|
||||
; Ðåçóëüòàò çàïèñûâàåòñÿ â EAX, ñòàòóñ - â EDX.
|
||||
;  ñëó÷àå óñïåõà EDX ñîäåðæèò çíà÷åíèå SUCCESS (0),
|
||||
; èí÷àå - àäðåñ ñîîáùåíèÿ îá îøèáêå.
|
||||
;
|
||||
;
|
||||
|
||||
; Ââåñòè ìàêñèìàëüíîå ÷èñëî
|
||||
; Ðåçóëüòàò: EAX - ìàêñèìàëüíîå ÷èñëî
|
||||
input_max_number:
|
||||
;ñîçäàòü ñòåê-ôðåéì,
|
||||
;4 áàéòà äëÿ ëîêàëüíûõ ïåðåìåííûõ
|
||||
enter 4, 1
|
||||
|
||||
;ïîêàçûâàåì ïîäïèñü
|
||||
push str_max_number_label ;ñì. string_constants.asm
|
||||
call _printf
|
||||
add esp, 4
|
||||
|
||||
;âûçûâàåì scanf
|
||||
mov eax, ebp
|
||||
sub eax, 4
|
||||
|
||||
push eax
|
||||
push str_max_number_input_format ;ñì. string_constants.asm
|
||||
call _scanf
|
||||
add esp, 8
|
||||
|
||||
mov eax, [ebp-4]
|
||||
|
||||
;ïðîâåðêà
|
||||
cmp eax, MIN_MAX_NUMBER
|
||||
jb .number_too_little
|
||||
cmp eax, MAX_MAX_NUMBER
|
||||
ja .number_too_big
|
||||
jmp .success
|
||||
|
||||
;âûõîä
|
||||
.number_too_little:
|
||||
mov edx, str_error_max_num_too_little ;ñì. string_constants.asm
|
||||
jmp .return
|
||||
|
||||
.number_too_big:
|
||||
mov edx, str_error_max_num_too_big ;ñì. string_constants.asm
|
||||
jmp .return
|
||||
|
||||
.success:
|
||||
push eax
|
||||
push str_max_number_output_format ;ñì. string_constants.asm
|
||||
call _printf
|
||||
add esp, 4
|
||||
pop eax
|
||||
mov edx, SUCCESS
|
||||
|
||||
.return:
|
||||
leave
|
||||
ret
|
||||
|
||||
|
||||
; Âûäåëèòü ïàìÿòü äëÿ ìàññèâà ôëàãîâ
|
||||
; Àðãóìåíò: EAX - ìàêñèìàëüíîå ÷èñëî
|
||||
; Ðåçóëüòàò: EAX - óêàçàòåëü íà ïàìÿòü
|
||||
allocate_flags_memory:
|
||||
enter 8, 1
|
||||
|
||||
;âûäåëèòü EAX+1 áàéò
|
||||
inc eax
|
||||
mov [ebp-4], eax
|
||||
|
||||
push eax
|
||||
call _malloc
|
||||
add esp, 4
|
||||
|
||||
;ïðîâåðêà
|
||||
cmp eax, 0
|
||||
je .fail
|
||||
mov [ebp-8], eax
|
||||
|
||||
;èíèöèàëèçàöèÿ
|
||||
mov byte [eax], 0
|
||||
|
||||
cld
|
||||
mov edi, eax
|
||||
inc edi
|
||||
mov edx, [ebp-4]
|
||||
add edx, eax
|
||||
|
||||
mov al, 1
|
||||
.write_true:
|
||||
stosb
|
||||
cmp edi, edx
|
||||
jb .write_true
|
||||
|
||||
;âûõîä
|
||||
mov eax, [ebp-8]
|
||||
jmp .success
|
||||
|
||||
.fail:
|
||||
mov edx, str_error_malloc_failed ;ñì. string_constants.asm
|
||||
jmp .return
|
||||
|
||||
.success:
|
||||
mov edx, SUCCESS
|
||||
|
||||
.return:
|
||||
leave
|
||||
ret
|
||||
|
||||
; Îñâîáîäèòü ïàìÿòü îò ìàññèâà ôëàãîâ
|
||||
; Àðãóìåíò: EAX - óêàçàòåëü íà ïàìÿòü
|
||||
free_flags_memory:
|
||||
enter 0, 1
|
||||
|
||||
push eax
|
||||
call _free
|
||||
add esp, 4
|
||||
|
||||
leave
|
||||
ret
|
||||
|
||||
|
||||
;Íàéòè ïðîñòûå ÷èñëà ñ ïîìîùüþ ðåøåòà Ýðàòîñôåíà
|
||||
;Àðãóìåíòû: EAX - óêàçàòåëü íà ìàññèâ ôëàãîâ, EBX - ìàêñèìàëüíîå ÷èñëî
|
||||
find_primes_with_eratosthenes_sieve:
|
||||
enter 8, 1
|
||||
mov [ebp-4], eax
|
||||
|
||||
add eax, ebx
|
||||
inc eax
|
||||
mov [ebp-8], eax
|
||||
|
||||
;âû÷åðêèâàåì ñîñòàâíûå ÷èñëà
|
||||
cld
|
||||
mov edx, 2 ;p = 2
|
||||
mov ecx, 2 ;ìíîæèòåëü ñ = 2
|
||||
.strike_out_cycle:
|
||||
;x = c*p
|
||||
mov eax, edx
|
||||
push edx
|
||||
mul ecx
|
||||
pop edx
|
||||
|
||||
cmp eax, ebx
|
||||
jbe .strike_out_number
|
||||
jmp .increase_p
|
||||
|
||||
.strike_out_number:
|
||||
mov edi, [ebp-4]
|
||||
add edi, eax
|
||||
mov byte [edi], 0
|
||||
inc ecx ;c = c + 1
|
||||
jmp .strike_out_cycle
|
||||
|
||||
.increase_p:
|
||||
mov esi, [ebp-4]
|
||||
add esi, edx
|
||||
inc esi
|
||||
|
||||
mov ecx, edx
|
||||
inc ecx
|
||||
.check_current_number:
|
||||
mov eax, ecx
|
||||
mul eax
|
||||
cmp eax, ebx
|
||||
ja .return
|
||||
|
||||
lodsb
|
||||
inc ecx
|
||||
cmp al, 0
|
||||
jne .new_p_found
|
||||
jmp .check_current_number
|
||||
|
||||
.new_p_found:
|
||||
mov edx, ecx
|
||||
dec edx
|
||||
mov ecx, 2
|
||||
jmp .strike_out_cycle
|
||||
|
||||
.return:
|
||||
leave
|
||||
ret
|
||||
|
||||
|
||||
; Âûâåñòè ïðîñòûå ÷èñëà
|
||||
; Ïàðàìåòðû: EAX - óêàçàòåëü íà ìàññèâ ôëàãîâ, EBX - ìàêñèìàëüíîå ÷èñëî
|
||||
print_primes:
|
||||
enter 12, 1
|
||||
mov [ebp-4], eax
|
||||
mov [ebp-8], ebx
|
||||
|
||||
push str_print_primes_label
|
||||
call _printf
|
||||
add esp, 4
|
||||
|
||||
cld
|
||||
mov esi, [ebp-4]
|
||||
mov edx, esi
|
||||
add edx, [ebp-8]
|
||||
inc edx
|
||||
|
||||
mov [ebp-12], edx
|
||||
mov ecx, 0
|
||||
.print_cycle:
|
||||
lodsb
|
||||
cmp al, 0
|
||||
jne .print
|
||||
jmp .check_finish
|
||||
.print:
|
||||
push esi
|
||||
push ecx
|
||||
push str_prime ;ñì. string_constants.asm
|
||||
call _printf
|
||||
add esp, 4
|
||||
pop ecx
|
||||
pop esi
|
||||
mov edx, [ebp-12]
|
||||
.check_finish:
|
||||
inc ecx
|
||||
cmp esi, edx
|
||||
jb .print_cycle
|
||||
|
||||
push str_cr_lf
|
||||
call _printf
|
||||
add esp, 4
|
||||
|
||||
leave
|
||||
ret
|
||||
@@ -0,0 +1,63 @@
|
||||
%define SUCCESS 0
|
||||
%define MIN_MAX_NUMBER 3
|
||||
%define MAX_MAX_NUMBER 4294967294
|
||||
|
||||
global _main
|
||||
extern _printf
|
||||
extern _scanf
|
||||
extern _malloc
|
||||
extern _free
|
||||
|
||||
SECTION .text
|
||||
_main:
|
||||
enter 0, 0
|
||||
|
||||
;ââîä ìàêñèìàëüíîãî ÷èñëà
|
||||
call input_max_number
|
||||
cmp edx, SUCCESS
|
||||
jne .custom_exit
|
||||
mov [max_number], eax
|
||||
|
||||
;âûäåëÿåì ïàìÿòü äëÿ ìàññèâà ôëàãîâ
|
||||
mov eax, [max_number]
|
||||
call allocate_flags_memory
|
||||
cmp edx, SUCCESS
|
||||
jne .custom_exit
|
||||
mov [primes_pointer], eax
|
||||
|
||||
;îòñåÿòü ñîñòàâíûå ÷èñëà
|
||||
mov eax, [primes_pointer]
|
||||
mov ebx, [max_number]
|
||||
call find_primes_with_eratosthenes_sieve
|
||||
|
||||
;âûâåñòè ÷èñëà
|
||||
mov eax, [primes_pointer]
|
||||
mov ebx, [max_number]
|
||||
call print_primes
|
||||
|
||||
;îñâîáîäèòü ïàìÿòü îò ìàññèâà ôëàãîâ
|
||||
mov eax, [primes_pointer]
|
||||
call free_flags_memory
|
||||
|
||||
;âûõîä
|
||||
.success:
|
||||
push str_exit_success
|
||||
call _printf
|
||||
jmp .return
|
||||
|
||||
.custom_exit:
|
||||
push edx
|
||||
call _printf
|
||||
|
||||
.return:
|
||||
mov eax, SUCCESS
|
||||
leave
|
||||
ret
|
||||
|
||||
%include "functions.asm"
|
||||
|
||||
SECTION .data
|
||||
max_number: dd 0
|
||||
primes_pointer: dd 0
|
||||
|
||||
%include "string_constatns.asm"
|
||||
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,14 @@
|
||||
;ïîäïèñè ââîäà-âûâîäà, ôîðìàòû
|
||||
str_max_number_label: db "Max number (>=3): ", 0
|
||||
str_max_number_input_format: db "%u", 0
|
||||
str_max_number_output_format: db "Using max number %u", 0xD, 0xA, 0
|
||||
|
||||
str_print_primes_label: db "Primes:", 0xD, 0xA, 0
|
||||
str_prime: db "%u", 0x9, 0
|
||||
str_cr_lf: db 0xD, 0xA, 0
|
||||
|
||||
;ñîîáùåíèÿ âûõîäà
|
||||
str_exit_success: db "Success!", 0xD, 0xA, 0
|
||||
str_error_max_num_too_little: db "Max number is too little!", 0xD, 0xA, 0
|
||||
str_error_max_num_too_big: db "Max number is too big!", 0xD, 0xA, 0
|
||||
str_error_malloc_failed: db "Can't allocate memory!", 0xD, 0xA, 0
|
||||
BIN
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 2.7 MiB |
Binary file not shown.
|
After Width: | Height: | Size: 2.8 MiB |
Binary file not shown.
|
After Width: | Height: | Size: 2.4 MiB |
Binary file not shown.
|
After Width: | Height: | Size: 2.4 MiB |
Binary file not shown.
|
After Width: | Height: | Size: 2.3 MiB |
BIN
Binary file not shown.
Binary file not shown.
Binary file not shown.
Executable
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
Reference in New Issue
Block a user