Assembly Language for x86 Processors --- 4.2 After-Class Exercises Answers

4.2 Addition and Subtraction

1 Is it possible to set the Overflow flag if you add a positive integer to a negative integer? (If you add a positive integer to a negative integer, can you set the overflow flag?)

Answer: No

2 Will the Overflow flag be set if you add a negative integer to a negative integer and produce a positive result? (If you add a negative integer to a negative integer and produce a positive result, will the overflow flag be set?)

Answer: Yes

3 Is it possible for the NEG instruction to set the Overflow flag?

Answer: Yes

4 Is it possible for both the Sign and Zero flags to be set at the same time?

Answer: No

5 Use the following data:
.data
val1 BYTE 10h
val2 WORD 8000h
val3 DWORD 0FFFFh
val4 WORD 7FFFh

a. Write an instruction that subtracts val3 from EAX.
b. Write an instruction that increments val2.
c. Write instructions that subtract val4 from val2.
d. If val2 is incremented by 1 using the ADD instruction, what will be the values of the Carry and Sign flags?
e. If val4 is incremented by 1 using the ADD instruction, what will be the values of the Overflow and Sign flags?

答 :
a. sub eax,val3

b. inc val2

c. mov ax,val4
sub val2,ax

d. CF = 0
SF = 1

e. OF = 1
SF = 1

6 Implement the following expression in assembly language: AX = (-val2 + BX) - val4.


Ax mov ax, val2
neg ax
add ax, bx
sub ax, val4.

7 Write a sequence of two instructions that set both the Carry and Overflow flags at the same time.

答 :
mov al, 80h
add al, 80h

8 Where indicated, write down the values of the Carry, Sign, Zero, and Overflow flags after each instruction has executed:

mov ax,7FF0h
add al,10h
; a. CF = SF = ZF = OF =

add ah,1
; b. CF = SF = ZF = OF =

add ax,2
; c. CF = SF = ZF = OF =

答 :
a. CF = 1 SF = 0 ZF = 1 OF = 0
b. CF = 0 SF = 1 ZF = 0 OF = 1
c. CF = 0 SF = 1 ZF = 0 OF = 0

Guess you like

Origin blog.csdn.net/weixin_43574277/article/details/105427729