2009. 11. 3. 12:31
STST와 LDST
2009. 11. 3. 12:31 in 공부합시다/어셈블리어
STST 와 LDST 구현하기
file : main.c
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 LDST(Context *);
void STST(Context *);
int main()
{
Context stat;
int i;
memset(&stat, 0,sizeof(Context));
printf("시작\n");
printf("efl = [%08X]\n", stat.efl);
printf("eip = [%08X]\n", stat.eip);
printf("edi = [%08X]\n", stat.edi);
printf("esi = [%08X]\n", stat.esi);
printf("ebp = [%08X]\n", stat.ebp);
printf("esp = [%08X]\n", stat.esp);
printf("ebx = [%08X]\n", stat.ebx);
printf("edx = [%08X]\n", stat.edx);
printf("ecx = [%08X]\n", stat.ecx);
printf("eax = [%08X]\n", stat.eax);
STST(&stat);
printf("\n");
printf("여기는 과거\n");
printf("efl = [%08X]\n", stat.efl);
printf("eip = [%08X]\n", stat.eip);
printf("edi = [%08X]\n", stat.edi);
printf("esi = [%08X]\n", stat.esi);
printf("ebp = [%08X]\n", stat.ebp);
printf("esp = [%08X]\n", stat.esp);
printf("ebx = [%08X]\n", stat.ebx);
printf("edx = [%08X]\n", stat.edx);
printf("ecx = [%08X]\n", stat.ecx);
printf("eax = [%08X]\n", stat.eax);
printf("0 == Exit, 1 == First\n");
scanf("%d", &i);
if(1 == i)
{
LDST(&stat);
printf("실패!!!");
}
return 0;
}
file : asm.asm
segment .text
global _STST
global _LDST
_STST:
push ebp
mov ebp, esp
pushf
mov esp, [ebp+8] ;context 의 시작 주소
add esp, 40 ;context 의 끝 주소
pusha ; ax, cx, dx, bx, sp, bp, si, di
mov eax, [ebp+4] ;eax = eip(return address)
push eax ;context.eip = eip
mov eax, [ebp-4] ;eax = efl
push eax ;context.efl = efl
mov eax, [ebp] ;eax = before ebp
mov [esp+16], eax ;context.ebp = ebp(before)
mov eax, ebp
add eax, 8 ;eax = before esp
mov [esp+20], eax ;context.esp = esp(before)
mov esp, ebp
pop ebp
ret
_LDST:
mov esp, [esp+4] ;esp = address of context
popf ;efl = context.efl
pop eax ;eax = context.eip
mov ebx, esp ;ebx = esp
mov esp, [esp+12] ;esp = context.esp
push eax ;esp(before) = eip
mov esp, ebx ;esp(before) = esp(now)
popa ;di, si, bp, bx, dx, cx, ax
mov esp, [esp-20] ;esp = context.esp(before)
sub esp, 4 ;esp(before) = eip(before)
ret
file : main.c
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 LDST(Context *);
void STST(Context *);
int main()
{
Context stat;
int i;
memset(&stat, 0,sizeof(Context));
printf("시작\n");
printf("efl = [%08X]\n", stat.efl);
printf("eip = [%08X]\n", stat.eip);
printf("edi = [%08X]\n", stat.edi);
printf("esi = [%08X]\n", stat.esi);
printf("ebp = [%08X]\n", stat.ebp);
printf("esp = [%08X]\n", stat.esp);
printf("ebx = [%08X]\n", stat.ebx);
printf("edx = [%08X]\n", stat.edx);
printf("ecx = [%08X]\n", stat.ecx);
printf("eax = [%08X]\n", stat.eax);
STST(&stat);
printf("\n");
printf("여기는 과거\n");
printf("efl = [%08X]\n", stat.efl);
printf("eip = [%08X]\n", stat.eip);
printf("edi = [%08X]\n", stat.edi);
printf("esi = [%08X]\n", stat.esi);
printf("ebp = [%08X]\n", stat.ebp);
printf("esp = [%08X]\n", stat.esp);
printf("ebx = [%08X]\n", stat.ebx);
printf("edx = [%08X]\n", stat.edx);
printf("ecx = [%08X]\n", stat.ecx);
printf("eax = [%08X]\n", stat.eax);
printf("0 == Exit, 1 == First\n");
scanf("%d", &i);
if(1 == i)
{
LDST(&stat);
printf("실패!!!");
}
return 0;
}
file : asm.asm
segment .text
global _STST
global _LDST
_STST:
push ebp
mov ebp, esp
pushf
mov esp, [ebp+8] ;context 의 시작 주소
add esp, 40 ;context 의 끝 주소
pusha ; ax, cx, dx, bx, sp, bp, si, di
mov eax, [ebp+4] ;eax = eip(return address)
push eax ;context.eip = eip
mov eax, [ebp-4] ;eax = efl
push eax ;context.efl = efl
mov eax, [ebp] ;eax = before ebp
mov [esp+16], eax ;context.ebp = ebp(before)
mov eax, ebp
add eax, 8 ;eax = before esp
mov [esp+20], eax ;context.esp = esp(before)
mov esp, ebp
pop ebp
ret
_LDST:
mov esp, [esp+4] ;esp = address of context
popf ;efl = context.efl
pop eax ;eax = context.eip
mov ebx, esp ;ebx = esp
mov esp, [esp+12] ;esp = context.esp
push eax ;esp(before) = eip
mov esp, ebx ;esp(before) = esp(now)
popa ;di, si, bp, bx, dx, cx, ax
mov esp, [esp-20] ;esp = context.esp(before)
sub esp, 4 ;esp(before) = eip(before)
ret
'공부합시다 > 어셈블리어' 카테고리의 다른 글
[어셈블리어] push와 pop의 구분동작 (0) | 2009.05.14 |
---|---|
[어셈블리어] PUSHA와 POPA (0) | 2009.05.14 |
[어셈블리어] 데이터 전송 명령어 mov, movzx, movsx (0) | 2009.05.11 |
[어셈블리어] 곱셈 - MUL, IMUL (0) | 2009.05.08 |
[어셈블리어] 나눗셈 (0) | 2009.05.06 |