Search results for 'Code/assembly'

  1. 2010.09.07 -- move 명령어
  2. 2009.08.09 -- 기본 색인 주소 지정 모드 4

move 명령어

2010. 9. 7. 02:29

http://faydoc.tripod.com/cpu/movsd.htm 에 본문이 있슴.

Code Mnemonic Description
A4 MOVS m8, m8 Move byte at address DS:(E)SI to address ES:(E)DI
A5 MOVS m16, m16 Move word at address DS:(E)SI to address ES:(E)DI
A5 MOVS m32, m32 Move doubleword at address DS:(E)SI to address ES:(E)DI
A4 MOVSB Move byte at address DS:(E)SI to address ES:(E)DI
A5 MOVSW Move word at address DS:(E)SI to address ES:(E)DI
A5 MOVSD Move doubleword at address DS:(E)SI to address ES:(E)DI

Moves the byte, word, or doubleword specified with the second operand (source operand) to the location specified with the first operand (destination operand). Both the source and destination operands are located in memory. The address of the source operand is read from the DS:ESI or the DS:SI registers (depending on the address-size attribute of the instruction, 32 or 16, respectively). The address of the destination operand is read from the ES:EDI or the ES:DI registers (again depending on the address-size attribute of the instruction). The DS segment may be over-ridden with a segment override prefix, but the ES segment cannot be overridden.

대충 메모리에 있는 데이터를  바이트,워드, 더블 워드 만큼 ESI(src address)에서 EDI(dst address)로 옮긴단 얘기. 계속 설명을 보면..

After the move operation, the (E)SI and (E)DI registers are incremented or decremented automatically according to the setting of the DF flag in the EFLAGS register. (If the DF flag is 0, the (E)SI and (E)DI register are incremented; if the DF flag is 1, the (E)SI and (E)DI registers are decremented.) The registers are incremented or decremented by 1 for byte operations, by 2 for word operations, or by 4 for doubleword operations.

Move 연산 이후에 ESI와 EDI 레지스터는 EFLAGS 레지스터의 DF 플래그에 따라서 값이 증가하거나 감소되거나 한다. DF 가 0 이면 증가, 1이면 감소, 사이즈에 따라서 연산되는 크기도 달라지고.. 마지막엔 이렇게 나와있다.

The MOVS, MOVSB, MOVSW, and MOVSD instructions can be preceded by the REP prefix

MOVS 류 명령어 앞에는 REP 가 올 수 있단다.
같은 사이트에서 REP를 찾아보면..


Code Mnemonic Description
F3 6C REP INS r/m8, DX Input (E)CX bytes from port DX into ES:[(E)DI]
F3 6D REP INS r/m16, DX Input (E)CX words from port DX into ES:[(E)DI]
F3 6D REP INS r/m32, DX Input (E)CX doublewords from port DX into ES:[(E)DI]
F3 A4 REP MOVS m8, m8 Move (E)CX bytes from DS:[(E)SI] to ES:[(E)DI]
F3 A5 REP MOVS m16, m16 Move (E)CX words from DS:[(E)SI] to ES:[(E)DI]
F3 A5 REP MOVS m32, m32 Move (E)CX doublewords from DS:[(E)SI] to ES:[(E)DI]
F3 6E REP OUTS DX, r/m8 Output (E)CX bytes from DS:[(E)SI] to port DX
F3 6F REP OUTS DX, r/m16 Output (E)CX words from DS:[(E)SI] to port DX
F3 6F REP OUTS DX, r/m32 Output (E)CX doublewords from DS:[(E)SI] to port DX
F3 AC REP LODS AL Load (E)CX bytes from DS:[(E)SI] to AL
F3 AD REP LODS AX Load (E)CX words from DS:[(E)SI] to AX
F3 AD REP LODS EAX Load (E)CX doublewords from DS:[(E)SI] to EAX
F3 AA REP STOS m8 Fill (E)CX bytes at ES:[(E)DI] with AL
F3 AB REP STOS m16 Fill (E)CX words at ES:[(E)DI] with AX
F3 AB REP STOS m32 Fill (E)CX doublewords at ES:[(E)DI] with EAX
F3 A6 REPE CMPS m8, m8 Find nonmatching bytes in ES:[(E)DI] and DS:[(E)SI]
F3 A7 REPE CMPS m16, m16 Find nonmatching words in ES:[(E)DI] and DS:[(E)SI]
F3 A7 REPE CMPS m32, m32 Find nonmatching doublewords in ES:[(E)DI] and DS:[(E)SI]
F3 AE REPE SCAS m8 Find non-AL byte starting at ES:[(E)DI]
F3 AF REPE SCAS m16 Find non-AX word starting at ES:[(E)DI]
F3 AF REPE SCAS m32 Find non-EAX doubleword starting at ES:[(E)DI]
F2 A6 REPNE CMPS m8, m8 Find matching bytes in ES:[(E)DI] and DS:[(E)SI]
F2 A7 REPNE CMPS m16, m16 Find matching words in ES:[(E)DI] and DS:[(E)SI]
F2 A7 REPNE CMPS m32, m32 Find matching doublewords in ES:[(E)DI] and DS:[(E)SI]
F2 AE REPNE SCAS m8 Find AL, starting at ES:[(E)DI]
F2 AF REPNE SCAS m16 Find AX, starting at ES:[(E)DI]
F2 AF REPNE SCAS m32 Find EAX, starting at ES:[(E)DI]

Repeats a string instruction the number of times specified in the count register ((E)CX) or until the indicated condition of the ZF flag is no longer met. The REP (repeat), REPE (repeat while equal), REPNE (repeat while not equal), REPZ (repeat while zero), and REPNZ (repeat while not zero) mnemonics are prefixes that can be added to one of the string instructions. The REP prefix can be added to the INS, OUTS, MOVS, LODS, and STOS instructions, and the REPE, REPNE, REPZ, and REPNZ prefixes can be added to the CMPS and SCAS instructions. (The REPZ and REPNZ prefixes are synonymous forms of the REPE and REPNE prefixes, respectively.) The behavior of the REP prefix is undefined when used with non-string instructions

ECX  또는 ZF 플래그 조건이 충족되는 만큼 문자열 명령을 반복한다.

따라서 REP  MOVSD 일때 ECX가 10 이면, ESI 레지스터의 값을 EDI로 10 번 옮기게 된다.

매번 까먹고 구글링에 지쳐서.. 적어놓으면 좀 나을려나.


'Code > assembly' 카테고리의 다른 글

기본 색인 주소 지정 모드  (4) 2009.08.09

badcob Code/assembly

기본 색인 주소 지정 모드

2009. 8. 9. 04:31


아래의 코드는 어느 바이너리의 특정 함수를 objdump 로 디스어셈블한 것이다.

사용자 삽입 이미지


 0x8048508에 있는 구문을 보자

     lea    (%edx, %eax, 1), %eax
   
  주소를 eax 에 넣긴 하는거 같은데 어떻게 계산이 이루어지는 지 모르겠다. 도와줘요 IDA~~
  
사용자 삽입 이미지
 

AT&T 문법에 맞춰서 디스어셈블되면  

lea   eax, [edx+eax]  로 바뀐 것을 볼 수 있다.

구글링을 해보니 리눅스 어셈블러: GAS와 NASM 비교 라는 글에서 다음과 같은 설명을 찾을 수 있었다.



NASM: mov al, byte [ebx + esi]

GAS: movb (%ebx, %esi, 1), %al

이런 방식은 기본 색인 주소 지정 모드다. 여기서 구성요소가 세 개 등장한다. 첫째는 기본 주소, 둘째는 색인 레지스터, 셋째는 승수(乘數)다. 메모리 위치에서 접근하는 바이트 숫자를 결정하기란 불가능하므로 주소를 지정한 메모리 총량을 찾아내기 위한 방법이 필요하다. NASM은 byte 연산자를 사용해 자료에서 한 바이트가 이동했음을 어셈블러에 알려준다. GAS에서는 니모닉에 b, w, l 접미사를 붙이는(예를 들어, movb) 동시에 승수를 사용해 이런 문제를 해결한다. 처음 보면 GAS 구문이 좀더 복잡해보일 수도 있다.

GAS에서 일반적인 기본 색인 주소 지정 형식은 다음과 같다.

%segment:ADDRESS (, index, multiplier)

또는

%segment:(offset, index, multiplier)

또는

%segment:ADDRESS(base, index, multiplier)

이 공식을 사용해 최종 주소를 계산한다.

ADDRESS or offset + base + index * multiplier.

따라서 바이트에 접근하려면 승수는 1이 되며, 워드에 접근하려면 승수는 2가 되며, 더블 워드에 접근하려면 승수가 4가 되어야 한다. 물론 NASM은 좀더 단순한 구문을 사용한다. 따라서 NASM으로 상기 공식을 풀어쓰면 다음과 같다.

Segment:[ADDRESS or offset + index * multiplier]

각각 메모리 1, 2, 4 바이트에 접근하도록 byte, word, dword 접두사를 메모리 주소 앞에 표시한다.



즉, movb (%ebx, %esi, 1), %al 구문으로 설명한다면  

ebx(address or offset) 의 값에 esi(index)를 더해서 al 레지스터로 옮긴다.
3번째 인자로 인덱스를 byte, word, dword 단위로 조정할 수 있으며 위의 코드에서는  
바이트(1) 단위로 접근하는 것을 알 수 있다.
 


'Code > assembly' 카테고리의 다른 글

move 명령어  (0) 2010.09.07

badcob Code/assembly