Presentation is loading. Please wait.

Presentation is loading. Please wait.

KyungHee Univ. 2-0 Chapter 2 Assembly Language Programming.

Similar presentations


Presentation on theme: "KyungHee Univ. 2-0 Chapter 2 Assembly Language Programming."— Presentation transcript:

1 KyungHee Univ. 2-0 Chapter 2 Assembly Language Programming

2 KyungHee Univ. 2-1  왜 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

3 KyungHee Univ. 2-2  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

4 KyungHee Univ. 1-3  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 이다.

5 KyungHee Univ. 2-4  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 Code 00004b 2700 00004c 9300 0062 00004e bb01 00004f ef0f 000050 bb02 000051 9300 0061 Processor RAM ROM 2700 9300 0062 bb01 Ef0f bb02 9300 0061 I/O Port External Device AssemblerLoader Microcontroller

6 KyungHee Univ. 1-5 AVR128A Processor  Architectural Overview : Block Diagram of the AVR Architecture

7 KyungHee Univ. 2-6  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 명령만 사용 가능 함.

8 KyungHee Univ. 2-7  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

9 KyungHee Univ. 2-8  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 으로 사용 된다. Bit76543210 ITHSVNZC Read/Write R/W Initial Value 00000000

10 KyungHee Univ. 2-9 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 된다.

11 KyungHee Univ. 2-10  AVR General Purpose Register File Rg0 – Rg15 Rg16 – Rg31 X Rg Y Rg Z Rg

12 KyungHee Univ. 2-11  X-register, Y-register, and Z-register Registers R26:R31 은 일반 Rg 로의 기능 이외에 Data Space 에서 Indirect addressing 을 위한 16-bit address pointers 로서의 기능을 갖는다. 15XHXL0 X-Rg7070 R27($1B)R26($1A) 15YHYL0 Y-Rg7070 R29($1D)R28($1C) 15ZHZL0 Z-Rg7070 R31($1F)R30($1E)

13 KyungHee Univ. 1-12 Index Rg 이용 예  1000 번지 부터 1099 번지 까지 100Byte 를 2000 번지 부터 저장하라.  Index Rg 가 없는 경우의 예 (2000)  (1000) (2001)  (1001) (2002)  (1002) - - - - (2099)  (1099)

14 KyungHee Univ. 1-13 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:

15 KyungHee Univ. 1-14 AVR Memory Space  Data Memory Map Working Registers  Includes X, Y, and Z registers. I/O Register Space  Includes “named” registers SRAM – Data Space  Runtime Variables and Data  Stack space  Program Flash Memory Map Program Flash Memory  Vectors, Code, and (Unchangeable) Constant Data. Boot Program Flash Memory  EEPROM space  For non-volatile but alterable data

16 KyungHee Univ. 1-15 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

17 KyungHee Univ. 1-16 SRAM Data Memory  Tmega128A Normal Mode Register file : First 32($0-$19) locations address Standard I/O memory : Next 64($20-$5f) location Extended I/O memory : Next 160($60-$ff) locations Internal data SRAM : Next 4096 locations address ConfigurationInternal SRAM Data Memory External SRAM Data Memory Normal mode4096up to 64K ATmega103 Compatibility mode 4000up to 64K Memory Configurations

18 KyungHee Univ. 1-17 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 Vector Table $0000 Application Program Section Boot Program Section$FFFF

19 KyungHee Univ. 1-18 EEPROM Data Memory  ATmega128A 4K bytes 의 Data EEPROM memory Separate data space Single bytes read, written 가 가능 함. EEPROM access registers are accessible in the I/O space EEPROM is read, the CPU is halted for four clock cycles before the next instruction is executed EEPROM is written, the CPU is halted for two clock cycles before the next instruction is executed

20 KyungHee Univ. 1-19 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

21 KyungHee Univ. 1-20  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

22 KyungHee Univ. 1-21 일반적인 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 를 저장함.

23 KyungHee Univ. 2-22  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 InstructionStack pointerDescription PUSHDecremented by 1Data is pushed onto the stack CALL ICALL RCALL Decremented by 2Return address is pushed onto the stack with a subroutine call or interrupt POPIncremented by 1Data is popped from the stack RET RETI Incremented by 2Return address is popped from the stack with return from subroutine or return from interrupt

24 KyungHee Univ. 2-23  SPH and SPL - Stack Pointer High and Low Register AVR Stack Pointer 는 I/O space 내에 Two 8-bit registers 로 실현된다. Stack 영역을 작게 요구되는 기종에서는 SPL(8-bit) 만 이용 된다. Bit15141312111098 SPH SP15SP14SP13SP12SP11SP10SP9SP8 SPL SP7SP6SP5SP4SP3SP2SP1SP0 Bit 76543210 Read/Write R/W Initial Value 00000000

25 KyungHee Univ. 2-24 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 의 보호를 위한 임시 저장 장치로 사용

26 KyungHee Univ. 2-25 Subroutines and the Stack  push Operation 예 : push Rg (SP) ← Rg SP ← SP - 1  Pull Operation 예 : pull Rg SP ← SP + 1 Rg ← (SP)

27 KyungHee Univ. 2-26  Subroutine 일반 프로세서에서의 Subroutine  Call sub (SP)  PC SP  SP-- PC  Subroutine 의 시작 번지  Return SP  SP ++ PC  (SP) 주 : 여기에서 ++, -- 의 의미는 다음 번지, 이전 번지의 의미 임. Subroutines and the Stack

28 KyungHee Univ. 2-27 2.2.9 Subroutines and the Stack  push Operation ldi R16, 0xAA push R16 Push Operation 실행 전 실행 후 Lower Address SP  3Top 2 Higher Address 1 Lower Address SP  AATop 3 2 Higher Address 1

29 KyungHee Univ. 2-28 2.2.9 Subroutines and the Stack  pop Operation pop R16 Pop Operation 실행 전 실행 후 Lower Address SP  AATop 3 2 Higher Address 1 Lower Address SP  AA 3Top 2 Higher Address 1 R16AA

30 KyungHee Univ. 1-29 일반적인 Addressing Mode  Inherent Addressing Mode Operand Field 를 갖지 않는다.  Immediate Addressing Mode Operand Field 에 상수 값이 직접 주어진다.  Direct Addressing Mode Operand Field 에 실제 Address 가 온다.  Indirect Addressing Mode Operand Field 에 실제 Address 가 저장된 주소가 온다.  Relative Addressing Mode PC 또는 Index Rg 를 기준으로한 상대번지가 온다.

31 KyungHee Univ. 1-30  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

32 KyungHee Univ. 1-31 Register Direct: 1 Register Register File 0 d 31 1540 OPRd Example: inc R16 clr R22

33 KyungHee Univ. 1-32 Register Direct: 2 Register Register File 0 d r 31 159540 OPRrRd Example: add R16,R17 cp R22,R5 mov R0,R1 Rd: R0-R31 or R16-R31 ( Depending on instruction) Rr: R0-R31

34 KyungHee Univ. 1-33 I/O Direct I/O Memory 0 63 1550 OPnRd Example: in R16,PIND out PORTC,R16

35 KyungHee Univ. 1-34 Data Direct Data Space &0000 $FFFF 160 OPRr0000 15Address0 Example: sts 0x1000,R16

36 KyungHee Univ. 1-35 Data Indirect Data Space $0000 $FFFF 1550 OPRd/Rr Example: ld R16, Y st Z, R16 1550 X,Y or Z Register

37 KyungHee Univ. 1-36 Data Indirect r/w Displacement Data Space $0000 $FFFF 1550 X,Y or Z Register Example: ldd R16, Y+0x10 std Z+0x20, R16 1510650 OPRd/Rra +

38 KyungHee Univ. 1-37 Data Indirect : Pre-Decrement Data Space $0000 $FFFF 1550 X,Y or Z Register Example: ld R16, -Z st -Z, R16 +

39 KyungHee Univ. 1-38 Data Indirect : Post-Increment Data Space $0000 $FFFF 1550 X,Y or Z Register Example: ld R16, Z+ st Z+, R16 1 +

40 KyungHee Univ. 1-39 Program Memory Addressing Program Memory $000 $7FF/$FFF 1510 Z Register Example: lpm R30, Z+

41 KyungHee Univ. 1-40 Indirect Program Memory Addressing Program Memory $000 $7FF/$FFF 150 Z Register Example: ijmp icall Z Rg 를 Index Rg 로 사용 한다.

42 KyungHee Univ. 1-41 Relative Program Addressing Program Memory $000 $7FF/$FFF 150 PC Example: rjmp rcall 1512110 OPk +


Download ppt "KyungHee Univ. 2-0 Chapter 2 Assembly Language Programming."

Similar presentations


Ads by Google