Skip to content
Snippets Groups Projects
Commit fc192e5a authored by k26-chow's avatar k26-chow
Browse files

edited

parent d7509f58
Branches
No related tags found
No related merge requests found
%include "asm_io.inc"
segment .data
prompt_start db "Enter the start of the range: ", 0 ;
prompt_end db "Enter the end of the range: ", 0 ;
msg_result db "The sum of the array is: ", 0 ;
prompt_start db "Enter the start index (1-100): ", 0 ; Prompt for start index
prompt_end db "Enter the end index (1-100): ", 0 ; Prompt for end index
error_msg db "Error: Invalid range. Please enter values between 1 and 100.", 0 ; Error message
newline db 10, 0 ; Newline character
sum_msg db "The sum of the selected range is: ", 0 ; Sum message
segment .bss
array resd 100 ;
sum resd 1 ;
start resd 1 ;
end resd 1 ;
array resd 100 ; Array to hold 100 integers
start_index resd 1 ; Variable to store start index
end_index resd 1 ; Variable to store end index
sum_result resd 1 ; Variable to store the sum result
segment .text
global asm_main
asm_main:
pusha ;
mov ebx, array ;
init_array:
mov [ebx], ecx ;
add ebx, 4 ;
inc ecx ;
cmp ecx, 101 ;
jl init_array ;
; Sum the entire array
mov ecx, 100 ;
mov ebx, array ;
xor edx, edx ;
sum_array:
add edx, [ebx] ;
add ebx, 4 ;
loop sum_array ;
pusha ; Save all registers
; Store the sum in the 'sum' variable
mov [sum], edx ;
; Initialize the array with values 1 to 100
mov ecx, 1 ; Start from 1
mov edi, array ; Point to the start of the array
initialize_array:
mov [edi], ecx ; Store current value into array
add edi, 4 ; Move to the next element (each element is 4 bytes)
inc ecx ; Increment the value
cmp ecx, 101 ; Stop when value reaches 101
jl initialize_array ; Repeat for values 1 to 100
; Print the result message
mov eax, msg_result ;
call print_string ;
mov eax, [sum] ;
call print_int ;
call print_nl ;
; Ask the user for the start index
mov eax, prompt_start ; Load "Enter the start index:" message
call print_string ; Print the prompt
call read_int ; Read the integer input (start index)
mov [start_index], eax ; Store start index
; Ask the user for the start of the range
mov eax, prompt_start ;
call print_string ;
call read_int ;
mov [start], eax ;
; Ask the user for the end index
mov eax, prompt_end ; Load "Enter the end index:" message
call print_string ; Print the prompt
call read_int ; Read the integer input (end index)
mov [end_index], eax ; Store end index
; Ask the user for the end of the range
mov eax, prompt_end ;
call print_string ;
call read_int ;
mov [end], eax ;
; Validate the range (check if start_index is less than or equal to end_index, and within bounds)
mov eax, [start_index] ; Load start index
cmp eax, 1 ; Check if start index >= 1
jl invalid_range ; Jump to error if less than 1
cmp eax, 100 ; Check if start index <= 100
jg invalid_range ; Jump to error if greater than 100
; Check if the range is valid
mov eax, [start] ;
mov ebx, [end] ;
cmp eax, ebx ;
jge invalid_range ;
mov eax, [end_index] ; Load end index
cmp eax, 1 ; Check if end index >= 1
jl invalid_range ; Jump to error if less than 1
cmp eax, 100 ; Check if end index <= 100
jg invalid_range ; Jump to error if greater than 100
; Sum the range between start and end
xor edx, edx ;
mov ecx, [start] ;
mov ebx, [end] ;
sum_range:
add edx, ecx ;
inc ecx ;
cmp ecx, ebx ;
jle sum_range ;
mov eax, [start_index] ; Load start index into eax
mov ebx, [end_index] ; Load end index into ebx
cmp eax, ebx ; Compare start index with end index
jg invalid_range ; If start > end, jump to invalid_range
; Print the sum of the range
mov eax, "The sum of the range is: " ; Print result message
call print_string ;
mov eax, edx ;
call print_int ;
call print_nl ;
; Sum the elements from start_index to end_index - 1
xor ecx, ecx ; Clear ECX (counter)
xor edx, edx ; Clear EDX (sum accumulator)
mov edi, array ; Point to the start of the array
mov esi, [start_index] ; Load start index (1-based)
sub esi, 1 ; Convert start index to 0-based (1 -> 0)
mov ecx, esi ; Set ECX to start index (0-based)
jmp end_program ;
sum_range:
; Add current element to the sum
mov eax, [array + ecx*4] ; Load the value from the array at index ECX
add edx, eax ; Add the value to the sum
inc ecx ; Move to the next index
cmp ecx, [end_index] ; Check if we reached the end index (ignoring the last element)
jge display_sum ; If ECX >= end_index, jump to display the sum
jmp sum_range ; Repeat summing
invalid_range:
; Print an error message if the range is invalid
mov eax, "Error: Invalid range. The start must be less than the end." ;
call print_string ;
call print_nl ;
; Print the error message
mov eax, error_msg ; Load error message
call print_string ; Print the error message
call print_nl ; Print a newline
jmp asm_main ; Restart the program
display_sum:
; Display the sum result
mov eax, sum_msg ; Load the sum message
call print_string ; Print the sum message
mov eax, edx ; Move the sum into EAX
call print_int ; Print the sum
call print_nl ; Print a newline
end_program:
popa ;
mov eax, 0 ;
ret
; End the program
popa ; Restore all registers
mov eax, 0 ; Set return value to 0
ret ; Return from the program
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment