Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
OS_WorkSheet1
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
k26-chow
OS_WorkSheet1
Commits
fc192e5a
Commit
fc192e5a
authored
6 months ago
by
k26-chow
Browse files
Options
Downloads
Patches
Plain Diff
edited
parent
d7509f58
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/task2.2.asm
+77
-73
77 additions, 73 deletions
src/task2.2.asm
with
77 additions
and
73 deletions
src/task2.2.asm
+
77
−
73
View file @
fc192e5a
%include "asm_io.inc"
seg
ment
.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
seg
ment
.bss
array
resd
100
;
s
um
resd
1
;
start
resd
1
;
end
resd
1
;
array
resd
100
; Array to hold 100 integers
s
tart_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
seg
ment
.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
;
jg
e
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
disp
lay_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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment