Busan IT/Assembly2015. 10. 29. 17:35

==================================Outline====================================

String Operation

----------------------------------------------------------------------------

 

String Operation

 

교재 p/232

 

문자열과 관련된 명령어는 5개가 있다.

 

movs(move string)

cmps(compare string)

scas(scan string)

stos(store string)

lods(load string)

 

1byte 단위로 명령어를 내릴 때는 뒤에 첨자 'b'가 붙고, 2byte‘w', 4byte’d'가 붙는다.

 

어셈블리에서의 string은 문자열이 아닌 배열을 뜻한다.

문자열을 사용할 때는 레지스터 'ESI'와 'EDI'가 사용된다.

 

ESI = Extended Source Index

EDI = Extended Destination Index

 

'movs'명령어는 메모리에서 메모리로 데이터를 복사함으로 4사이클이 소모된다.

  

 

 

'movs' 명령어는 한 번에 한 단위 원소만을 조작시키고 명령어가 수행되고 나면 'esi'‘edi'는 조작 단위만큼 이동하게 되는데 이 때 ’EFL‘레지스에 ’DF‘ 플래그가 0일 때는 주소 값이 증가하고 1일 때는 주소 값이 감소하게 되는데 'DF'플래그의 값은 'cld', 'std'명령어로 조작할 수 있다.

문자열을 복사하는 함수를 만들어보자.

 

//함수를 사용하고 나서는 사용하기전의 레지스터 상태로 원상복구 시켜 주어야 한다.

 

교재 p/234


[strcpy.asm]

.386
.MODEL FLAT
ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD
INCLUDE io.h
cr  equ   0dh
Lf  equ   0ah

.STACK 4096

.DATA
prompt    BYTE  cr, Lf, "Original string? ",0
stringIn  BYTE  80 DUP (?)
display    BYTE  cr, Lf, "Your string was...", cr, Lf
stringOut  BYTE  80 DUP  (?)

.CODE
_start:  output   prompt
  input  stringIn,   80
  lea  eax,    stringOut
  push  eax
  lea  eax, stringIn
  push  eax
  call   strcopy
  output  display
  INVOKE  ExitProcess,  0

PUBLIC  _start

strcopy    PROC NEAR32

  push  ebp
  mov  ebp, esp

  push edi
  push esi
  pushf

  mov  esi, [ebp+8]
  mov  edi, [ebp+12]
  cld

whileNoNull:
  cmp  BYTE PTR [esi],  0
  je  endWhileNoNull
  movsb
  jmp  whileNoNull

endWhileNoNull:
  mov BYTE PTR [edi], 0

  popf
  pop  esi
  pop  edi
  pop  ebp
  ret  8

strcopy  ENDP

  END

 

 


반응형
Posted by newind2000