The following problem was stolen redeployed from Jason Fritts at St. Louis Unive
ID: 3796168 • Letter: T
Question
The following problem was stolen redeployed from Jason Fritts at St. Louis University.
Given the following x86 assembly code that sums the numbers from 1 to N:
; assume you have put the value of N into the edi register
mov eax, 0 # eax: sum = 0
mov ecx, 1 # ecx: i = 1
LOOP:
add eax, ecx
add ecx, 1
cmp ecx, edi
jle LOOP
LOOP_EXIT:
...
Note: The variables of i, N, and sum are stored in registers ecx, edi, and eax, respectively.
a. What one line would you need to add and/or change in order to sum the following number sequence? Write it below.
1 4 7 10 13 16 19 22 25 28 ...
b. What two lines (of the original code) would you need to add and/or change in order to compute the factorial of N?
c. What lines (of the original code) would you need to add and/or change in order to compute the sum of squares from 1 to N?
i.e. 12 + 22 + 32 + 42 + 52 + ...
= 1 + 4 + 9 + 16 + 25 + ...
Explanation / Answer
Solution :
1)
;assume you have put the value of N into the edi register
mov eax, 0 # eax: sum = 0
mov ecx, 1 # ecx: i = 1
LOOP:
add eax, ecx
add ecx, 3 #this line has been changed.
cmp ecx, edi
jle LOOP
LOOP_EXIT:
2)
;assume you have put the value of N into the edi register
mov eax, 1 # eax: fact = 1
mov ecx, 1 # ecx: i = 1
LOOP:
mul eax, ecx #multiplication will be performed.
add ecx, 1
cmp ecx, edi
jle LOOP
LOOP_EXIT:
3)
;assume you have put the value of N into the edi register
mov eax, 0 # eax: sum of squares = 0
mov ecx, 1 # ecx: i = 1
LOOP:
mul ecx,ecx
add eax, ecx
add ecx, 1
cmp ecx, edi
jle LOOP
LOOP_EXIT:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.