본문 바로가기
코스웨어/13년 스마트컨트롤러

20131014 어셈블리 _이수현

by 알 수 없는 사용자 2013. 10. 14.
728x90
반응형

ASM+C    :    어셈블리와 C언어 결합

컴파일

ml /c /coff /Zi strsch.asm

cl /c main_strsch.c

cl main_strsch.obj strsch.obj



내용

- 입력받은 단어중 해당단어의 위치를 찾는 함수를 어셈블리로 만든다





<main_strsch.c>


#include <stdio.h>

extern int  strsch (char *, char*);

int main ()
{
  char  src[80];
  char  key[80];
  int    iRet;

  printf (" String to search?");
  scanf ("%s", src);

  printf (" Key to search for?"); 
  scanf ("%s", key);

  iRet = strsch (src,key);

  if (0 == iRet)
  {
    printf (" The key does not appear in the string.\n");
  }
  else
  {
    printf (" The key appears at position  [%d] in the string\n", iRet);
  }

  return 0;
}





<strsch.asm>



2013.10.14

.386
.MODEL  FLAT

;INCLUDE  io.h
PUBLIC  _strsch

.STACK  4096

.DATA
trgtLength  DWORD ?
keyLength  DWORD ?
lastPosn  DWORD ?

.CODE
_strsch  PROC  NEAR32
;entry code
      PUSH  EBP
      MOV    EBP, ESP

      mov    eax, [ebp+8]    ; find length of string
      push  eax        ; length parameter
      call  strlen
      mov    trgtLength, eax  ; save length of ebp+8
      mov    eax, [ebp+12]    ; find length of string
      push  eax    
      call  strlen
      mov    keyLength, eax  ; save length of ebp+12

; calculate last position of ebp+8 to check
      mov    eax,trgtLength
      sub    eax,keyLength
      inc    eax        ; trgtLength - ebp+12Length + 1
      mov    lastPosn, eax
      cld            ; left to right comparison
      mov    eax,1      ; starting position

whilePosn:  cmp    eax,lastPosn  ; position <= last_posn?
      jnle  endWhilePosn  ; exit if past last position
      mov    esi,[ebp+8]    ; address of ebp+8 string
      add    esi,eax      ; add position
      dec    esi        ; address of position to check
      mov    edi,[ebp+12]      ; address of ebp+12
      mov    ecx,keyLength  ; number of positions to check
      repe  cmpsb      ; check
      jz    found      ; exit on success
      inc    eax        ; increment position
      jmp    whilePosn    ; repeat
endWhilePosn:
      mov    eax,0
found:
; exit code
      MOV    ESP, EBP
      POP    EBP
      RET  

_strsch  ENDP

strlen PROC NEAR32
; find length of string whose address is passed on stack
; length returned in EAX
      push  ebp        ; establish stack frame
      mov    ebp, esp
      pushf          ; save flags
      push  ebx        ; and EBX
      sub    eax, eax    ; length := 0
      mov    ebx, [ebp+8]  ; address of string

whileChar:  cmp    BYTE PTR [ebx], 0 ; null byte?
      je    endWhileChar  ; exit if so
      inc    eax        ; increment length
      inc    ebx        ; point at next character
      jmp    whileChar    ; repeat

endWhileChar:
      pop    ebx        ; restore registers and flags
      popf
      pop    ebp
      ret    4        ; return, discarding parameter

strlen    ENDP
END



728x90