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

Delete task2.2.asm

parent 9a30bd5b
Branches
No related tags found
No related merge requests found
int __attribute__((cdecl)) asm_main(void);
int main() {
int ret_status;
ret_status = asm_main();
return ret_status;
}
%include "asm_io.inc" ; Include the I/O functions
segment .data
prompt_start db "Enter the start of the range: ", 0 ; Prompt for the start of the range
prompt_end db "Enter the end of the range: ", 0 ; Prompt for the end of the range
msg_result db "The sum of the array is: ", 0 ; Message for the sum of the array
newline db 10, 0 ; Newline character
segment .bss
array resd 100 ; Reserve space for 100 integers (the array)
sum resd 1 ; Reserve space for the sum
start resd 1 ; Reserve space for the start of the range
end resd 1 ; Reserve space for the end of the range
segment .text
global asm_main
asm_main:
pusha ; Save all registers
; Initialize the array with numbers from 1 to 100
mov ecx, 1 ; Start from 1
mov ebx, array ; Load the base address of the array
init_array:
mov [ebx], ecx ; Store the value of ECX in the array
add ebx, 4 ; Move to the next element (4 bytes)
inc ecx ; Increment ECX
cmp ecx, 101 ; Check if we reached 101
jl init_array ; If not, continue initializing
; Sum the entire array
mov ecx, 100 ; Loop 100 times
mov ebx, array ; Reload the base address of the array
xor edx, edx ; Clear EDX (sum)
sum_array:
add edx, [ebx] ; Add the current array element to EDX
add ebx, 4 ; Move to the next element
loop sum_array ; Repeat for 100 elements
; Store the sum in the 'sum' variable
mov [sum], edx ; Store the result in sum
; Print the result message
mov eax, msg_result ; Load the result message
call print_string ; Print the message
mov eax, [sum] ; Load the sum
call print_int ; Print the sum
call print_nl ; Print a newline
; Ask the user for the start of the range
mov eax, prompt_start ; Load the prompt_start message
call print_string ; Print the start prompt
call read_int ; Read the start value
mov [start], eax ; Store the start value
; Ask the user for the end of the range
mov eax, prompt_end ; Load the prompt_end message
call print_string ; Print the end prompt
call read_int ; Read the end value
mov [end], eax ; Store the end value
; Check if the range is valid
mov eax, [start] ; Load the start value
mov ebx, [end] ; Load the end value
cmp eax, ebx ; Compare start and end
jge invalid_range ; Jump if the start is greater than or equal to end
; Sum the range between start and end
xor edx, edx ; Clear EDX (range sum)
mov ecx, [start] ; Start from the 'start' value
mov ebx, [end] ; End at the 'end' value
sum_range:
add edx, ecx ; Add the current number to the sum
inc ecx ; Increment the counter
cmp ecx, ebx ; Check if we've reached the end
jle sum_range ; If not, continue the loop
; Print the sum of the range
mov eax, "The sum of the range is: " ; Print result message
call print_string ; Print the message
mov eax, edx ; Load the sum
call print_int ; Print the sum
call print_nl ; Print a newline
jmp end_program ; Jump to the end
invalid_range:
; Print an error message if the range is invalid
mov eax, "Error: Invalid range. The start must be less than the end." ; Load the error message
call print_string ; Print the error message
call print_nl ; Print a newline
end_program:
popa ; Restore all registers
mov eax, 0 ; Return 0 to indicate successful execution
ret
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment