0. Assembly Programming
1) Assembly Language Program 예시
1> 예시 1
2> 예시 2
- Data segment와 Code segment로 나뉜다. (Stack segment도 있다. 나중에 나옵니다.)
- size에 대해 잘 알아야 한다.
2) Microsoft Syntax Notation
해당 notation을 자주 사용할 것이다.
- […] : optional.
- {…|…|…} : a choice of one of the enclosed elements separated by | character.
- Elements in italics : items which have known definitions or descriptions.
1. Basic Language Elements
1) Integer Literals / Integer Constant
(상수와 리터럴 둘 다, 변하지 않는 값(데이터)를 의미)
(Literal : 변수에 넣는 변하지 않는 데이터 | 상수는 변하지 않는 변수)
1> format
[{+|-}] digits [radix] ex> 26, -26, 26d, 11010011b, 42q, 42o, 1Ah, 0A3h
- h : hexadecimal, q/o : octal, d : decimal, b : binary, r : encoded real, t : decimal(alt), y : binary(alt)
2> example
- 16진수는 가장 높은 자리의 수가 알파벳이면 맨 앞에 0을 추가한다. (identifier로 오해하는 일을 없애려고)
cf> Constant Integer Expressions
1> integer와 arithmetic operator로 이루어져 있다.
2> arithmetic operator에는 precedence가 있다.
2) Real Numer Literals
(12강에서 주로 다루니 지금은 가볍게 알아도 된다.)
1> format
[sign] integer.[integer][exponent]
- sign : {+|-}
- exponent : E[{+|-}]integer
2> example
-44.2E+5
=> -(sign)44(integer).2(integer)E+5(exponent)
3) Character String Literals
1> formats
- 둘 중 하나로 감싸져 있으면 character이거나 string
- 큰 따옴표와 작은 따옴표 모두 사용 가능하다.
2> Embedded quotes
'I said "Hi, internet.com.".'
- 내부에 큰 따옴표를 넣고 싶다면 -> 문자열의 시작과 끝을 나타내는 기호는 작은 따옴표를 사용
3> 문자열은 integer byte value의 연속으로 저장한다.
ex> 'ABCD'의 경우 41h, 42h, 43h, 44h 로 저장하면 된다.
0100 0001 0100 0010 0100 0011 0100 0100
4) Reserved Words
: Assembly Language가 먼저 사용하고 있는 단어 (C언어의 int와 같은)
1> Instruction mnemonics (명령어)
: CPU에게 직접 내리는 명령
- MOV를 mov 혹은 Mov로 써도 된다.
ex> MOV, ADD, and MUL
2> Register names
ex> EAX, AX, AH 등등
3> Directives (코드/데이터 영역 표시, 메모리 모델 선택, 프로시저 선언)
: assembler에게 어떻게 assemble할 것인지 지시해준다.
4> Attributes (Data type)
: variables나 operand가 어떤 크기에 어떤 정보를 담을지 알려주는 단어
ex> BYTE, WORD
5> Operators
: constant expressions에 사용되는 연산 기호
ex> *, +, -, /
6> Predefined symbols
: @data과 같이 assembly time(어셈블 할 때)에 특정 값을 갖게 되는 기호이다.
5) Identifiers
: 프로그래머가 정하는 variables, constants, procedures, labels의 이름
0> 규칙
- 1~247글자
- Case insensitive : 대소문자를 구분하지 않는다. (A와 a는 같다.)
- 첫 번째 글자 : letter(A~z), _, @, $ 중 하나 (두 번째 이상부터 숫자가 올 수 있다.)
- Reserved Words와 달라야 한다.
1> variables
2> constants
3> procedure
4> labels (data label : / code label : jmp 혹은 loop의 목적지)
6) Directives
1> assembler가 assemble할 때(compile) 해석하여 특정 일을 수행해주는 명령
(CPU에 직접 프로그래머가 명령 내리는 것이 아니다.)
ex> .data, .code, name PROC
2> 특징
- assembler마다 다른 directive를 가진다.
- Case insensitive
ex> .data == .Data == .Data
3> 역할
- declare code area and data area
- define variable
- declare procedure(함수)
- select memory model
4> 예시
- code area에 instuction이 들어간다.
(stack area는 나중에 배울 예정)
7) Instruction
1> 정의 : program을 assemble할 때 실행가능한 명령
- assembler가 instruction을 machine language로 번역하고, run time에서 CPU가 이를 실행한다.
2> standard instruction format
Label: Mnemonic Operand(s) ; Comment
(Label과 Comment는 선택사항)
- Label (identifier) : code(instruction)이나 data의 위치를 표시
- code label : code(instruction)의 위치를 표시 (C에서 goto문 처럼 특정 위치로 가는 역할) [colon(:) 사용]
- data label : data의 위치를 표시 [colon 사용 X]
- Mnemonic : CPU에 내리는 명령 코드(opcode)를 이해하기 쉽게 영문 약자로 표시한 것 ex> MOV
- Comment : semi-colon(;) 사용
3> examples
stc ; set Carry flag
=> operand가 없는 instruction
inc eax ; register (increase)
inc myByte ; memory (increase)
add ebx,ecx ; register, register
=> ebx와 ecx에 저장된 값을 더해서 ebx(destination)에 할당
sub myByte,25 ; memory, constant
add eax,36*25 ; register, constant-expression
cf> Example 1 : Adding and Subtracting Integers
- call DumpRegs : display로 출력한다. (아마 아래와 같이 모든 register를 출력하는 명령인 것 같다.)
cf> Example 2 : Adding and Subtracting Integers (Alternative Version)
- INVOKE ExitProcess, 0 : exit의 대체
- .386 : minimum CPU required for this program
- .MODEL flat,stdcall (flat : generate code for protected mode) (stdcall : enables the calling of MS Windows)
- .STACK 4096 (4096은 default size)
- ExitProcess PROTO, dwExitCode:DWORD
- DumpRegs PROTO : procedure from Irvine32 link lib.
cf> Programming Suggestions
- 대문자 최대한 통일시켜서 쓰기
- label : 들여쓰기 X
- instruction : 들여쓰기 (4~5줄)
- comment : 40~45줄 띄어서 모든 comment가 평행을 이루도록하기
- instruction과 operand 사이는 1~3줄 띄어쓰기
- procedure 사이는 1~2 line의 공백 만들기
'Assembly' 카테고리의 다른 글
4-1강 - Data Transfer, Addressing and Arithmetic 1 (Data Transfer Instruction) (0) | 2020.05.04 |
---|---|
3-2강 - 어셈블리어 기본 2 (Assembly Language Fundamentals 2) - (0) | 2020.04.30 |
Register 정리 (0) | 2020.04.28 |
2강 - X86 Proceesor Architecture (0) | 2020.04.28 |
1강 - 기본 개념 (Basic Concept) (0) | 2020.04.27 |