Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m trying to complete the programming 5.5 (3) Programming Exercise in: Introdu

ID: 3681706 • Letter: I

Question

I'm trying to complete the programming 5.5 (3) Programming Exercise in: Introduction to 80x86 by Robert Detmer (pg. 162)

I need to convert the C++ function to asslemby. I have done some below but working with ASM array what i'm struggling with.


// Function in C++ ::

prime[l] := 2; { first prime number }
prime[2] := 3; { second prime number }
primeCount := 2;
candidate := 5; { first candidate for a new prime }
while primeCount < 100 loop
index := 1;
while ( index le primeCount)
and (prime[index] does not evenly divide candidate) loop
add 1 to index;
end while;
if (index > primeCount)
then {no existing prime evenly divides the candidate, so it is a new prime}
add 1 to primeCount;
prime[primeCount] := candidate ;
end if;
add 2 to candidate;
end while;

// Function in Assembly (CURRENT CODE) ::

   ; prime[l] := 2; { first prime number }
   ; prime[2] := 3; { second prime number }
mov edx, 2           ; primeCount := 2;
mov ebx, 50           ; candidate := 5; { first candidate for a new prime }
whilepC:   cmp edx, 100      ; while primeCount < 100 loop
jnl whilepC ; exit if not
mov ecx, 1 ; index := 1;
whileIdx:   cmp ecx, edx       ; while ( index le primeCount)
jnle whilepC
    ; and (prime[index] does not evenly divide candidate) loop
inc   ecx              ; add 1 to index;
endwhilepC:    ; end while;
cmp ecx, edx       ; if (index > primeCount)
jnl endwhilepC
   ; then {no existing prime evenly divides the candidate, so it is a new prime}
inc edx ; add 1 to primeCount;
    ; prime[primeCount] := candidate ;
   ; end if;
add ebx, 2 ; add 2 to candidate;
jmp whilepC ; repeat
endwhileHW:                       ; end while;

Explanation / Answer

;Converting given C++ code into 8086 assembly language

;initially you have to declare prime variable and next initialize with 2 values.

prime db 100 ;declaring prime variable for 100 bytes of memory

;next initalize values using mov instruction.

mov edx,1;

mov al,2 ; use al to mov any instruction to array

mov [prime+edx],al ;intializing prime[1]=2

inc edx;

mov al,3

mov [prime+edx],al ;intializing prime[2]=2 and edx is prime count.

mov ebx,5 ;ebx is candidate assigned to 5

whilepC:   cmp edx, 100      ; while primeCount < 100 loop
jnl endwhilepC ; exit if not
mov ecx, 1 ; index := 1;
whileIdx:   cmp ecx, edx ; while ( index <= primeCount)
jnle endwhilepdx ; and (prime[index] does not evenly divide candidate) loop
inc   ecx ; add 1 to index;
endwhiledx: ; end whiledx loop;
cmp ecx, edx ; if (index > primeCount)
jng whilepC    ; end if;

; then {no existing prime evenly divides the candidate, so it is a new prime}
inc edx ; add 1 to primeCount;

mov al,edx
mov [prime+edx],al ; prime[primeCount] := candidate ;
    add ebx, 2 ; add 2 to candidate;
jmp whilepC ; repeat
endwhilepC:                       ; end whilepC