The following problem was stolen redeployed from Jason Fritt

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 + ...

Solution

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:

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:
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:

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site