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

ASM 프로시저를 이용한 레지스터값 출력!!

by 알 수 없는 사용자 2013. 9. 16.
728x90
반응형

 

☞ 프로시저를 이용한 context 스위칭 프로그램 만들기

    - 현재 레지스터값 출력!!

C 소스

#include <stdio.h>


typedef struct _context
{


 int efl;
 int eip;
 int edi;
 int esi;
 int ebp;
 int esp; 
 int ebx;
 int edx;
 int ecx;
 int eax;

}context ;

 

void linux(context *);
void Printf_REG(context *);

 

int main()
{

 context stReg = {   
                           0x12345678,
                           0x12345678,
                           0x12345678,
                           0x12345678,
                           0x12345678,
                           0x12345678,
                           0x12345678,
                           0x12345678,
                           0x12345678,
                           0x12345678
                      };

 
       linux(&stReg);

       Printf_REG(&stReg);
 

       return 0;
}

 

 

void Printf_REG(context *stpReg)
{

 printf("EAX : 0x%08X  ECX : 0x%08X \n", stpReg->eax, stpReg->ecx);
 printf("EDX : 0x%08X  EBX : 0x%08X \n", stpReg->edx, stpReg->ebx);
 printf("ESP : 0x%08X  EBP : 0x%08X \n", stpReg->esp, stpReg->ebp);
 printf("ESI : 0x%08X  EDI : 0x%08X \n", stpReg->esi, stpReg->edi);
 printf("EIP : 0x%08X  EFL : 0x%08X \n", stpReg->eip, stpReg->efl);

 

}

 

 

 

 

ASM 프로시저

.386
.MODEL FLAT

PUBLIC _linux
.CODE
_linux      PROC      NEAR32

  

  push ebp                             ; Entry code
  mov ebp, esp
  
  pushfd                                     efl 백업  
    
  mov       esp, [ebp+8]
  add       esp, 40                       ;esp를 stReg변수 멤버 eax 위치로 이동

 

  pushad                                   ;레지스터 값 push
  push     [ebp+4]                       ;eip(return address)
  push     [ebp-4]                       ;push efl

 

  add       esp,  24                       ;구조체의 esp 멤버 위치로 가서

  mov       eax, ebp
  add       eax, 12                         ; ebp로 부터 esp를 찾아서  
  push     eax                             ; main:: esp push

  push    [ebp]                           ; main:: ebp push


  mov      esp, ebp
  sub      esp, 4                           ; esp 원래 위치로
  
  popfd                                       ; efl 복원        

  mov     esp, ebp                       ; Exit code
  pop     ebp
  ret

 

 

_linux  ENDP 

END

 

☞ 결과

 

 

 

 

 

 

 

728x90