Предполагается, что эта программа попросит пользователя ввести 10 целых чисел, а затем выводит сумму, среднее значение, диапазон и количество основных чисел. < /p>
Вот код: < /p>
section .data
prompt db "Enter integer: ", 0
newline db 10, 0
sum_msg db "Sum: ", 0
avg_msg db "Average: ", 0
range_msg db "Range: ", 0
prime_msg db "Number of primes: ", 0
buffer db 20 dup(0)
section .bss
input resb 20
numbers resd 10
section .text
global _start
_start:
mov ecx, 0 ; Counter for number of inputs
read_loop:
cmp ecx, 10
jge compute
; Display prompt
mov eax, 4 ; sys_write
mov ebx, 1 ; stdout
mov ecx, prompt
mov edx, 15
int 0x80
; Read input
mov eax, 3 ; sys_read
mov ebx, 0 ; stdin
mov ecx, input
mov edx, 20
int 0x80
; eax contains the number of bytes read
; Replace newline with null terminator
mov ebx, eax
cmp ebx, 1
jl skip_null
dec ebx
mov byte [input + ebx], 0
skip_null:
; Convert input to integer
mov esi, input
call str_to_int
mov [numbers + ecx*4], eax
inc ecx
jmp read_loop
compute:
; Initialize variables
xor eax, eax ; Sum
mov ebx, [numbers] ; Min
mov edx, [numbers] ; Max
xor edi, edi ; Prime count
xor ecx, ecx ; Index
calc_loop:
cmp ecx, 10
jge display_results
mov esi, [numbers + ecx*4]
add eax, esi ; Sum += number
cmp esi, ebx
jl set_min
jmp check_max
set_min:
mov ebx, esi
check_max:
cmp esi, edx
jg set_max
jmp check_prime
set_max:
mov edx, esi
check_prime:
push eax
push ebx
push ecx
push edx
push esi
call is_prime
cmp eax, 1
jne not_prime
inc edi
not_prime:
pop esi
pop edx
pop ecx
pop ebx
pop eax
inc ecx
jmp calc_loop
display_results:
; Display Sum
mov ecx, sum_msg
call print_string
mov eax, 0
mov ecx, 0
sum_loop:
cmp ecx, 10
jge sum_done
add eax, [numbers + ecx*4]
inc ecx
jmp sum_loop
sum_done:
call print_int
call print_newline
; Display Average
mov ecx, avg_msg
call print_string
mov eax, 0
mov ecx, 0
avg_loop:
cmp ecx, 10
jge avg_done
add eax, [numbers + ecx*4]
inc ecx
jmp avg_loop
avg_done:
mov ebx, 10
xor edx, edx
div ebx
call print_int
call print_newline
; Display Range
mov ecx, range_msg
call print_string
mov eax, edx
sub eax, ebx
call print_int
call print_newline
; Display Number of Primes
mov ecx, prime_msg
call print_string
mov eax, edi
call print_int
call print_newline
; Exit program
mov eax, 1
xor ebx, ebx
int 0x80
; -------------------------------
; Function: str_to_int
; Converts string at ESI to integer in EAX
str_to_int:
xor eax, eax
xor ebx, ebx
mov bl, byte [esi]
cmp bl, '-'
jne parse_digits
inc esi
call str_to_int
neg eax
ret
parse_digits:
xor eax, eax
.next_digit:
mov bl, byte [esi]
cmp bl, 0
je .done
cmp bl, '0'
jl .done
cmp bl, '9'
jg .done
sub bl, '0'
imul eax, eax, 10
add eax, ebx
inc esi
jmp .next_digit
.done:
ret
; -------------------------------
; Function: is_prime
; Checks if ESI is a prime number; result in EAX (1 = prime, 0 = not)
is_prime:
mov eax, esi
cmp eax, 2
jl not_prime_flag
mov ebx, 2
mov ecx, eax
shr ecx, 1
prime_loop:
cmp ebx, ecx
jg prime_flag
mov edx, 0
mov eax, esi
div ebx
cmp edx, 0
je not_prime_flag
inc ebx
jmp prime_loop
prime_flag:
mov eax, 1
ret
not_prime_flag:
xor eax, eax
ret
; -------------------------------
; Function: print_string
; Prints null-terminated string at ECX
print_string:
push eax
push ebx
push edx
mov edx, 0
.count:
cmp byte [ecx + edx], 0
je .print
inc edx
jmp .count
.print:
mov eax, 4
mov ebx, 1
int 0x80
pop edx
pop ebx
pop eax
ret
; -------------------------------
; Function: print_int
; Prints integer in EAX
print_int:
push eax
push ebx
push ecx
push edx
mov ecx, buffer
add ecx, 19
mov byte [ecx], 0
dec ecx
test eax, eax
jns .convert
neg eax
mov bl, '-'
mov [ecx], bl
dec ecx
.convert:
mov ebx, 10
.repeat:
xor edx, edx
div ebx
add dl, '0'
mov [ecx], dl
dec ecx
test eax, eax
jnz .repeat
inc ecx
mov eax, 4
mov ebx, 1
mov edx, 20
sub edx, ecx
int 0x80
pop edx
pop ecx
pop ebx
pop eax
ret
; -------------------------------
; Function: print_newline
; Prints a newline character
print_newline:
mov eax, 4
mov ebx, 1
mov ecx, newline
mov edx, 1
int 0x80
ret
Подробнее здесь: https://stackoverflow.com/questions/796 ... ly-program
Ошибка сегментации в моей программе сборки ⇐ Linux
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Почему printf () в моей программе SDL не печатает в окне вывода моей IDE?
Anonymous » » в форуме C++ - 0 Ответы
- 1 Просмотры
-
Последнее сообщение Anonymous
-