# include <stdio.h>

void _menu_(void)

{

char* addPrompt = "[1] addition \n";

char* subPrompt = "[2] subtraction \n";

char* mulPrompt = "[3] multiplication \n";

char* divPrompt = "[4] dividen \n";

__asm

{

push addPrompt;

call dword ptr[printf];

pop ebx;


push subPrompt;

call dword ptr[printf];

pop ebx;


push mulPrompt;

call dword ptr[printf];

pop ebx;


push divPrompt;

call dword ptr[printf];

pop ebx;

}

}

void _add_(int nParam1, int nParam2)

{

char* addPrompt = "%d + %d = %d \n";

__asm

{

mov eax, nParam1;

add eax, nParam2;

push eax;

push nParam2;

push nParam1;

push addPrompt;

call dword ptr[printf];

pop ebx;

pop ebx;

pop ebx;

pop ebx;

}

} // end of _add_ function


void _sub_(int nParam1, int nParam2)

{

char* subPrompt = "%d - %d = %d \n";

__asm

{

mov eax, nParam1;

sub eax, nParam2;

push eax;

push nParam2;

push nParam1;

push subPrompt;

call dword ptr[printf];

pop ebx;

pop ebx;

pop ebx;

pop ebx;

}

}


void _mul_(int nParam1, int nParam2)

{

char* mulPrompt = "%d x %d = %d \n";

__asm

{

mov eax, nParam1;

imul eax, nParam2;

push eax; // = %d 

push nParam2;

push nParam1;

push mulPrompt;

call dword ptr[printf];

pop ebx;

pop ebx;

pop ebx;

pop ebx;

}

}


void _div_(int nParam1, int nParam2)

{

int que = 0;

int rem = 0;

__asm

{

xor edx, edx;

mov eax, nParam1;

div nParam2;

mov rem, edx;

mov que, eax;

}

printf("remainder => %d\n", rem);

printf("queotiont => %d\n", que);

}

int main(void)

{

int v1 = 0;

int v2 = 0;

int nChoice = 0;

char* ScanfPrompt = "%d";

char* prompt1 = "v1 = ";

char* prompt2 = "v2 = ";

char* PrintF1 = "v1 => %d\n";

char* PrintF2 = "v2 => %d\n";

char* promptChoice = "Write = ";

__asm

{

call dword ptr[_menu_];

// "v1 = " ********************

push prompt1;

call dword ptr[printf];

pop ebx;

lea eax, v1;

push eax; // &v1

push ScanfPrompt; // "%d"

call dword ptr[scanf];

pop ebx;

pop ebx;


// "v2 = " ********************

push prompt2;

call dword ptr[printf];

pop ebx;


lea eax, v2;

push eax; // &v2

push ScanfPrompt; // "%d"

call dword ptr[scanf];

pop ebx;

pop ebx;


// choice

push promptChoice;

call dword ptr[printf];

pop ebx;


lea eax, nChoice; // &nChoice

push eax;

push ScanfPrompt; // "%d"

call dword ptr[scanf];

pop ebx;

pop ebx;


//

cmp nChoice, 0x1;

je addJump;

jne not_addJump;

addJump: 

push v2;

push v1;

call dword ptr[_add_];

pop ebx;

pop ebx;

jmp _EnD;

not_addJump:

cmp nChoice, 0x2; 

je subJump;

jne not_subJump;

subJump:

push v2;

push v1;

call dword ptr[_sub_];

pop ebx;

pop ebx;

jmp _EnD;

not_subJump:

cmp nChoice, 0x3;

je mulJump;

jne not_mulJump;

mulJump:

push v2;

push v1;

call dword ptr[_mul_];

pop ebx;

pop ebx;  

jmp _EnD;

not_mulJump:

push v2;

push v1;

call dword ptr[_div_];

pop ebx;

pop ebx;

jmp _EnD;

}

_EnD:

return 0;

}

'어셈블리' 카테고리의 다른 글

jz ==> zero flag  (0) 2017.09.23
대소관계  (0) 2017.09.23
hello world  (0) 2017.07.20
인라인 어셈블리 계산기(사칙연산 완성)  (0) 2017.07.04
nasm  (0) 2017.07.01