BoB/합격 후 활동

Intel x86 Insturction Set Architecture - 4

zooonique 2019. 11. 4. 21:14
반응형

[Conditional Jumps]

 

IA-32(Intel Architecture) instruction set에서는 if-then-else와 같은 high-level logic 구조가 없다.

 

하지만, 비교 조합과 논리구조 보충을 위한 점프를 사용할 수 있다.

 

우선, CMP, AND, SUB와 같은 명령어들은 CPU Flags들을 수정한다.

 

그 후, Conditional Jumps 명령어는 플래그를 테스트하고 그것에 따라 수행 흐름을 바꾼다.

 

[Jcond instruction]

 

Conditional jump 명령어는 특정한 레지스터나 플래그 상태변화가 있으면 label로 분기한다.

 

  1. Based on specific flag values
  2. Based on equality between operands
  3. Based on Comparisons of unsigned operands
  4. Based on Comparisons of signed operands
  • Jumps based on specific flags

 

  • Jumps based on equality

 

  • Jumps based on unsigned comparisons

왼쪽이 기준!

 

  • Jumps based on signed comparions

 

Examples

 

 

ax가 bx보다 작거나 같으면 Next, ax가 bx보다 크면 Large에 ax를 대입한다.

 

bx가 ax보다 크거나 같으면 Next, bx가 ax보다 작으면 Small에 bx를 대입한다.

 

[BT(Bit Test) instruction]

 

오퍼랜드로부터 bit n을 복사하여 Carry Flag에 넣는다.

 

Syntax : BT bitBase, n

 

Example

AX 레지스터 안에 bit9가 1이면 L1로 점프한다.

 

  • BTC bitbase, n : bit test and complement(보수)
  • BTR bitbase, n :  bit test and reset(clear)
  • BTS bitbase, n : bit test and set

 

 

[Conditional Loops]

 

[LOOPNZ, LOOPNE]

 

ECX ← ECX - 1;

 

ECX != 0 and ZF = 0이면 destination으로 jump 한다.

 

[Conditional Structures]

 

Examples

 

 


 

 

 

 

 


 

Exercise

while(eax < ebx)
{

	eax++;
    
    if(ebx==ecx)
    	x=2;
        
    else
    	x=3;
        
}

 

_while:

	cmp eax, ebx
    jae _endwhile
    inc eax
    cmp ebx,ecx
    jne _else
    mov x, 2
    jmp _while
    
_else:
	mov x, 3
    jmp _while
    
_endwhile:

 

 

[Table-Driven Selection]

 

Switch case문과 같이 다수 개로 분기가 발생하는 선택 구조를 대치하기 위하여 테이블을 이용하는 구조이다.

 

label이나 procedure offset과 lookup values가 포함된 표를 만들고, 그 표를 찾기 위해 루프를 사용한다.

 

[1단계]

lookup values나 procedure offsets가 포함된 테이블을 만든다.

 

[2단계]

loop를 이용해 테이블을 검색한다. 일치하는 것이 발견되면, 현재 테이블 엔트리에서 오프셋이 저장된 절차를 호출한다.

 

반응형