Directives, Memory, and Stack. Directives Special commands to the assembler May or may not generate...

23
Directives, Memory, and Stack

Transcript of Directives, Memory, and Stack. Directives Special commands to the assembler May or may not generate...

Page 1: Directives, Memory, and Stack. Directives Special commands to the assembler May or may not generate machine code Categories by their function Programming.

Directives, Memory, and Stack

Page 2: Directives, Memory, and Stack. Directives Special commands to the assembler May or may not generate machine code Categories by their function Programming.

Directives

Special commands to the assembler May or may not generate machine code

Categories by their function Programming directives

Object file directivesControl DirectivesList Directives Data Directives

Page 3: Directives, Memory, and Stack. Directives Special commands to the assembler May or may not generate machine code Categories by their function Programming.

Data Directives

Describe data ASCII data can be stored in memory using declare byte (DB) or DATA

Page 4: Directives, Memory, and Stack. Directives Special commands to the assembler May or may not generate machine code Categories by their function Programming.

Data Directives - Example

DE "Test Data"

Page 5: Directives, Memory, and Stack. Directives Special commands to the assembler May or may not generate machine code Categories by their function Programming.

List Directives

Control listing processExample:

LIST P=18F4520, F=INHX32 ;directive to define processor and file format#include <P18F4520.INC> ;processor specific variable definitions

Page 6: Directives, Memory, and Stack. Directives Special commands to the assembler May or may not generate machine code Categories by their function Programming.

Object File Directives

How to generate code in the object fileExample: RESET_VECTOR CODE Ox0000Example:

Page 7: Directives, Memory, and Stack. Directives Special commands to the assembler May or may not generate machine code Categories by their function Programming.

Control Directives

Control the assembly at the time of link process

Page 8: Directives, Memory, and Stack. Directives Special commands to the assembler May or may not generate machine code Categories by their function Programming.

ASCII TABLE

Page 9: Directives, Memory, and Stack. Directives Special commands to the assembler May or may not generate machine code Categories by their function Programming.

PIC18 Memory Space

Page 10: Directives, Memory, and Stack. Directives Special commands to the assembler May or may not generate machine code Categories by their function Programming.

8-bit CPU

ProgramMemory

Data Memory

Clock Generation

I/O Ports

Timers

Analog to Digital Converter

SerialPorts

OtherPeripherals

16 wires

8 wires

DataEEPROM

31 x 21Stack Memory

21 wires

8 wires

8 wires

Internal PIC18 Architecture

Page 11: Directives, Memory, and Stack. Directives Special commands to the assembler May or may not generate machine code Categories by their function Programming.

Program Memory

Program memory addresses are 21-bit address starting at location 0x000000.

Program memory is either PROM or EEPROM. The PROM version is called OTP (one-

time programmable) The EEPROM version is called Flash

memory. If it has flash it will have 256 bytes or

1024 bytes of data EEPROM The data EEPROM memory is

indirectly addressed through the special function register

Page 12: Directives, Memory, and Stack. Directives Special commands to the assembler May or may not generate machine code Categories by their function Programming.

Program Memory

There are three important memory locations in the program memory. 0x0000, 0x0008, and 0x0018 called vectors. Generally the GOTO instruction in

assembly language is placed at a vector location.

A vector is an address that is accessed when the reset or interrupt occurs.

The reset vector is 0x0000, the high priority interrupt vector is 0x0008, and the low priority interrupt vector is 0x0018.

Page 13: Directives, Memory, and Stack. Directives Special commands to the assembler May or may not generate machine code Categories by their function Programming.

Data Memory

Data memory is either SRAM or EEPROM.

SRAM data memory begins at 12-bit address 0x000 and ends at 12-bit address 0xFFF. Not all PIC18 versions contain 4K or data

memory space.Various PIC18 versions contain between

256 and 3968 bytes of data memory.

Page 14: Directives, Memory, and Stack. Directives Special commands to the assembler May or may not generate machine code Categories by their function Programming.

Data Memory

There are two types of registers: general-purpose registers (GPRs) special-function registers (SFRs)

GPRs are used to hold dynamic data when the PIC18 CPU is executing a program.

SFRs are registers used by the CPU and peripheral modules for controlling the desired operation of the MCU.

The upper 128 bytes of the data memory are used for special function registers (SFR) at addresses 0xF80 through 0xFFF. Some versions of the PIC18 have additional SFRs at locations below 0xF80.

Page 15: Directives, Memory, and Stack. Directives Special commands to the assembler May or may not generate machine code Categories by their function Programming.

Data Memory

Page 16: Directives, Memory, and Stack. Directives Special commands to the assembler May or may not generate machine code Categories by their function Programming.

Using BSR – Writing into file registers

0x12

0x2F0x3

a=1; bank selection

a=0; access bank

Page 17: Directives, Memory, and Stack. Directives Special commands to the assembler May or may not generate machine code Categories by their function Programming.

A Typical Instruction showing the a-bit

15 10 9 8 7 0

Op-code 8-bit data memory address

a-bit

a = 0 access banka = 1 use BSR

d-bit

d = 0 WREG

d = 1 data memory address

Page 18: Directives, Memory, and Stack. Directives Special commands to the assembler May or may not generate machine code Categories by their function Programming.

MOVLW 0x06 ;place a 0x06 into WADDLW 0x02 ;add a 0x02 to WMOVWF 0x00, 0 ;copy W to access bank register 0x00

; OR another version using the ACCESS keyword

MOVLW 0x06 ;place a 0x06 into WADDLW 0x02 ;add a 0x02 to WMOVWF 0x00, ACCESS ;copy W to access bank register 0x00

Page 19: Directives, Memory, and Stack. Directives Special commands to the assembler May or may not generate machine code Categories by their function Programming.

MOVLW 0x06 ;place a 0x06 into WADDLW 0x02 ;add a 0x02 to WMOVLB 2 ;load BSR with bank 2MOVWF 0x00, 1 ;copy W to data register 0x00

;of bank 2 or address 0x200

; OR using the BANKED keyword

MOVLW 0x06 ;place a 0x06 into WADDLW 0x02 ;add a 0x02 to WMOVLB 2 ;load BSR with 2MOVLF 0x00, BANKED ;copy W to data register 0x00

;of bank 2 or address 0x200

; OR without any bank indication

MOVLW 0x06 ;place a 0x06 into WADDLW 0x02 ;add a 0x02 to WMOVLB 2 ;load BSR with bank 2MOVWF 0x00 ;copy W to data register 0x00

;of bank 2 or address 0x200

Page 20: Directives, Memory, and Stack. Directives Special commands to the assembler May or may not generate machine code Categories by their function Programming.

MOVLW 0x7FMOVWF ADCON1 ;select all digital pins for portsMOVLW 0x00 ;place 0x00 in Port A direction registerMOVWF TRISA ;to select output operationMOVLW 0x03MOVWF PORTA ;place 0x03 on Port A

Page 21: Directives, Memory, and Stack. Directives Special commands to the assembler May or may not generate machine code Categories by their function Programming.

Data Memory

0x0000x001

0x002

0x003

0x004

Register File (Data Memory)

0xF7F

0xF7E

0xF7D

Bank Select Register (BSR)-4 bit

Accumulator (WREG)

Product High (PRODH)

Product Low (PRODL)

0xFE8

0xFE0

Major Special Function Registers

0xFF3

0xFF4

File Select Register 0 High (FSR0H)

File Select Register 0 Low (FSR0L)

File Select Register 1 High (FSR1H)

File Select Register 1 Low (FSR1L)

File Select Register 2 High (FSR2H)File Select Register 2 Low (FSR2L)

0xFEA

0xFE9

0xFE2

0xFE1

0xFD9

0xFDA

Status Register (SR)0xFD8

8-Bits

Program Counter (PC)

8-Bits

Note: - The program counter is an internal 21-bit physical register - The program counter is modified by the GOTO, CALL, RETURN, and branch instructions. The program counter is not directly addressable.

Page 22: Directives, Memory, and Stack. Directives Special commands to the assembler May or may not generate machine code Categories by their function Programming.

Initializing the RAM – indirect addressing

What is this doing?

Page 23: Directives, Memory, and Stack. Directives Special commands to the assembler May or may not generate machine code Categories by their function Programming.

Program Stack Memory

The PIC18 contains a program stack that stores up to 31 return addresses from functions.

The program stack is 21 bits in width as is the program address.

When a function is called, the return address (location of the next step in a program) is pushed onto the stack.

When the return occurs within the function, the return address is retrieved from the stack and placed into the program counter.