본문 바로가기

Assembly

6-3강 - Conditional Processing 3 (Conditional Structures 조건문)

4. Conditional Structures

 

1) IF

1> 간단한 if문 예시 1

2> Unsigned 예시

 

3> Signed 예시

2) IF문 내부의 AND 구현

'P AND Q'에서 P가 거짓이면 Q는 건너뛴다. 

 

1> 기본

 

    cmp al, bl   ; first expression...
    ja  L1
    jmp next
L1: 
    cmp bl, cl   ; second expression...
    ja  L2
    jmp next
L2:              ; both are true
    mov X, 1     ; set X to 1
next:

 

2> 더 짧은 code ("fall through"방식)

기존 code를 뒤집어서

    cmp al, bl   ; first expression...
    jbe next     ; quit if false
    cmp bl, cl   ; second expression...
    jbe next     ; quit if false
    mov X, 1     ; both are true
next:

3) OR

'P OR Q'에서 P가 참이면 Q는 건너뛴다. 

1> fall through 방식 적용

 

4) While

 

 

5) Table-Driven Selection

1> 특징

- 여러가지 중 한 case를 고를 수 있는 table 구조 (multiway selection structure)

- loop를 이용해서 검색하는 구조

- 많은 수를 비교할 때 유용하다.

2> 구성

- lookup value의 offset

- lookup value에 해당하는 label이나 procedure의 offset

3> 예시 - 1단계

lookup value와 procedure offset을 담은 table 생성

.data
CaseTable BYTE 'A' 				; lookup value (1 byte)
	DWORD Process_A 			; address of procedure (4 byte)
	EntrySize = ($ - CaseTable)		; (entry size : 5 byte)
	BYTE 'B'
	DWORD Process_B
	BYTE 'C'
	DWORD Process_C
	BYTE 'D'
	DWORD Process_D

NumberOfEntries = ($ - CaseTable) / EntrySize

4> 예시 - 2단계

반복문으로 table을 검색함 -> 찾으면 해당하는 procedure offset을 이용해 함수 호출

    mov ebx, OFFSET CaseTable   ; point EBX to the table
    mov ecx, NumberOfEntries    ; loop counter
L1: cmp al, [ebx]               ; match found?
    jne L2                      ; no: continue
    call NEAR PTR [ebx + 1]     ; yes: call the procedure
    call WriteString            ; display message
    call Crlf
    jmp L3                      ; and exit the loop
L2: add ebx, EntrySize          ; point to next entry
    loop L1                     ; repeat until ECX = 0
L3: