์ƒˆ์†Œ์‹

Languages/Assembly

[์–ด์…ˆ๋ธ”๋ฆฌ์–ด] ๋กœ๋˜ ์ƒ์„ฑํ•˜๊ธฐ

  • -
๋ฐ˜์‘ํ˜•
๋กœ๋˜ ์ƒ์„ฑ๊ธฐ

 

์ „์ฒด ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์„ค๊ณ„

 

C์—์„œ ๊ฐ€์ ธ์™€์•ผ ํ•  ๊ธฐ๋Šฅ์„ ๋จผ์ € ์ƒ๊ฐํ•ด๋ณธ๋‹ค.

์–ด์…ˆ๋ธ”๋ฆฌ ์–ธ์–ด๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ ํž˜๋“  ๋žœ๋ค ๊ฐ’์„ ๊ฐ€์ ธ์˜ค๋ ค ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ ค๋ฉด ๋žœ๋ค ์‹œ๋“œ ๊ฐ’์„ ์ดˆ๊ธฐํ™”(ํ˜„์žฌ์‹œ๊ฐ„์œผ๋กœ) ์‹œํ‚ค๋Š” ํ•จ์ˆ˜์™€ 1~45 ์‚ฌ์ด์˜ ์ •์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•ด์ค„ ํ•จ์ˆ˜๊ฐ€ ํ•„์š”ํ•  ๊ฒƒ์ด๋‹ค.

 

- inimylib() : ๋žœ๋ค ์‹œ๋“œ ๊ฐ’ ์ดˆ๊ธฐํ™”(ํ˜„์žฌ์‹œ๊ฐ„)

- getnumber() : 1~45 ์‚ฌ์ด์˜ ์ •์ˆ˜ ๋ฆฌํ„ด

 

๋‹ค์Œ์œผ๋กœ๋Š”

 

์–ด์…ˆ๋ธ”๋ฆฌ ์–ธ์–ด์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ์œผ๋กœ ์„ค๊ณ„๋ฅผ ํ•ด๋ณธ๋‹ค.

๋จผ์ € 6๊ฐœ์˜ ์ˆซ์ž๋ฅผ ๋‹ด์„ ๋ฐฐ์—ด์ด ํ•„์š”ํ•  ๊ฒƒ์ด๋‹ค. ๋˜ํ•œ ์ˆซ์ž๋ฅผ ๋น„๊ตํ•˜๋Š” ํ•จ์ˆ˜๋„ ํ•„์š”ํ•˜๋‹ค.

 

-int data[6] : ์ „์—ญ๋ณ€์ˆ˜

- main : ๋ฉ”์ธ ํ•จ์ˆ˜

- checksame() : ๋™์ผํ•œ ์ˆซ์ž๊ฐ€ ๋“ค์–ด๊ฐ€์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜

- showdata() : ์ˆซ์ž 6๊ฐœ๋ฅผ ์ถœ๋ ฅํ•˜์—ฌ ๋ณด์—ฌ์ฃผ๋Š” ํ•จ์ˆ˜

 


 

๊ตฌ์„ฑ

 

1. c๋กœ ๋จผ์ € ํฐ ํ‹€์„ ์งœ๋ณด๊ณ  ์–ด์…ˆ๋ธ”๋ฆฌ์–ด๋กœ ํ•˜๋ฉด ๋” ๋„์›€์ด ๋งŽ์ด ๋จ (๋œ ํ—ท๊ฐˆ๋ฆผ)

2. c ํŒŒ์ผ์„ ์ƒ์„ฑํ•ด์ค€๋‹ค. (c์–ธ์–ด๋กœ ๋žœ๋ค๊ฐ’ ์ƒ์„ฑํ•˜๊ณ  ๋Œ๋ ค์ฃผ๋Š” ์ฝ”๋“œ)

3. ์ค‘๊ฐ„ํŒŒ์ผ ์ƒ์„ฑ

4. ๋ณ€์ˆ˜ ์„ ์–ธ๊ณผ ๊ธฐ๋ณธ ํ•จ์ˆ˜๋“ค์˜ ๊ณจ๊ฒฉ์„ ๋งŒ๋“ ๋‹ค. 292์‚ฌ์ง„ ์ฐธ๊ณ 

 

-->์—ฌ๊ธฐ๊นŒ์ง€ ์™„์„ฑ์ด ๋˜์—ˆ๋‹ค๋ฉด ํ”„๋กœ์‹œ์ €๋ฅผ ๊ตฌํ˜„ํ•ด์ค€๋‹ค.

 

5.์–ด์…ˆ๋ธ”๋ฆฌ์–ด ์ฝ”๋“œ ๋งˆ๋ฌด๋ฆฌ

6. ์ค‘๊ฐ„ํŒŒ์ผ ๋งŒ๋“ค๊ณ , ํ•ฉ์น˜๊ธฐ

7. ๊ฒฐ๊ณผ

 

 

๋ฐฐ์น˜ํŒŒ์ผ์„ ์ƒ์„ฑํ•ด์ฃผ์–ด, ๊ฒฝ๋กœ์•ˆ์—์„œ ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋˜๊ฒŒ๋” ํ•˜์˜€๋‹ค.

 

 

1. c๋กœ ํฐ ํ‹€ ์ƒ๊ฐํ•ด๋ณด๊ธฐ

// c๋กœ showdata ์ฝ”๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„

void showdata()
{
	t=0;
	printf("Lotto : ");
	while(t<6)
	{
		printf(data[t]);
		t=t+1;
		if(t <5)
		{
			printf(",")
		}
	}
}


//checksame ์ฝ”๋“œ ์•Œ๊ณ ๋ฆฌ์ฆ˜
//๋™์ผํ•œ ์ˆซ์ž ์žˆ๋‹ˆ?

int data[6];
//eax : ์ƒˆ๋กœ์šด ๊ฐ’, ebx : ๋งŒ๋“ค์–ด์ง„ ๋ฐ์ดํ„ฐ ๊ฐœ์ˆ˜
int checksame(eax,ebx) //๋ณ€์ˆ˜์ด๋ฆ„์€ ์ƒ๊ด€ ์—†์Œ.
{
	t=0;
	while(t <ebx)
	{
		if(data[t] == eax)
		{
			eax = 1;
			return eax;
			
		}
		t=t+1;
	}
	
	eax=0;
	return eax;
	
}			

// main ์•Œ๊ณ ๋ฆฌ์ฆ˜

int data[6];
void main()
{
	int size=0;
	while(size < 6)
	{
		a= getnumber();
		if(1!= checksame(a,size))
		{
			data[size]=a; //๋ฐ›์•„์˜จ ๋žœ๋ค๊ฐ’์ด ๋™์ผํ•œ์น˜ ์ฒดํฌ ํ•˜๋Š”๊ฒƒ
			size = size +1;
		}
	}
	
	showdata(data); //์ˆซ์ž 6๊ฐœ ์ถœ๋ ฅํ•ด์„œ ๋ณด์—ฌ์ฃผ๋Š” ํ•จ์ˆ˜
}

 

2. random ๊ฐ’ ๋Œ๋ ค์ฃผ๋Š” ํ•จ์ˆ˜

 

#include <stdlib.h>
#include <time.h> 
void now_time(){
   srand(time(NULL));
}
int random_num(){
   int a;
   a = rand()%45+1;
   return a;
}

 

3. ์ค‘๊ฐ„ํŒŒ์ผ ๋งŒ๋“ค๊ธฐ

 

 

4. ๊ธฐ๋ณธ ๊ณจ๊ฒฉ ๋งŒ๋“ค๊ธฐ(ํฐ ํ‹€)

 

 

5. ์–ด์…ˆ๋ธ”๋ฆฌ ์ฝ”๋“œ (ํ•œ์ค„๋งŒ ๋ฝ‘๋Š” ์ฝ”๋“œ) -> ๋กœ๋˜ 1์ค„

; 32bit mode
%include "io.inc"

LOTTO_COUNT equ 6
;equ ์ง€์‹œ์–ด๋Š” ์‹ฌ๋ณผ์„ ์ •์˜ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ์‹ฌ๋ณผ(symbol)์ด๋ž€ ์–ด์…ˆ๋ธ”๋ฆฌ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋Š” ์ƒ์ˆ˜๋ฅผ ๋งํ•˜๋ฉฐ, ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•œ๋‹ค.
;symbol equ value ํ•œ ๋ฒˆ ์ •์˜๋œ ์‹ฌ๋ณผ์˜ ๊ฐ’์€ ์ ˆ๋Œ€๋กœ ์žฌ์ •์˜ ๋  ์ˆ˜ ์—†๋‹ค.

section .text
global CMAIN
extern _getnumber ;c์–ธ์–ด์—์„œ ๊ฐ€์ ธ์˜ค๋Š”ํ•จ์ˆ˜ 1~45 ์‚ฌ์ด ์ˆ˜ ๋ฆฌํ„ด
extern _inimylib ; ๋žœ๋ค์‹œ๋“œ๊ฐ’ ์ดˆ๊ธฐํ™” ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜
;============================================์•„๋ž˜ ๊ฐ๊ฐ์—๋Š” ์–ด์…ˆํ•จ์ˆ˜๊ฐ€ ๋“ค์–ด๊ฐˆ ๊ฒƒ์ž„

;input:
;return:

CMAIN:
   call _inimylib ; c์–ธ์–ด๋กœ ์งœ๋†“์€ ์ฝ”๋“œ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ, ๋žœ๋ค์‹œ๋“œ๊ฐ’ ์ดˆ๊ธฐํ™”
   ;mov ecx,0 ; ecx๋ ˆ์ง€์Šคํ„ฐ ์ดˆ๊ธฐํ™” 
   ;ecx๋Š” ๋ณดํ†ต ์นด์šดํ„ฐ ๋ ˆ์ง€์Šคํ„ฐ์ž„. ์ฃผ๋กœ ๋ฐ˜๋ณต ๋ช…๋ น์–ด ์‚ฌ์šฉ์‹œ ๋ฐ˜๋ณต ์นด์šดํ„ฐ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๋ ˆ์ง€์Šคํ„ฐ๋กœ, ECX ๋ ˆ์ง€์Šคํ„ฐ์— ๋ฐ˜๋ณตํ•  ํšŸ์ˆ˜๋ฅผ ์ง€์ •ํ•˜๊ณ , ๋ฐ˜๋ณต ์ž‘์—…์„ ์ˆ˜ํ–‰
   mov ecx,0
cmain_loop1:
   cmp ecx, LOTTO_COUNT ;๊ฐ’์„ ๋น„๊ต ํ•จ.
   jge cmain_end ; ecx์˜ ๊ฐ’์ด ๋” ํฌ๋ฉด cmain ํ•จ์ˆ˜ ์ข…๋ฃŒ.
   
   push ecx
   call _getnumber ;45 ์‚ฌ์ด ์ˆ˜ ๋ฆฌํ„ด๋ฐ›์Œ
   mov [a],eax
   ;์—ฌ๊ธฐ์„œ eax๋Š” c์ฝ”๋“œ์—์„œ ๋ฐ›์•„์˜จ a,์ฆ‰ ๋žœ๋ค์œผ๋กœ ๋ฝ‘ํžŒ ํ•œ๊ฐœ์˜ ๊ฐ’์ž„   
   pop ecx
   
   push ecx
   mov ebx,ecx
   call CheckSame ; CheckSame์ฆ‰ ๋™์ผํ•œ ๊ฐ’์ด ๋“ค์–ด๊ฐ€๋Š”์ง€ ๋น„๊ตํ•˜๋Š” ํ•จ์ˆ˜ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
   pop ecx
   
   cmp eax,1
   je checksame_loop1 ;je=๊ฐ™์œผ๋ฉด ์ ํ”„ (์ฐธ์ผ์‹œ ์ฆ‰ ๊ฐ’์ด ๊ฐ™์œผ๋ฉด)
   
   mov ebx, [a]
   mov [data+ecx*4],ebx ;ebx๋ฅผ ๋‹ค์Œ ๋ฐฐ์—ด์˜ ๋„ฃ๋Š”๋‹ค.
   
   inc ecx;๊ฐ’์ฆ๊ฐ€ +1   
   jmp cmain_loop1;๋‹ค์‹œ ๋ฃจํ”„๋Œ๊ธฐ (๋ฐ˜๋ณต๋ฌธ)
   
cmain_end:
   call ShowData ;์ˆซ์ž 6๊ฐœ ์ฆ‰ > ๋žœ๋คํ•œ 6๊ฐœ ๋กœ๋˜ ๊ฐ’ ์ถœ๋ ฅ์‹œ์ผœ ๋ณด์—ฌ์คŒ   
   ret
;=============================================
;input: eax(a), ebx(size)
;return: eax(0: not same, 1: find same)
   inc ecx
CheckSame:
   mov edx,0 ;edx ์ดˆ๊ธฐํ™”
   mov ecx,0 ;ecx ์ดˆ๊ธฐํ™”

checksame_loop1:
   cmp ecx,ebx ;๊ฐ’์„ ๋น„๊ต
   jge checksame_end
   cmp eax, [data+ecx*4];*4๋ฅผ ํ•ด์ฃผ๋Š” ์ด์œ ๋Š” ๋‹ค์Œ ๋ฐฐ์—ด์„ ์ฐพ์•„์„œ ๊ฐ€๊ธฐ ์œ„ํ•จ์ž„(4byte์งœ๋ฆฌ), ๋‹ค์Œ์นธ ์ฃผ์†Œ ์•Œ์•„์„œ ์ฐพ์•„๊ฐ.
   jne checksame_endif  ;!= ์ ํ”„ํ•ด๋ผ!
   mov edx,1 
   jmp checksame_end

checksame_endif:
   inc ecx ; 1์ฆ๊ฐ€, ๋‹ค์Œ๊ฐ’ start
   jmp checksame_loop1
   NEWLINE

checksame_end:
   mov eax, edx   
   ret

;===================================================================
;input:
;return:
ShowData:
   NEWLINE
   PRINT_STRING msg1
   mov ecx,0 ;๋ ˆ์ง€์Šคํ„ฐ ์ดˆ๊ธฐํ™”
   
showdata_loop1: ;label start
   cmp ecx,LOTTO_COUNT ; ๊ฐ’ ๋น„๊ต 
   jge show_end;์•ž์˜ ๊ฐ’์ด ํฌ๊ฑฐ๋‚˜,๊ฐ™๋‹ค๋ฉด >= show_end๋กœ ์ด๋™
   
   push ecx;์Šคํƒ์— ๋„ฃ์–ด์คŒ
   PRINT_DEC 4, [data + ecx*4] ;์ถœ๋ ฅ
   pop ecx ; ๊ฐ’ ๋นผ์คŒ
   
   cmp ecx, LOTTO_COUNT -1 ;ecx์™€ ๊ฐ’ ๋น„๊ต
   jge show_next
   push ecx
   PRINT_STRING msg2 ;๋ฌธ์ž์—ด ์ถœ๋ ฅ
   pop ecx ;push๋กœ ์Šคํƒ์— ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๋ฉด pop์„ ์ด์šฉํ•ด์„œ ๋ฐ˜๋“œ์‹œ ๋น„์›Œ์ค˜์•ผํ•จ.

show_next:
   inc ecx ; inc = ํ”ผ์—ฐ์‚ฐ์ž์— 1์„ ๋”ํ•œ๋‹ค.
   jmp showdata_loop1 ;๋ฌด์กฐ๊ฑด jmpํ•˜๋Š” ๋ช…๋ น์–ด/ํ”ผ์—ฐ์‚ฐ์ž์˜ ์œ„์น˜๋กœ ์‹คํ–‰ ํ๋ฆ„์ด ๋ณ€๊ฒฝ๋œ๋‹ค. ํ”ผ์—ฐ์‚ฐ์ž๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ฝ”๋“œ๋กœ ์ ํ”„ ๋›ฐ์–ด์„œ ์‹คํ–‰ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.

show_end:
   ret
   
section .data ;์ดˆ๊ธฐ๊ฐ’,๋ฉ”๋ชจ๋ฆฌ ์šฉ๋Ÿ‰ ํ™•๋ณด
   msg1 db "Lotto : ",0
   msg2 db ",",0
 
   
   
section .bss;๋ฉ”๋ชจ๋ฆฌ ์šฉ๋Ÿ‰,๊ฐฏ์ˆ˜ ํ™•๋ณด
   a resd 1 ;4byte
   data resd 6  
   num resw 1;
   num2 resw 1;
   
   

์ฃผ์„์ฒ˜๋ฆฌ๋„ ํ•จ๊ป˜ ํ•ด๋†“์•˜์Šต๋‹ˆ๋‹ค. (์ฝ”๋“œ๋ธ”๋Ÿญ ์ข…๋ฅ˜์— asm ์ด ์—†์–ด์„œ ๋‹ค๋ฅธ๊ฑธ๋กœ ํ•ด๋‘์–ด, notepad++์ด๋‚˜, ๋‹ค๋ฅธ ์—๋””ํ„ฐ๋กœ ์‹คํ–‰์‹œ์ผœ ์ฃผ์„ธ์š”.

 

 

5. ์–ด์…ˆ๋ธ”๋ฆฌ ์ฝ”๋“œ (์ž…๋ ฅ ๋ฐ›๋Š” ๋งŒํผ ๋กœ๋˜ ์ถœ๋ ฅ)

 

%include "io.inc"

COUNT equ 6
section .data 
   count dd 0
   
section .bss
   paper resd 1
   lotto resd 6
   num resd 1
section .text
global CMAIN
extern _now_time
extern _random_num
extern _printf


   
CMAIN:
   call _now_time
	NEWLINE
   PRINT_STRING "๋ช‡์žฅ์„ ๊ตฌ๋งคํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ? : "
   GET_DEC 4, [paper]
   NEWLINE
   PRINT_STRING "=============================="
   
   NEWLINE
   
gasu:
   mov ecx, [paper]
   cmp [count], ecx
   jge real_Bye
   
   mov ecx, 0
Random:
   cmp ecx, COUNT ;6๊ฐœ ์ˆซ์ž ๋ฝ‘์•„๋‚ด๊ธฐ ์œ„ํ•œ ๊ฒƒ
   je Bye
   
   push ecx
   call _random_num
   mov [num], eax
   pop ecx
   
   push ecx
   mov ebx, ecx
   call Same
   pop ecx
   
   cmp eax, 1
   je Random
   
   mov ebx,[num]
   mov [lotto+ecx*4], ebx
   
   inc ecx
   jmp Random
   
Bye:
   call Show
   mov ecx, [count]
   inc ecx
   mov [count], ecx
   jmp gasu
   
   
real_Bye:
    ret
   
;------------------------------------

Same:
   mov edx,0 
   mov ecx,0 

checksame_loop1:
   cmp ecx, ebx 
   je Ok_check
   cmp eax, [lotto+ecx*4]
   jne check
   mov edx,1 
   jmp Ok_check

check:
   inc ecx 
   jmp checksame_loop1

Ok_check:
   mov eax, edx   
   ret 
;------------------------------------
Show:
	call _printf
   PRINT_STRING "lotto : %d"
   mov ecx, 0
number:
   cmp ecx, COUNT
   jge Print_end
   push ecx
   PRINT_DEC 4, [lotto + ecx*4]
   pop ecx
   
   cmp ecx, COUNT -1
   jge next
   push ecx
   PRINT_STRING ", "
   pop ecx

next:   
   inc ecx
   jmp number
   
Print_end:
   NEWLINE
   ret

 

 

6. ์ค‘๊ฐ„ํŒŒ์ผ ๋งŒ๋“ค๊ณ  ํ•ฉ์น˜๊ธฐ

 

ex) 

a.asm ํŒŒ์ผ๊ณผ, b.c ํŒŒ์ผ์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •์„ ํ•˜๋ฉด

1. nasm -g -f win32 a.asm -o a.obj --> ํ•˜์—ฌ ์ค‘๊ฐ„ ํŒŒ์ผ๋งŒ๋“ค๊ธฐ <a.obj>

2. gcc -c b.c --> .oํŒŒ์ผ ์ƒ์„ฑ <b.o>

3. gcc a.obj macro.c b.o -g -o lotto -m32 -->exeํŒŒ์ผ ๋งŒ๋“ค๊ธฐ

 

์ด๋Ÿฐ์‹์œผ๋กœ ํ•ฉ์ณ์ฃผ๋ฉด ๋œ๋‹ค.

 

 

7. ๊ฒฐ๊ณผ

 

 

๋ฐ˜์‘ํ˜•
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.