Upload
allison-baldwin
View
226
Download
2
Embed Size (px)
Citation preview
KyungHee Univ. 2-1
Chapter 2Assembly Language Programming
KyungHee Univ. 2-2
왜 Assembly Language Programming 을 배워야 하나 ? 프로세서의 동작 이해에 필요 하다 .
Programmable Digital System 설계에 필요한 기본 기술 이해 프로그램 오류 ( 알고리즘 또는 Coding 상의 문제 ) 해결에 필요 Higher-level Language 가 지원하지 못하는 Hardware 기능 구현Time-critical 한 Routine 의 작성
Assembly Language 의 특징 Assembler Instructions 은 1 대 1 로 Machine Instruction 에 대응
한다 . uP 의 성능을 100% 구현 할 수 있다 .
가장 효율적인 프로기램 구현이 가능 하다 . 실행 속도 프로그램 Code 길이
배우기 쉽다 .
Assembly Language Programming
KyungHee Univ. 2-3
Instruction Set 란 ? 프로세서의 명령 실행 주기 동안에 실행되는 Code set 를 말한다 .
Binary Instructions 명령어에서 Binary pattern 111011000001100(0xEA0A) 의 의미는 R16 0xAA (LDI R16, 0xAA) 의 의미 이다 . 위 Binary Instruction 은 프로세서에게는 효과적인 명령 이지만
인간이 이해하기에는 너무불편하여 보다 효과적인 표시 방법이 필요 하여 ,
16 진법과 8 진법을 이용 하여 에서 Binary pattern 을 표시 한다 . 그러나 , 16 진법과 8 진법도 인간이 프로세서의 동작을 이해하기에
부족 하기 때문에 Assembly Language 를 사용 한다 .
Assembly Language Programming
KyungHee Univ. 1-4
Assembly Language 인간이 좀더 쉽게 프로세서의 명령을 이해할 수 있도록 이해하기
쉬운 심볼을 사용한 프로그래밍 언어 이다 .
예 )Instruction Mnemonic Rg/Memory Hexadecimal Equivalent
ldi R16, 0xAA EA0A
mov R0, R16 2E00
ldi R16, 0x55 E505
mov R2, R16 2E20
Assembler 그러나 Assembly Language 는 프로세서가 직접 실행할 수 없기
때문에 Assembly Language 로 작성된 프로그램을 Binary Instruction 으로 변환 하는 프로그램이 필요 하게 되고 , 이 프로그램이 Assembler 이다 .
KyungHee Univ. 2-5
Assembly language Development Process
Source Code ; I/O Port Init clr AC0 sts PORTF, AC0 out DDRD, AC0 ser AC0 out PORTD, AC0 sts DDRF, AC0
Object Code00004b 2700 00004c 9300 0062 00004e bb0100004f ef0f 000050 bb02000051 9300 0061
Processor
RAM
ROM2700 9300 0062 bb01Ef0f bb029300 0061
I/O Port
External Device
Assembler Loader
Microcontroller
KyungHee Univ. 1-6
AVR128A Processor Architectural Overview : Block Diagram of the AVR Architecture
KyungHee Univ. 2-7
Architectural Overview Harvard architecture
Program 과 data 를 위한 Memory 와 Bus 가 분리되어 있다 . Register file : 32 x 8-bit general purpose working registers 3 개의 16-bit indirect address register pointers. ALU supports arithmetic and logic operations
Registers Registers fun Registers Registers Registers fun Constant
Program Flash memory space 는 Boot Program section 과 Application Program section 2 가지로 구성 된다 .
I/O memory space contains 64 addresses : $20 - $5F ATmega128A 는 Extended I/O space($60 - $FF in SRAM) 를 갖는다 .
오직 ST/STS/STD and LD/LDS/LDD 명령만 사용 가능 함 .
KyungHee Univ. 2-8
ALU – Arithmetic Logic Unit High-performance AVR ALU operation
32 개의 general purpose working registers 와 직접 연결 되어 있다 .
Single clock cycle arithmetic operations Registers Registers fun Registers Registers Registers fun Constant
Three main categories ALU operations Arithmetic, Logical, and Bit-functions
Powerful multiplier Signed/unsigned multiplication and fractional format
KyungHee Univ. 2-9
Status Register 최근에 실행된 Arithmetic instruction 결과에 대한 정보를 표시한다 . 이 정보는 Program 흐름을 바꾸는 Conditional Operations 에 사용
할 수 있다 . 모든 ALU 연산에 의하여 Status Rg 가 Update 된다 .
SREG - AVR Status Register
Bit 7 – I: Global Interrupt Enable Bit Bit 6 – T: Bit Copy Storage
Bit Copy instructions BLD (Bit LoaD) 과 BST (Bit STore) 에서 T-bit 가 source 또는 destination 으로 사용 된다 .
Bit 7 6 5 4 3 2 1 0
I T H S V N Z C
Read/Write R/W R/W R/W R/W R/W R/W R/W R/W
Initial Value 0 0 0 0 0 0 0 0
KyungHee Univ. 2-10
Bit 5 – H: Half Carry Flag : Half carry 는 BCD arithmetic 에서 이용 된다 .
Bit 4 – S: Sign Bit, S = N V⊕ Bit 3 – V: Two’s Complement Overflow Flag Bit 2 – N: Negative Flag Bit 1 – Z: Zero Flag Bit 0 – C: Carry Flag
Register File 의 Address Map Data memory space 의 첫 32($20) 번지에 mapping 된다 .
KyungHee Univ. 2-11
AVR General Purpose Register File
Rg0 – Rg15
Rg16 – Rg31X Rg
Y Rg
Z Rg
KyungHee Univ. 2-12
X-register, Y-register, and Z-register Registers R26:R31 은 일반 Rg 로의 기능 이외에 Data Space 에서
Indirect addressing 을 위한 16-bit address pointers 로서의 기능을 갖는다 .
15 XH XL 0
X-Rg 7 0 7 0
R27($1B) R26($1A)
15 YH YL 0
Y-Rg 7 0 7 0
R29($1D) R28($1C)
15 ZH ZL 0
Z-Rg 7 0 7 0
R31($1F) R30($1E)
KyungHee Univ. 1-13
Index Rg 이용 예1000 번지 부터 1099 번지 까지 100Byte 를 2000 번지
부터 저장하라 .
Index Rg 가 없는 경우의 예
• (2000) (1000)
• (2001) (1001)
• (2002) (1002)
- - - -
- - - -
• (2099) (1099)
KyungHee Univ. 1-14
Index Rg 이용 예1000 번지 부터 1099 번지 까지 100Byte 를 2000 번지
부터 저장하라 .
Index Rg 와 Counter Rg 가 있는 경우의 예
Start :
Index Rg1 1000
Index Rg2 2000
Counter Rg 100
Loop :
(Index Rg2 ) (Index Rg1)
Index Rg1 Index Rg1 + 1
Index Rg2 Index Rg2 + 1
Counter Rg Counter Rg - 1
If (Counter Rg != 0 ) goto Loop
Done:
KyungHee Univ. 1-15
AVR Memory Space
Data Memory MapWorking Registers
Includes X, Y, and Z registers.I/O Register Space
Includes “named” registersSRAM – Data Space
Runtime Variables and Data Stack space
Program Flash Memory MapProgram Flash Memory
Vectors, Code, and (Unchangeable) Constant Data.Boot Program Flash Memory
EEPROM space For non-volatile but alterable data
KyungHee Univ. 1-16
Data Memory Map 32 Registers $0000-$001F
64 I/O Registers $0020 - $005F
160 Ext. I/O Reg. $0060 - $00FF
Internal RAM (4096 x 8)
$0100
$10FF
External RAM
$1100
$FFFF
KyungHee Univ. 1-17
SRAM Data Memory
Tmega128A Normal ModeRegister file : First 32($0-$19) locations addressStandard I/O memory : Next 64($20-$5f) locationExtended I/O memory : Next 160($60-$ff) locations Internal data SRAM : Next 4096 locations address
Configuration Internal SRAM Data Memory
External SRAM Data Memory
Normal mode 4096 up to 64K
ATmega103 Compatibilitymode
4000 up to 64K
Memory Configurations
KyungHee Univ. 1-18
In-System Reprogrammable Flash Program Memory
128K bytes On-chip In-System All AVR instructions are 16 or
32 bits wide. Flash is organized as 64K x 16 Divided into two sections
Boot Program section and Application Program section.
Interrupt VectorTable
$0000
Application ProgramSection
Boot ProgramSection $FFFF
KyungHee Univ. 1-19
EEPROM Data Memory
ATmega128A 4K bytes 의 Data EEPROM memorySeparate data spaceSingle bytes read, written 가 가능 함 .EEPROM access registers are accessible in the I/O
spaceEEPROM is read, the CPU is halted for four clock
cycles before the next instruction is executedEEPROM is written, the CPU is halted for two clock
cycles before the next instruction is executed
KyungHee Univ. 1-20
I/O Memory
ATmega128A ATmega128A 의 모든 I/O 와 주변 장치는 I/O Space 에 할당
되어 있다 . I/O Space 에 할당된 모든 I/O 와 주변 장치가 사용 하는
Register 는 32 개의 Register(Data) 와 Data Transfer 가 가능 하다 .
IN, OUT 명령은 I/O addresses $00 - $3F 에 할당된 I/O 와 주변 장치에 사용 한다 .
Data Space 에 있는 I/O Registers($60-$FF) 는 LD 와 ST 명령을 사용 한다 .
I/O Port 의 실제 번지는 32 개의 일반 Register 에 할당된 $0-$19($20) 번지 값을 더 한 값이 된다 . 참고자료 : ATmega128_doc8151.pdf
KyungHee Univ. 1-21
AVR Instruction Set Overview The AVR instruction set is divided into:
ARITHMETIC AND LOGIC INSTRUCTIONS Branch instructions DATA TRANSFER INSTRUCTIONS BIT AND BIT-TEST INSTRUCTIONS MCU CONTROL INSTRUCTIONS
KyungHee Univ. 1-22
일반적인 uP 에서 Registers 의 종류와 역활Data Rg
연산 및 자료 처리에 필요한 Data 를 저장함 Index Rg
Operand 의 주소를 저장 ( Pointing ) 함 .
Stack Pointer
Last-In-First-Out : Stack Operation
First-In-First-Out : Buffed I/O 에 유용Program Counter
다음에 실행할 명령어의 번지를 저장함 .
Status Rg
연산 결과 상태를 표시하는 Flag 를 저장함 .
KyungHee Univ. 2-23
Stack Pointer Stack 은 LIFO(Last In First Out) 알고리즘을 수행 한다 . Stack 의 이용 분야
Temporary data 의 저장 Local variables 의 저장 Interrupts 과 Subroutine calls 후 Return addresses 의 저장
Stack 은 Stack 영역이 필요 할 경우 Stack memory 의 Higher memory locations 로 부터 Lower memory locations 으로 확장 된다 .
Stack Pointer Register 는 항상 Stack 의 Top 위치를 Pointing 한다 .
Stack Pointer instructions
Instruction Stack pointer Description
PUSH Decremented by 1 Data is pushed onto the stack
CALLICALLRCALL
Decremented by 2 Return address is pushed onto the stack with a sub-routine call or interrupt
POP Incremented by 1 Data is popped from the stack
RETRETI
Incremented by 2 Return address is popped from the stack with return from subroutine or return from interrupt
KyungHee Univ.2-24
SPH and SPL - Stack Pointer High and Low Register AVR Stack Pointer 는 I/O space 내에 Two 8-bit registers 로 실현된
다 . Stack 영역을 작게 요구되는 기종에서는 SPL(8-bit) 만 이용 된다 .
Bit 15 14 13 12 11 10 9 8
SPH SP15 SP14 SP13 SP12 SP11 SP10 SP9 SP8
SPL SP7 SP6 SP5 SP4 SP3 SP2 SP1 SP0
Bit 7 6 5 4 3 2 1 0
Read/Write R/W R/W R/W R/W R/W R/W R/W R/W
Initial Value 0 0 0 0 0 0 0 0
KyungHee Univ. 2-25
Subroutines and the Stack
Last in First Out (LIFO)사용 예 : Stack Operation
First in First Out (FIFO)사용 예 : I/O Buffer 등
Stack 의 사용 예Subroutine 의 Return Address 저장Local variable 영역 확보Parameter Passing : 상위 계층 프로그램 모듈과 하위
모듈 사이에 Parameter Passing Subroutine 내에서 사용하는 Rg 의 보호를 위한 임시
저장 장치로 사용
KyungHee Univ. 2-26
Subroutines and the Stack
push Operation 예 : push Rg(SP) ← RgSP ← SP - 1
Pull Operation 예 : pull RgSP ← SP + 1Rg ← (SP)
KyungHee Univ. 2-27
Subroutine일반 프로세서에서의 Subroutine
Call sub• (SP) PC• SP SP--• PC Subroutine 의 시작 번지
Return• SP SP ++• PC (SP)
주 : 여기에서 ++, -- 의 의미는 다음 번지 , 이전 번지의 의미 임 .
Subroutines and the Stack
KyungHee Univ. 2-28
2.2.9 Subroutines and the Stack
push Operationldi R16, 0xAApush R16
Push Operation 실행 전 실행 후
Lower Address
SP
3 Top
2
HigherAddress
1
Lower Address
SP
AA Top
3
2
HigherAddress
1
KyungHee Univ. 2-29
2.2.9 Subroutines and the Stack
pop Operationpop R16
Pop Operation 실행 전 실행 후
Lower Address
SP
AA Top
3
2
HigherAddress
1
Lower Address
SP AA
3 Top
2
HigherAddress
1
R16 AA
KyungHee Univ. 1-30
일반적인 Addressing Mode
Inherent Addressing ModeOperand Field 를 갖지 않는다 .
Immediate Addressing ModeOperand Field 에 상수 값이 직접 주어진다 .
Direct Addressing ModeOperand Field 에 실제 Address 가 온다 .
Indirect Addressing ModeOperand Field 에 실제 Address 가 저장된 주소가
온다 .Relative Addressing Mode
PC 또는 Index Rg 를 기준으로한 상대번지가 온다 .
KyungHee Univ. 1-31
Register Direct, with 1 and 2 registers
I/O Direct
Data Direct
Data Indirect
with pre-decrement
with post-increment
Code Memory Addressing
AVR uP 의 Addressing Mode
KyungHee Univ. 1-32
Register Direct: 1 Register
Register File
0
d
31
15 4 0
OP Rd
Example: inc R16 clr R22
KyungHee Univ. 1-33
Register Direct: 2 Register
Register File
0
d
r
31
15 9 5 4 0
OP Rr Rd
Example: add R16,R17 cp R22,R5 mov R0,R1
Rd: R0-R31 or R16-R31 ( Depending on instruction)Rr: R0-R31
KyungHee Univ. 1-34
I/O Direct
I/O Memory
0
63
15 5 0
OP n Rd
Example: in R16,PIND out PORTC,R16
KyungHee Univ. 1-35
Data Direct
Data Space
&0000
$FFFF
16 0
OP Rr 0000
15 Address 0
Example: sts 0x1000,R16
KyungHee Univ. 1-36
Data Indirect
Data Space
$0000
$FFFF
15 5 0
OP Rd/Rr
Example: ld R16, Y st Z, R16
15 5 0
X,Y or Z Register
KyungHee Univ. 1-37
Data Indirect r/w DisplacementData Space
$0000
$FFFF
15 5 0
X,Y or Z Register
Example: ldd R16, Y+0x10 std Z+0x20, R16
15 10 6 5 0
OP Rd/Rr a
+
KyungHee Univ. 1-38
Data Indirect : Pre-DecrementData Space
$0000
$FFFF
15 5 0
X,Y or Z Register
Example: ld R16, -Z st -Z, R16
-1
+
KyungHee Univ. 1-39
Data Indirect : Post-IncrementData Space
$0000
$FFFF
15 5 0
X,Y or Z Register
Example: ld R16, Z+ st Z+, R16
1
+
KyungHee Univ. 1-40
Program Memory Addressing
Program Memory
$000
$7FF/$FFF
15 1 0
Z Register
Example: lpm R30, Z+
KyungHee Univ. 1-41
Indirect Program Memory Addressing
Program Memory
$000
$7FF/$FFF
15 0
Z Register
Example: ijmp icall
Z Rg 를 Index Rg 로 사용 한다 .
KyungHee Univ. 1-42
Relative Program Addressing
Program Memory
$000
$7FF/$FFF
15 0
PC
Example: rjmp rcall
15 12 11 0
OP k
+