Busan IT/Assembly2015. 11. 2. 08:52

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

Repeat Prefixes and More String Instructions

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

 

Repeat Prefixes and More String Instructions

 

p/239

 

 

'rep' 명령어는 ‘ecx’ 레지스터를 카운터로 사용한다.

 

'rep'를 풀어서 쓰면 다음과 같다.

 

rep movsb

 

jecxz endCopy ;skip loop if count is zero

 

copy: movsb ;move 1 character

loop copy ;decrement count and continue

 

endCopy:

'ecx' 값은 'rep' 명령어 operand가 수행되기 전에 0인지 확인되고 'ZF'플래그는 operand가 수행되고 나서 검사가 된다.

 

첫 번째 문자가 같지 않으면 'ecx'값과 ‘ZF' 값이 동시에 0이 아님으로 ’repz'명령어는 멈추게 된다.

 

//‘repz’/‘repe’는 같은 명령어이다.

교재 p/244 String search program



[sca.asm]


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

.STACK 4096

.DATA
prompt1    BYTE   "String to search? "0
prompt2    BYTE   cr, Lf, "Key to search for? "0
target    BYTE  80 DUP (?)
key    BYTE  80 DUP (?)
trgtLength  DWORD  ?
KeyLength  DWORD  ?
lastPosn  DWORD  ?
failure    BYTE  cr, Lf, Lf, "The key does not appear in the string.", cr, Lf, 0
success    BYTE  cr, Lf, Lf, "The key appear in the string."
position  BYTE  11 DUP (?)
    BYTE  " in the string.", cr, Lf, 0

PUBLIC _start
.CODE

_start:    output   prompt1  ;
    input  target,  80 ;
    lea  eax,  target ;
    push  eax ;
    call  strlen ;
    mov  trgtLength,  eax;
    output  prompt2 ;
    input   key, 80 ;
    lea  eax,  key ;
    push  eax ;
    call  strlen ;
    mov  keyLength, eax ;

    mov  eax, trgtLength ;
    sub  eax, keyLength ;
    inc   eax ;
    mov lastPosn,  eax ;
    cld

    mov  eax, 1 ;

whilePosn:  cmp  eax, lastPosn ;
    jnle  endWhilePosn ;

    lea  esi, target ;
    add   esi, eax ;
    dec  esi ;
    lea  edi, key ;
    mov  ecx, keyLength ;
    repe  cmpsb ;
    jz  found ;
    inc  eax ;
    jmp  whilePosn ;
endWhilePosn:
    output  failure ;
    jmp  quit ;

found:    dtoa  position, eax ;
    output   success ;
quit:
    INVOKE ExitProcess,  0 ;

strlen    PROC  NEAR32 ;

    push  ebp ;
    mov  ebp, esp ;
    pushf ;
    push  ebx ;
    sub  eax, eax ;
    mov  ebx,  [ebp+8] ;

whileChar:  cmp  BYTE PTR [ebx], 0 ;
    je  endWhileChar ;
    inc   eax ;
    inc  ebx ;
    jmp  whileChar ;

endWhileChar:
    pop  ebx ;
    popf ;
    pop  ebp ;
    ret  4

strlen    ENDP

    END

 

 

 

 


반응형
Posted by newind2000