티스토리 뷰

CS

Addressing Modes

ellie.strong 2020. 4. 21. 00:08
728x90

Operand Addressing 방법

mov eax, 1		// OK 
mov [eax], 1		// OK
mov eax, ebx		// OK
mov eax, [ebx]		// OK
mov [eax], [ebx]	// ERROR M-to-M 타입은 없음!!

operand의 형태에 따라 addressing 방법이 달라진다. 이때 M-to-M(메모리-to-메모리) 타입은 불가능하다. 메모리 접근시 시스템 버스를 타게되는데, 이 시스템 버스는 한번에 하나씩만 사용이 가능하다. 따라서 M-to-M 타입의 경우 동시에 operand 두개가 동시에 시스템 버스를 사용해야하므로 불가능하다. 

#include <stdio.h>

int main() {
	int i = 0;
    __asm {
    	mov ss:[esp], 0x12345678
        mov eax, [esp]	// 다음과 같이 코드를 간단히 줄일 수 있지 않을까?
        mov i, eax	// -> mov i, [esp]
    }
	
    printf("%x\n", i);
    
    getchar();
    return 0;
}

위의 예제에서 이러한 의문이 들 수 있다. 하지만 다음과 같이 코드를 줄여서 작성할 경우 "피연산자의 형식이 틀렸습니다." 혹은 "operand의 type을 지원하지 않습니다." 등의 에러메시지를 만나게 된다. 이는 변수 i가 지역 변수로 stack(memory)에 존재하기 때문이다. 따라서 M-to-M 타입이 되어버려 에러가 발생하게된다. 

 

Addressing Modes

Addressing mode 구분 형식
Register Addressing Reg Register
Immediate Addressing Imm Immediate
Direct Addressing Mem [Disp]
Register Indirect Addressing Mem [Base]
Based Addressing Mem [Base + Disp]
Indexed Addressing Mem [Index + Disp]
Based-Indexed Addressing Mem [Base + Index + Disp]
Based-Indexed-Scaled Addressing Mem [Base + (Index*Scale) + Disp]

 

Register Addressing

레지스터 값을 직접 지정하는 방식으로 처리 속도가 가장 빠르다. 

mov eax, ebx

 

Immediate Addressing

immediate(상수) 값을 레지스터에 직접 지정하는 방식

mov eax, 0x12345678

 

Direct Addressing

segment:[메모리주소] 형식

mov eax, ss:[0012fe90h]

80386이후 Segment Selector를 Base 주소 계산용으로 사용하지 않지만, Segment Selector 부분 없이 아래의 두번째 줄 값이 작성하면 Immediate Addressing으로 해석해버린다. 

mov eax, 0012FE90h
mov eax, [0012FE90h] // 위의 코드와 같게 해석된다. 

 

Register Indirect Addressing

[Base Register] 또는 Segment:[Base Register] 형식

mov eax, [esp]
mov eax, ss:[esp]
mov eax, ds:[esp]

80386이후 더 이상 Segment Selector를 사용하지 않아도 되지만, Segment Selector 사용 여부에 따라 Instruction은 달라진다. 따라서 위의 세가지 경우의 코드의 경우 같은 결과 값을 도출하지만 인코딩되는 명령어는 다른다. 

원래는 정해진 Segment Selector를 사용해야 하지만 요즘에는 그 값이 모두 0으로 초기화 돼있어서 cs, ds, es, ss 중 아무거나 사용해도 된다. 

 

Based Addressing

[Base + Disp] 형식

mov eax, [ebx+20h]
mov eax, [ebx]+20h
mov eax, 20h[ebx]

위와 같이 다른 형식으로 표현이 가능하지만 최종적으로 [Base+Disp] 형식으로 변환되어 인코딩된다. 

 

Indexed Addressing

[Index + Disp] 형식

mov eax, [esi+20h]
mov eax, [esi]+20h
mov eax, 20h[esi]

Index의 경우 esi 레지스터를 사용한다는 것에 주의하자!!

 

Based-Indexed Addressing

[Base + Index + Disp] 형식

mov eax, [ebx+esi+20h]
mov eax, [ebx][esi+20h]
mov eax, [ebx][esi]+20h
mov eax, 20h[ebx][esi]

 

Based-Indexed-Scaled Addressing

[Base + (Index*Scale) + Disp] 형식

mov eax, [ebx+esi*2+20h]
mov eax, [ebx][esi*2+20h]
mov eax, [ebx][esi*2]+20h
mov eax, 20h[ebx][esi*2]
728x90

'CS' 카테고리의 다른 글

[CS/알고리즘] 정렬 (Sorting)  (0) 2021.03.05
오류 (Error), 결함 (Defect), 장애 (Failure)  (0) 2020.08.04
Assembly Intel Manual  (0) 2020.04.21
어셈블리어( Assembly ) 기초  (0) 2020.04.20
80x86 프로세서 구조  (0) 2020.04.03
댓글
공지사항
최근에 올라온 글