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

1. In the following instruction sequence, show the values of the Carry, Zero, an

ID: 3807371 • Letter: 1

Question

1. In the following instruction sequence, show the values of the Carry, Zero, and Sign flags

where indicated:

mov al,00001111b

test al,00000010b ; a. CF= ZF= SF=

mov al,00000110b

cmp al,00000101b ; b. CF= ZF= SF=

mov al,00000101b

cmp al,00000111b ; c. CF= ZF= SF=

2. What will be the final value in EDX after this code executes?

mov edx,1

mov eax,7FFFh

cmp eax,8000h

jl L1

mov edx,0

L1:

3. Implement the following pseudocode in assembly language. Use short-circuit evaluation

and assume that val1 and X are 32-bit variables.

if( val1 > ecx ) AND ( ecx > edx )

X = 1

else

X = 2;

Explanation / Answer

Answers:

1. In the following instruction sequence, show the values of the Carry, Zero, and Sign flags

where indicated:

mov al,00001111b

test al,00000010b ; a. CF= ZF= SF=

mov al,00000110b

cmp al,00000101b ; b. CF= ZF= SF=

mov al,00000101b

cmp al,00000111b ; c. CF= ZF= SF=

Answer:

a. Test operation is simply a bitwise AND operation. i.e

00001111 & 00000010 it give 00000010.

Now the effect on thye flags is

CF=0 (because it is a logical operation , not an arthimetic operatio)

ZF=0 (the result is non zero.)

SF=0 (the resultant MSB is off)

b. CMP (it is same as Subtarction)

00000110b - 00000101b = 00000001b

CF=0 (we don’t have any carry or borrow)

ZF=0 (the result is non zero.)

SF=0 (the resultant MSB is off)

c.   CMP (it is same as SUB)

00000101b - 00000111b = 11111110 (5-7 = -2)

CF=1 (borrow unto the MSB)

ZF=0

SF=1 (we got a negative result so the MSB is on)

2. What will be the final value in EDX after this code executes?

mov edx,1

mov eax,7FFFh

cmp eax,8000h

jl L1

mov edx,0

L1:

Answer:

edx is initialized to 1

jb is an unsigned operation and 7FFFh is below the 8000h , so here the jump will be taken , then skipping the mov edx,0. Then the final value in edx will be 1.

3. Implement the following pseudocode in assembly language. Use short-circuit evaluation

and assume that val1 and X are 32-bit variables.

if( val1 > ecx ) AND ( ecx > edx )

X = 1

else

X = 2;

Answer:

cmp val1,ecx

jna M1

cmp ecx,edx

jna M1

mov X,1

jmp next

M1: mov X, 2

next: