Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
os_worksheet
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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
k26-chow
os_worksheet
Commits
aef46c25
Commit
aef46c25
authored
7 months ago
by
k26-chow
Browse files
Options
Downloads
Patches
Plain Diff
Delete task2.2.asm
parent
9a30bd5b
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
task2.2.asm
+0
-105
0 additions, 105 deletions
task2.2.asm
with
0 additions
and
105 deletions
task2.2.asm
deleted
100644 → 0
+
0
−
105
View file @
9a30bd5b
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
seg
ment
.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
seg
ment
.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
seg
ment
.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
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