Busan IT/Assembly2015. 8. 20. 17:39

==================================Outline====================================

어셈블리어의 구성요소

----------------------------------------------------------------------------

 

어셈블리어의 구성요소

 

에셈블리어 자체의 문법은 간단하지만 알고리즘 구현은 까다롭다.

 

 

 

Directive(.)는 행동을 명령할 때 사용된다.

 

 

 

에셈블리어는 name, mnemonic 그리고 operand로 구성된다.

 

//mov 데이터를 삽입할 때 사용한다. C에서 '='와 같은 연산자이다.

 

간단한 어셈블리어 코드를 보고 기본 문법을 익혀 보자.

 


/*** 코드 해석 ***/


.386 ; 386에서 수행되는 명령어를 사용, 일반적으로 코딩 시 386을 사용한다.

.MODEL FLAT ; 데이터가 구역 없이 메모리에 저장되는 방식 <-> SEGMENT

 

ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD ; Exit함수 호출: 원상복구 시키며 종료

 

INCLUDE io.h ; 헤더 파일 추가

 

 

cr EQU 0dh ; #define cr 0dh //16진수를 사용할 때, ‘0*h’로 표현해준다.

Lf EQU 0ah ; #define Lf 0ah

 

.STACK 4096 ; 4kbyte 스택를 확보한다.

 

.DATA ; 변수 선언. 데이터 영역 확보(전역변수)

number1 DWORD ? ; 4byte number1 초기화 없이 선언

number2 DWORD ? ; 4byte number2 초기화 없이 선언

 

prompt BYTE "Enter first number: ", 0; 문자열 선언 //어셈블리는 문자열에서 NULL을 포함시키지 않기 때문에 NULL을 추가시켜 주어야 한다.

prompt BYTE "Enter first number: ", 0;

 

string BYTE 40 DUP (?) ; 40BYTE 쓰레기 값을 복사하여 string에 넣어라.

labell BYTE cr, Lf, "The sum is " ; 13, 10, The sum islabell에 넣어라.

sum BYTE 11 DUP(?) ; 11BYTE 쓰레기 값을 복사하여 sum에 넣어라.

BYTE cr, Lf, 0 ; 1byte cr, Lf, 0 생성

 

 

컴파일 명령어 : ml /c /coff example.asm

 

 

링크 명령어 : link /subsystem:console /entry:start /out:example.exe example.obj io.obj kernel32.lib

 

//WORD = 2BYTE

//DWORD = double word, 4BYTE


/*** 코드 ***/

; Sample code for dummy

.386
.MODEL FLAT

ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD

INCLUDE io.h

cr EQU 0dh
Lf EQU 0ah

.STACK 4096

.DATA
number1   DWORD  ?
number2   DWORD  ?
prompt1   BYTE  "Enter first number: "0
prompt2   BYTE  "Enter second number: "0
string    BYTE  40 DUP (?)
labell    BYTE  cr, Lf, "The sum is "
sum     BYTE  11 DUP (?)
    BYTE  cr, Lf, 0

.CODE

;entry point
_start:
  output  prompt1    ;a printf call in C, output is a function made from the provider
  input  string, 40  ;a scanf call in C, percieve as ASCII code
  atod  string    ;convert ASCII to DWORD and store to eax
  mov  number1, eax  ;number1 = eax

  output  prompt2
  input  string, 40
  atod  string
  mov  number2, eax  

  mov  eax, number1
  add  eax, number2  ; eax = eax + number2 // result of algebric operation is stored at eax
  dtoa  sum, eax
  output  labell

  INVOKE  ExitProcess, 0

PUBLIC _start

반응형
Posted by newind2000