본문 바로가기
BoB/합격 후 활동

Intel x86 Insturction Set Architecture - 2

by zooonique 2019. 10. 31.
반응형

Arithmetic Instructions

 

[INC, DEC]

 

  • Destination operand에서 1을 더하거나 빼는 명령어
  • INC destination
    • Logic : destination <- destination + 1
  • DEC destination
    • Logic : destination <- destination - 1

 

inc al을 하면, 하위 8비트 FF에만 +1을 하기때문에 al이 00이 된다.

 

 

 

[ADD, SUB]

 

  • ADD destination, source
    • Logic : destination ← destination + source
  • SUB destination, source
    • Logic : destination ← destination - source

 

 

[NEG]

 

2의보수법을 적용하는 것이다.

 

 

[Flags Affected by Arithmetic]

 

ALU는 산수 연산의 영향을 받는 상태 플래그를 가지고있다. (destination operand의 내용 기반이다.)

 

본질적인 플래그는 다음과 같다.

 

Zero Flag - 연산결과가 0일 때

 

Sign Flag - 연산결과가 음수일 때

 

Carry Flag - 부호없는 숫자 연산 결과가 비트 범위를 넘었을 때

 

Overflow Flag - 부호있는 숫자 연산 결과가 비트 범위를 넘었을 때

두개의 정수가 더해질 때, 오버플로우 플래그가 설정되는 경우

  1. 두 양수의 합이 음수가 나오는 경우
  2. 두 음수의 합이 양수가 나오는 경우

 

MOV명령어는 Flags에 영향을 주지않는다.

 

 

[Hardware Viewpoint]

 

모든 CPU 명령어는 signed와 unsigned를 같은 방식으로 동작한다.

 

CPU는 signed와 unsigned를 구별하지 못하기 때문에 프로그래머는 정확한 데이터타입을 명령어와 함께 써야한다.

 

 

[CF, OF]

 

ADD 명령어에서 OF와 CF

  • CF = (carry out of the MSB)
  • OF = (carry out of the MSB) ^ (carry into the MSB)

SUB 명령어에서 OF와 CF

  • source를 NEG하고 destination에 더한다.
  • CF = INVERT (carry out of the MSB)
  • OF = (carry out of the MSB) ^ (carry into the MSB)
반응형