SlideShare a Scribd company logo
Chapter 2
Assembly
Programming I
The assembly language is a low-level programming language that is particularly useful for
Embedded Systems and Hardware Device Drivers. The Embedded Systems have fewer resources
than PCs and mainframes. Assembly programming can be useful for optimizing code for speed or
size in small embedded systems, the statements correspond directly to clock cycles and enables
easier hardware-specific task. The control of Machine Code is better than a high level language, but
the programmer must know the architecture for an exact microcontroller before he can write
effective code. It’s necessary to consider that development time is longer.
Assembly Programming………………….…………….…….………. 1
Memory Organization…..…………..………………….……………. 1
Data Memory (RAM).………………………….……………….……. 1
Program Memory (Flash/EE)……………….……………………...…. 3
Extended Data Memory (XRAM)………………………………………. 4
Data Memory (Flash/EE).……………..………………………..……. 4
Addressing Modes.……………..………………………..…………. 6
Interrupts.……………..……………...……………………..…… 6
Triggering an Interrupt by Software.……….……..…………….……. 8
Stack pointers and Interrupts.…………….……..…………….……. 8
Reset Interrupt.……….……..……………………………….…….9
Checklist for Interrupt Programs.……….……………………..………9
Example Program.………..…..……………………………….…….9
Reset Value of Stack Pointer (07H)
Memory Organization
Data Memory (RAM)
Memory Function
7FH
30H
General Purpose
(Direct or Indirect
Addressing)
2FH
.
20H
7FH
Bit Addressable Area
00H
1FH
.
18H
Register Bank 3
PSW
x x x 1 1 x x x
17H
.
10H
Register Bank 2
PSW
x x x 1 0 x x x
0FH
.
08H
Register Bank 1
PSW
x x x 0 1 x x x
07H
.
00H
Register Bank 0
PSW
x x x 0 0 x x x
Assembly Programming
The MicroConverter® series of Analog Devices
contains four different memory blocks:
✓ Data Memory (RAM): 256 x 8
✓ Extended Data Memory (XRAM): 2K x 8
✓ Program Memory (Flash/EE): 62K x 8
✓ Data Memory (Flash/EE): 4K x 8
256 bytes
2k bytes
4k bytes
62k bytes
RAM
Flash/EE
(Data)
XRAM
Flash/EE
(Program)
1
The general-purpose RAM is divided into two separate memories, the upper and the lower 128 bytes of RAM. The
lower 128 bytes of RAM can be accessed through direct or indirect addressing. The upper 128 bytes of RAM can
only be accessed through indirect addressing because it shares the same address space as the SFR space, which
can only be accessed through direct addressing.
Lower 128 bytes: The lowest 32 bytes are grouped into four banks of eight registers addressed as R0 through R7.
The next 16 bytes (128 bits), form a block of bit addressable memory space (00H – 7FH). The last block (30H-7FH)
is for general purpose.
After Reset, the SP register contains value 07H. As we push data onto the stack, the SP is incremented.
It’s a good programming technic change the SP value to higher memory space (e.g. 70H), avoiding the lost of the
Register Bank 1. The Register Bank is selected using PSW (Program Status Word)
CY (PSW.7) Carry Flag. This flag is set whenever there is a carry out from the D7 bit. Affected after an 8-bit addition
or subtraction.
AC (PSW.6) Auxiliary Carry Flag. Set when there is a carry from D3 to D4 during an addition or subtraction operation.
F0 (PSW.5) General Purpose Flag.
RS1 (PSW.4) Register Bank Selector Bit 1.
RS0 (PSW.3) Register Bank Selector Bit 0.
OV (PSW.2) Overflow Flag. This flag is set whenever the result of a signed number operation is too large.
F1 (PSW.1) General Purpose Flag.
P (PSW.0) Parity Flag. If the A register contains an odd number of 1s, then P = 1. Therefore, P = 0 if A has an even
number of 1s.
CY AC F0 RS1 RS0 OV F1 P
Assembly Programming
Assembly Programming
2
Program Memory (Flash/EE)
Program Memory is used for permanent saving program (Machine Code). Flash/EE memory combines the flexible
in-circuit reprogrammable features of EEPROM with the space efficient/ density features of EPROM which is based
on a single transistor cell architecture. Depending on the settings made in compiler, program memory may also
used to store a constant variables. After Reset, the Microconverter begins execution from address 0000H. The
following program test PSW Register. The resulting Machine Code in program memory is shown below
Assembly Programming
Assembly Programming
The above program tests the Data Memory (RAM), using indirect, direct and bit addressing , the value of the
memory locations are shown below
3
The internal XRAM (2K x 8) can be used as an extension of RAM (only 256 x 8). By default, this memory is not active.
The XRAM can be enable using CFG832 register (MOV CFG832, #01H). The transfer with XRAM Memory is
implemented using MOVX instructions. An example of use of this memory is shows below
The 4 kBytes of Flash/EE data memory are configured as 1024 pages, each of 4 bytes. A group of four data registers
(EDATA1–4) is used to hold the four bytes of data at each page. The page is addressed via the two registers, EADRH
and EADRL as is shows in the table
The ECON register is an 8-bit control register that may be written with one of nine Flash/EE memory access
commands to trigger various read, write, erase, and verify functions.
Extended Data Memory (XRAM)
Data Memory (Flash/EE)
Page Address (EADRH/L) EDATA 1 EDATA 2 EDATA 3 EDATA 4
3FFH Byte 1 ( 0FFCH) Byte 2 ( 0FFDH) Byte 3 ( 0FFEH) Byte 4 ( 0FFFH)
.. .. .. .. ..
001H Byte 1 ( 0004H) Byte 2 ( 0005H) Byte 3 ( 0006H) Byte 4 ( 0007H)
000H Byte 1 ( 0000H) Byte 2 ( 0001H) Byte 3 ( 0002H) Byte 4 ( 0003H)
Assembly Programming
Assembly Programming
4
The core cannot respond to interrupt requests until the Flash/EE operation is complete, some operations like
ERASE command, can delay until 2 msec.
Assembly Programming
Assembly Programming
5
The Vector Address represents the address of the interrupt service routine. The Interrupt Bit represents the bits
that are “set” when the equivalent interrupt is requested. We can cause an interrupt with an instruction that
“set” the interrupt flag. The Priority Level control the order in which multiple interrupts will be serviced.
In case of multiple interrupts, the following rules apply:
✓ While a low priority interrupt handler is running, if a high priority interrupt arrives, the handler will be
interrupted, and the high priority handler will run (interrupt inside interrupt).
✓ If a high priority interrupt is running, it cannot be interrupted by any other.
✓ A low-priority interrupt handler will be invoked only if no other interrupt is already executing.
✓ If two interrupts occur at the same time, the interrupt with higher priority will execute first. If both
interrupts are of the same priority, the interrupt which is higher in polling sequence will be executed
first.
Addressing Modes
Addressing Mode Assembler Instruction Example Description
Immediate MOV A, #data MOV A, #30H Transfers an 8-bit data immediately
Direct MOV A, direct MOV A, 30H 30H is the address where the operand is stored
Register Direct MOV A, Rn MOV A, R5 Registers can take value from R0,R1…to R7
Register Indirect MOV A, @Ri MOV A, @R0 R0, R1 has the address where the operand is stored
Indexed MOVC A, @A+DPTR MOVC A, @A+DPTR The source operand is @A+DPTR
Interrupts
Int. Number Interrupt Name Vector Address Interrupt Bit Priority Level
0 External Hardware (INT0) 03H IE0 3
1 Timer/Counter 0 overflow 0BH TF0 5
2 External Hardware (INT1) 13H IE1 6
3 Timer/Counter 1 overflow 1BH TF1 7
4 UART 23H RI/TI 9
5 Timer/Counter 2 overflow 2BH TF2/EXF2 10
6 End of ADC Conversion 33H ADCI 4
7 SPI/I2C 3BH ISPI/I2CI 8
8 Power Supply Monitor 43H PSMCON.5 1
9 Time Interval Counter 53H TIMECON.2 11
10 Watchdog timer 5BH WDS 2
Addressing mode is a way to address an operand. It can be a direct address of memory, register, numerical data etc.
Assembly Programming
Assembly Programming
A single microcontroller can serve several devices. There are two ways to do that: Interrupt or Polling. In the
Interrupt method, whenever any device needs its service, the device notifies the microcontroller by sending it an
interrupt signal. Upon receiving an interrupt signal, the microcontroller interrupts whatever it is doing and serves
the device. The program associated with the interrupt is called the Service Interrupt Routine (ISR). In Polling, the
microcontroller continuously monitors the status of a given device; when the status condition is met, it performs
the service. After that, it moves on to monitor the next device until each one is serviced. The advantage of
interrupts is that the microcontroller can serve many devices (not all at the same time, of course); each device
can get the attention of the microcontroller based on the priority assigned to it. The most important reason that
the interrupt method is preferable is that the polling method wastes much of the microcontroller’s time by polling
devices that do not need service. The following table shows the typical interrupts in Microconverters based on
8052 core
6
The following program shows how an External Interrupt 0 is attended
Assembly Programming
Assembly Programming
There are three interrupt-related Special Function Registers. IE (Interrupt Enable):
EA (IE.7). Enable all interrupt sources (1 = enable; 0 = disable)
EADC (IE.6). ADC Interrupt (1 = enable; 0 = disable)
ET2 (IE.5). Timer 2 interrupt (1 = enable; 0 = disable)
ES (IE.4). UART serial port interrupt (1 = enable; 0 = disable)
ET1 (IE.3). Timer 1 interrupt (1 = enable; 0 = disable)
EX1 (IE.2). External Interrupt 1 (1 = enable; 0 = disable)
ET0 (IE.1). Timer 0 interrupt (1 = enable; 0 = disable)
EX0 (IE.0). External Interrupt 0 (1 = enable; 0 = disable)
IP (Interrupt Priority):
RESERVED (IP.7). Reserved for future use
PADC (IP.6). ADC interrupt priority (1 = high; 0 = low)
PT2 (IP.5). Timer 2 interrupt priority (1 = high; 0 = low)
PS (IP.4). UART serial port interrupt priority (1 = high; 0 = low)
PT1 (IP.3). Timer 1 interrupt priority (1 = high; 0 = low)
PX1 (IP.2). External Interrupt 1 priority (1 = high; 0 = low)
PT0 (IP.1). Timer 0 interrupt priority (1 = high; 0 = low)
PX0 (IP.0). External Interrupt 0 (1 = enable; 0 = disable)
IEIP2 (SECONDARY INTERRUPT ENABLE REGISTER, No Bit Addressable)
RESERVED. Reserved for future use
PTI. Priority for time interval interrupt
PPSM. Priority for power supply monitor interrupt
PSI. Priority for SPI/I2C interrupt
RESERVED. This bit must contain 0
ETI. Enable interval counter interrupt (1 = enable; 0 = disable)
EPSMI. Enable power supply monitor interrupt (1 = enable; 0 = disable)
ESI. Enable SPI or I2C serial port interrupt (1 = enable; 0 = disable)
7
The program is stored in memory as shows bellow
The interrupt program of the above section is executed by falling edge on P3.2. The same occur when you set IE0
(SETB IE0). Set the equivalent interrupt bit is equivalent to generate an interrupt by software.
A stack operates in a last-in/first-out sense. When a new data item is entered onto the top of a stack (push), the stack
pointer increments to the next physical memory address, and the new item is copied to that address. When a data
item is pulled (pop) from the top of a stack, the item is copied from the address of the SP, and the SP decrements to
the next available item at the top of the stack. The most general types of stack-specific instructions are: PUSH (put
data onto the stack), POP (“remove” data from the stack), CALL (jump to a subroutine), RETURN (return from a
subroutine) and INTERRUP.
Triggering an Interrupt by Software
Stack Pointer and Interrupts
Assembly Programming
Assembly Programming
After RESET, the Program Counter (PC) has the value
0000H, where the Microconveter take the Operation
Code (OC) of the first instruction to be executed (JMP).
Address
ROM
Instruction Machine
code
PC
0000H JMP 000AH 02H (OC) 0000H
0001H 00H 0000H
0002H 0AH 000AH
. . . .
000AH SETB IT0 D2H (OC) 000AH
000BH 88H 000CH
000CH SETB EX0 D2H (OC) 000CH
000DH A8H 000EH
000EH SETB EA D2H (OC) 000EH
000FH AFH 0010H
0010H SETB P2.5 D2H (OC) 0010H
0011H A5H 0012H
0012H MOV
PCON,#22H
75H (OC) 0012H
0013H 87H 0015H
0014H 22H 0015H
0015H NOP 00H 0015H
0016H NOP 00H 0016H
1 1
The PC takes the value of the address where the OC
of the next instruction is stored.
2
2
3
The program Stop the Execution, waiting for an External
Interrupt 0.
The PC has the value of the address where the OC of
the next instruction is stored (0015H).
3
4
4
Address RAM Value
07H (SP)
08H (SP+1) 15 (PC, LS Byte)
09H (SP+2) 00 (PC, MS Byte)
The external interrupt 0 is activated with the falling
edge on P3.2. IE0 of TCON goes high. The Stack
Pointer (SP) is increased, and the PC is stored on Stack
(Least significant byte first).
5
5
Address
ROM
Instruction Machine
code
PC
0003H CLR P2.5 C2H (OC) 0003H
0004H A5H 0005H
0005H RETI 32 (OC) 0005H
The PC is initialized with the 0003H address ( INT0
Interrupt address of the ISR from the interrupt vector
table of). It starts to execute the interrupt service.
subroutine
6
6
7
The interrupt routine is executed until RETI instruction,
which clear IE0 of TCON, indicating that the servicing of
the interrupt is over. Get pop PC (0015H) from Stack,
decreasing SP 2 times and tacking its original value (07H)
7
8
8
The Microconverter continues the execution of the
program that was interrupted
8
✓ Vector Address: Address of the Interrupt Service Routine (ISR). Place ISR, which is terminated by RETI instruction.
✓ Unconditional jump to anywhere in program memory: There are only 8 bytes between ISR Addresses.
✓ Redefine the value of the Stack: You must ensure that there is sufficient RAM capacity in the STACK to handle
all interrupts, PUSH instructions to save registers and CALL to subroutines.
Redefine the Stack avoid the loss of Register Bank 1.
✓ Protect your registers: An interrupt must leave the processor in the same state as it was in when the interrupt
initiated. In general, your interrupt routine must protect the following registers:
• PSW
• DPTR (DPH/DPL)
• PSW
• ACC
• B
• Registers R0-R7
✓ Pop the same number of values off the stack as you pushed onto it, take care of the order (last in/first out).
✓ Prioritize interrupts properly.
✓ Keep ISR short, use flags.
✓ The interrupts must be enable by software. Enable the global interrupt control bit.
✓ External interrupts: Level or Edge triggered.
Reset Interrupt
Checklist for Interrupt Programs
Example program
Assembly Programming
Assembly ProgrammingRESET is the highest priority interrupt. A high level on RESET pin for 24 master clock cycles while the oscillator is
running resets the device. As result PC = 0000H, where must be defined the OC of the first instruction to be executed.
RESET can be executed by software, using following program
9
1
1 Vector Address (INT0)
2
2 Stack redefinition
3
3 Interrupt priority
4
4 New address for ISR
5 5External interrupt: Falling edge
66 Interrupt enable
Assembly Programming
Assembly Programming
10
4
7 Used registers are saved
7
8
8 Used registers are restored: Inverse order (last in/first out)

More Related Content

PDF
343logic-design-lab-manual-10 esl38-3rd-sem-2011
PPTX
Embedded Product Development Life Cycle(EDLC)
PDF
Đề tài: Thiết kế mô hình cân bằng con lắc ngược, HAY
PDF
Đề tài: Nghiên cứu ứng dụng lập trình điều khiển động cơ, HOT
PDF
Thiết kế hệ thống giám sát và điều khiển thiết bị công nghiệp, HAY
PPTX
PPTX
Distance Measurement by Ultrasonic Sensor
PDF
[Cntt] bài giảng kĩ thuật vi xử lí
343logic-design-lab-manual-10 esl38-3rd-sem-2011
Embedded Product Development Life Cycle(EDLC)
Đề tài: Thiết kế mô hình cân bằng con lắc ngược, HAY
Đề tài: Nghiên cứu ứng dụng lập trình điều khiển động cơ, HOT
Thiết kế hệ thống giám sát và điều khiển thiết bị công nghiệp, HAY
Distance Measurement by Ultrasonic Sensor
[Cntt] bài giảng kĩ thuật vi xử lí

What's hot (20)

DOC
Đề tài: Đo tốc độ động cơ dùng 8051, HAY, 9đ
DOC
Bao chay bao khoi
PDF
Introduction to ICS 1st Year Book
PPTX
embedded system and AVR
PDF
Tailieuhoc multisim
PDF
Đồ án điện tử công suất: Thiết kế bộ chỉnh lưu hình tia ba pha
PDF
Đề tài: Xe tự hành tránh vật cản, HAY
PDF
đồ áN tốt nghiệp điều khiển và tự động hóa robot tránh vật cản
PDF
bài giảng MẠNG CẢM BIẾN KHÔNG DÂY
ODP
Inputs of physical design
PPTX
Introduction to EDA Tools
PDF
1. Introduction to Embedded Systems & IoT
DOCX
Đề tài: Thiết kế mạch điều khiển máy bơm nước tự động, HAY
PDF
báo cáo mạch OTL vi sai
PPT
Bai giang ROBOT cong nghiep
PDF
THIẾT KẾ BỘ ĐIỀU KHIỂN SỐ KẾT HỢP KHÂU HIỆU CHỈNH MÔ HÌNH ĐIỀU KHIỂN CON LẮC ...
PDF
Bai giang cau truc may tinh
PDF
Luận văn: Xử lý ảnh video theo thời gian thực trên kit STM32, 9đ
PPTX
Tongquanktmt
PPTX
Orcad pspice intro and basics
Đề tài: Đo tốc độ động cơ dùng 8051, HAY, 9đ
Bao chay bao khoi
Introduction to ICS 1st Year Book
embedded system and AVR
Tailieuhoc multisim
Đồ án điện tử công suất: Thiết kế bộ chỉnh lưu hình tia ba pha
Đề tài: Xe tự hành tránh vật cản, HAY
đồ áN tốt nghiệp điều khiển và tự động hóa robot tránh vật cản
bài giảng MẠNG CẢM BIẾN KHÔNG DÂY
Inputs of physical design
Introduction to EDA Tools
1. Introduction to Embedded Systems & IoT
Đề tài: Thiết kế mạch điều khiển máy bơm nước tự động, HAY
báo cáo mạch OTL vi sai
Bai giang ROBOT cong nghiep
THIẾT KẾ BỘ ĐIỀU KHIỂN SỐ KẾT HỢP KHÂU HIỆU CHỈNH MÔ HÌNH ĐIỀU KHIỂN CON LẮC ...
Bai giang cau truc may tinh
Luận văn: Xử lý ảnh video theo thời gian thực trên kit STM32, 9đ
Tongquanktmt
Orcad pspice intro and basics
Ad

Similar to Assembly programming (20)

PPTX
Unit 4 Introduction to Microcontrollers.pptxUnit-3 IO Interfacing-1.pptximpor...
PDF
8085 micro processor
PDF
Notes of 8085 micro processor Programming for BCA, MCA, MSC (CS), MSC (IT) &...
PPT
Embedded System
PPT
PPT
8085 intro
PPT
8085 (1)
PPTX
module-2.pptx
PPTX
UNIT 2 PPT microprocessor presentation.pptx
PPTX
Micrprocessor unit 2 ppt This unit synchronizes all the microprocessor operat...
PDF
80386_ Bus Cycles & System Architecture.pdf
PPTX
UNIT 4.pptx
PPTX
Embedded system and Internet of things-1.pptx
PDF
assignment 1-MC.pdf
PPTX
Unit iv introduction to 8051 microcontroller ppts
PPT
PPTX
MPMC Architecture of 8085 Microprocessor and Programming.pptx
PPT
8085 Microprocessor artchitecture -ppt.ppt
PDF
Microprocessors and microcontrollers
Unit 4 Introduction to Microcontrollers.pptxUnit-3 IO Interfacing-1.pptximpor...
8085 micro processor
Notes of 8085 micro processor Programming for BCA, MCA, MSC (CS), MSC (IT) &...
Embedded System
8085 intro
8085 (1)
module-2.pptx
UNIT 2 PPT microprocessor presentation.pptx
Micrprocessor unit 2 ppt This unit synchronizes all the microprocessor operat...
80386_ Bus Cycles & System Architecture.pdf
UNIT 4.pptx
Embedded system and Internet of things-1.pptx
assignment 1-MC.pdf
Unit iv introduction to 8051 microcontroller ppts
MPMC Architecture of 8085 Microprocessor and Programming.pptx
8085 Microprocessor artchitecture -ppt.ppt
Microprocessors and microcontrollers
Ad

More from Omar Sanchez (20)

PDF
Assembler4
PDF
Assembly programming II
PDF
Assembler Programming
PDF
Functions for Nano 5 Card
PDF
Ejemplos de modelos basados en adaptacion parametrica
PDF
Sistemas numericos
PDF
Ensamblador
PDF
Nano5 features
PDF
Referencias MATLAB
PPT
Control Modelo de Referencia y Linealizacion po Realimentacion
PPTX
Fundposori
PPT
Segmentación de imagenes
PPT
Imagen e histograma
PPTX
Funciones de Lyapunov basado en Krasovskii
PPT
Sensores de robots
PPT
Vehiculos no tripulados
PPTX
Algunos aspectos de estabilidad
PPT
Control basado en modelo
PPT
Grupos próximos
PPT
Redes Neuronales
Assembler4
Assembly programming II
Assembler Programming
Functions for Nano 5 Card
Ejemplos de modelos basados en adaptacion parametrica
Sistemas numericos
Ensamblador
Nano5 features
Referencias MATLAB
Control Modelo de Referencia y Linealizacion po Realimentacion
Fundposori
Segmentación de imagenes
Imagen e histograma
Funciones de Lyapunov basado en Krasovskii
Sensores de robots
Vehiculos no tripulados
Algunos aspectos de estabilidad
Control basado en modelo
Grupos próximos
Redes Neuronales

Recently uploaded (20)

PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Cloud computing and distributed systems.
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
cuic standard and advanced reporting.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Approach and Philosophy of On baking technology
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
KodekX | Application Modernization Development
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Big Data Technologies - Introduction.pptx
PDF
Encapsulation theory and applications.pdf
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Mobile App Security Testing_ A Comprehensive Guide.pdf
Cloud computing and distributed systems.
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
cuic standard and advanced reporting.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Approach and Philosophy of On baking technology
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Understanding_Digital_Forensics_Presentation.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
KodekX | Application Modernization Development
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
20250228 LYD VKU AI Blended-Learning.pptx
Big Data Technologies - Introduction.pptx
Encapsulation theory and applications.pdf
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...

Assembly programming

  • 1. Chapter 2 Assembly Programming I The assembly language is a low-level programming language that is particularly useful for Embedded Systems and Hardware Device Drivers. The Embedded Systems have fewer resources than PCs and mainframes. Assembly programming can be useful for optimizing code for speed or size in small embedded systems, the statements correspond directly to clock cycles and enables easier hardware-specific task. The control of Machine Code is better than a high level language, but the programmer must know the architecture for an exact microcontroller before he can write effective code. It’s necessary to consider that development time is longer.
  • 2. Assembly Programming………………….…………….…….………. 1 Memory Organization…..…………..………………….……………. 1 Data Memory (RAM).………………………….……………….……. 1 Program Memory (Flash/EE)……………….……………………...…. 3 Extended Data Memory (XRAM)………………………………………. 4 Data Memory (Flash/EE).……………..………………………..……. 4 Addressing Modes.……………..………………………..…………. 6 Interrupts.……………..……………...……………………..…… 6 Triggering an Interrupt by Software.……….……..…………….……. 8 Stack pointers and Interrupts.…………….……..…………….……. 8 Reset Interrupt.……….……..……………………………….…….9 Checklist for Interrupt Programs.……….……………………..………9 Example Program.………..…..……………………………….…….9
  • 3. Reset Value of Stack Pointer (07H) Memory Organization Data Memory (RAM) Memory Function 7FH 30H General Purpose (Direct or Indirect Addressing) 2FH . 20H 7FH Bit Addressable Area 00H 1FH . 18H Register Bank 3 PSW x x x 1 1 x x x 17H . 10H Register Bank 2 PSW x x x 1 0 x x x 0FH . 08H Register Bank 1 PSW x x x 0 1 x x x 07H . 00H Register Bank 0 PSW x x x 0 0 x x x Assembly Programming The MicroConverter® series of Analog Devices contains four different memory blocks: ✓ Data Memory (RAM): 256 x 8 ✓ Extended Data Memory (XRAM): 2K x 8 ✓ Program Memory (Flash/EE): 62K x 8 ✓ Data Memory (Flash/EE): 4K x 8 256 bytes 2k bytes 4k bytes 62k bytes RAM Flash/EE (Data) XRAM Flash/EE (Program) 1 The general-purpose RAM is divided into two separate memories, the upper and the lower 128 bytes of RAM. The lower 128 bytes of RAM can be accessed through direct or indirect addressing. The upper 128 bytes of RAM can only be accessed through indirect addressing because it shares the same address space as the SFR space, which can only be accessed through direct addressing. Lower 128 bytes: The lowest 32 bytes are grouped into four banks of eight registers addressed as R0 through R7. The next 16 bytes (128 bits), form a block of bit addressable memory space (00H – 7FH). The last block (30H-7FH) is for general purpose. After Reset, the SP register contains value 07H. As we push data onto the stack, the SP is incremented.
  • 4. It’s a good programming technic change the SP value to higher memory space (e.g. 70H), avoiding the lost of the Register Bank 1. The Register Bank is selected using PSW (Program Status Word) CY (PSW.7) Carry Flag. This flag is set whenever there is a carry out from the D7 bit. Affected after an 8-bit addition or subtraction. AC (PSW.6) Auxiliary Carry Flag. Set when there is a carry from D3 to D4 during an addition or subtraction operation. F0 (PSW.5) General Purpose Flag. RS1 (PSW.4) Register Bank Selector Bit 1. RS0 (PSW.3) Register Bank Selector Bit 0. OV (PSW.2) Overflow Flag. This flag is set whenever the result of a signed number operation is too large. F1 (PSW.1) General Purpose Flag. P (PSW.0) Parity Flag. If the A register contains an odd number of 1s, then P = 1. Therefore, P = 0 if A has an even number of 1s. CY AC F0 RS1 RS0 OV F1 P Assembly Programming Assembly Programming 2
  • 5. Program Memory (Flash/EE) Program Memory is used for permanent saving program (Machine Code). Flash/EE memory combines the flexible in-circuit reprogrammable features of EEPROM with the space efficient/ density features of EPROM which is based on a single transistor cell architecture. Depending on the settings made in compiler, program memory may also used to store a constant variables. After Reset, the Microconverter begins execution from address 0000H. The following program test PSW Register. The resulting Machine Code in program memory is shown below Assembly Programming Assembly Programming The above program tests the Data Memory (RAM), using indirect, direct and bit addressing , the value of the memory locations are shown below 3
  • 6. The internal XRAM (2K x 8) can be used as an extension of RAM (only 256 x 8). By default, this memory is not active. The XRAM can be enable using CFG832 register (MOV CFG832, #01H). The transfer with XRAM Memory is implemented using MOVX instructions. An example of use of this memory is shows below The 4 kBytes of Flash/EE data memory are configured as 1024 pages, each of 4 bytes. A group of four data registers (EDATA1–4) is used to hold the four bytes of data at each page. The page is addressed via the two registers, EADRH and EADRL as is shows in the table The ECON register is an 8-bit control register that may be written with one of nine Flash/EE memory access commands to trigger various read, write, erase, and verify functions. Extended Data Memory (XRAM) Data Memory (Flash/EE) Page Address (EADRH/L) EDATA 1 EDATA 2 EDATA 3 EDATA 4 3FFH Byte 1 ( 0FFCH) Byte 2 ( 0FFDH) Byte 3 ( 0FFEH) Byte 4 ( 0FFFH) .. .. .. .. .. 001H Byte 1 ( 0004H) Byte 2 ( 0005H) Byte 3 ( 0006H) Byte 4 ( 0007H) 000H Byte 1 ( 0000H) Byte 2 ( 0001H) Byte 3 ( 0002H) Byte 4 ( 0003H) Assembly Programming Assembly Programming 4
  • 7. The core cannot respond to interrupt requests until the Flash/EE operation is complete, some operations like ERASE command, can delay until 2 msec. Assembly Programming Assembly Programming 5
  • 8. The Vector Address represents the address of the interrupt service routine. The Interrupt Bit represents the bits that are “set” when the equivalent interrupt is requested. We can cause an interrupt with an instruction that “set” the interrupt flag. The Priority Level control the order in which multiple interrupts will be serviced. In case of multiple interrupts, the following rules apply: ✓ While a low priority interrupt handler is running, if a high priority interrupt arrives, the handler will be interrupted, and the high priority handler will run (interrupt inside interrupt). ✓ If a high priority interrupt is running, it cannot be interrupted by any other. ✓ A low-priority interrupt handler will be invoked only if no other interrupt is already executing. ✓ If two interrupts occur at the same time, the interrupt with higher priority will execute first. If both interrupts are of the same priority, the interrupt which is higher in polling sequence will be executed first. Addressing Modes Addressing Mode Assembler Instruction Example Description Immediate MOV A, #data MOV A, #30H Transfers an 8-bit data immediately Direct MOV A, direct MOV A, 30H 30H is the address where the operand is stored Register Direct MOV A, Rn MOV A, R5 Registers can take value from R0,R1…to R7 Register Indirect MOV A, @Ri MOV A, @R0 R0, R1 has the address where the operand is stored Indexed MOVC A, @A+DPTR MOVC A, @A+DPTR The source operand is @A+DPTR Interrupts Int. Number Interrupt Name Vector Address Interrupt Bit Priority Level 0 External Hardware (INT0) 03H IE0 3 1 Timer/Counter 0 overflow 0BH TF0 5 2 External Hardware (INT1) 13H IE1 6 3 Timer/Counter 1 overflow 1BH TF1 7 4 UART 23H RI/TI 9 5 Timer/Counter 2 overflow 2BH TF2/EXF2 10 6 End of ADC Conversion 33H ADCI 4 7 SPI/I2C 3BH ISPI/I2CI 8 8 Power Supply Monitor 43H PSMCON.5 1 9 Time Interval Counter 53H TIMECON.2 11 10 Watchdog timer 5BH WDS 2 Addressing mode is a way to address an operand. It can be a direct address of memory, register, numerical data etc. Assembly Programming Assembly Programming A single microcontroller can serve several devices. There are two ways to do that: Interrupt or Polling. In the Interrupt method, whenever any device needs its service, the device notifies the microcontroller by sending it an interrupt signal. Upon receiving an interrupt signal, the microcontroller interrupts whatever it is doing and serves the device. The program associated with the interrupt is called the Service Interrupt Routine (ISR). In Polling, the microcontroller continuously monitors the status of a given device; when the status condition is met, it performs the service. After that, it moves on to monitor the next device until each one is serviced. The advantage of interrupts is that the microcontroller can serve many devices (not all at the same time, of course); each device can get the attention of the microcontroller based on the priority assigned to it. The most important reason that the interrupt method is preferable is that the polling method wastes much of the microcontroller’s time by polling devices that do not need service. The following table shows the typical interrupts in Microconverters based on 8052 core 6
  • 9. The following program shows how an External Interrupt 0 is attended Assembly Programming Assembly Programming There are three interrupt-related Special Function Registers. IE (Interrupt Enable): EA (IE.7). Enable all interrupt sources (1 = enable; 0 = disable) EADC (IE.6). ADC Interrupt (1 = enable; 0 = disable) ET2 (IE.5). Timer 2 interrupt (1 = enable; 0 = disable) ES (IE.4). UART serial port interrupt (1 = enable; 0 = disable) ET1 (IE.3). Timer 1 interrupt (1 = enable; 0 = disable) EX1 (IE.2). External Interrupt 1 (1 = enable; 0 = disable) ET0 (IE.1). Timer 0 interrupt (1 = enable; 0 = disable) EX0 (IE.0). External Interrupt 0 (1 = enable; 0 = disable) IP (Interrupt Priority): RESERVED (IP.7). Reserved for future use PADC (IP.6). ADC interrupt priority (1 = high; 0 = low) PT2 (IP.5). Timer 2 interrupt priority (1 = high; 0 = low) PS (IP.4). UART serial port interrupt priority (1 = high; 0 = low) PT1 (IP.3). Timer 1 interrupt priority (1 = high; 0 = low) PX1 (IP.2). External Interrupt 1 priority (1 = high; 0 = low) PT0 (IP.1). Timer 0 interrupt priority (1 = high; 0 = low) PX0 (IP.0). External Interrupt 0 (1 = enable; 0 = disable) IEIP2 (SECONDARY INTERRUPT ENABLE REGISTER, No Bit Addressable) RESERVED. Reserved for future use PTI. Priority for time interval interrupt PPSM. Priority for power supply monitor interrupt PSI. Priority for SPI/I2C interrupt RESERVED. This bit must contain 0 ETI. Enable interval counter interrupt (1 = enable; 0 = disable) EPSMI. Enable power supply monitor interrupt (1 = enable; 0 = disable) ESI. Enable SPI or I2C serial port interrupt (1 = enable; 0 = disable) 7
  • 10. The program is stored in memory as shows bellow The interrupt program of the above section is executed by falling edge on P3.2. The same occur when you set IE0 (SETB IE0). Set the equivalent interrupt bit is equivalent to generate an interrupt by software. A stack operates in a last-in/first-out sense. When a new data item is entered onto the top of a stack (push), the stack pointer increments to the next physical memory address, and the new item is copied to that address. When a data item is pulled (pop) from the top of a stack, the item is copied from the address of the SP, and the SP decrements to the next available item at the top of the stack. The most general types of stack-specific instructions are: PUSH (put data onto the stack), POP (“remove” data from the stack), CALL (jump to a subroutine), RETURN (return from a subroutine) and INTERRUP. Triggering an Interrupt by Software Stack Pointer and Interrupts Assembly Programming Assembly Programming After RESET, the Program Counter (PC) has the value 0000H, where the Microconveter take the Operation Code (OC) of the first instruction to be executed (JMP). Address ROM Instruction Machine code PC 0000H JMP 000AH 02H (OC) 0000H 0001H 00H 0000H 0002H 0AH 000AH . . . . 000AH SETB IT0 D2H (OC) 000AH 000BH 88H 000CH 000CH SETB EX0 D2H (OC) 000CH 000DH A8H 000EH 000EH SETB EA D2H (OC) 000EH 000FH AFH 0010H 0010H SETB P2.5 D2H (OC) 0010H 0011H A5H 0012H 0012H MOV PCON,#22H 75H (OC) 0012H 0013H 87H 0015H 0014H 22H 0015H 0015H NOP 00H 0015H 0016H NOP 00H 0016H 1 1 The PC takes the value of the address where the OC of the next instruction is stored. 2 2 3 The program Stop the Execution, waiting for an External Interrupt 0. The PC has the value of the address where the OC of the next instruction is stored (0015H). 3 4 4 Address RAM Value 07H (SP) 08H (SP+1) 15 (PC, LS Byte) 09H (SP+2) 00 (PC, MS Byte) The external interrupt 0 is activated with the falling edge on P3.2. IE0 of TCON goes high. The Stack Pointer (SP) is increased, and the PC is stored on Stack (Least significant byte first). 5 5 Address ROM Instruction Machine code PC 0003H CLR P2.5 C2H (OC) 0003H 0004H A5H 0005H 0005H RETI 32 (OC) 0005H The PC is initialized with the 0003H address ( INT0 Interrupt address of the ISR from the interrupt vector table of). It starts to execute the interrupt service. subroutine 6 6 7 The interrupt routine is executed until RETI instruction, which clear IE0 of TCON, indicating that the servicing of the interrupt is over. Get pop PC (0015H) from Stack, decreasing SP 2 times and tacking its original value (07H) 7 8 8 The Microconverter continues the execution of the program that was interrupted 8
  • 11. ✓ Vector Address: Address of the Interrupt Service Routine (ISR). Place ISR, which is terminated by RETI instruction. ✓ Unconditional jump to anywhere in program memory: There are only 8 bytes between ISR Addresses. ✓ Redefine the value of the Stack: You must ensure that there is sufficient RAM capacity in the STACK to handle all interrupts, PUSH instructions to save registers and CALL to subroutines. Redefine the Stack avoid the loss of Register Bank 1. ✓ Protect your registers: An interrupt must leave the processor in the same state as it was in when the interrupt initiated. In general, your interrupt routine must protect the following registers: • PSW • DPTR (DPH/DPL) • PSW • ACC • B • Registers R0-R7 ✓ Pop the same number of values off the stack as you pushed onto it, take care of the order (last in/first out). ✓ Prioritize interrupts properly. ✓ Keep ISR short, use flags. ✓ The interrupts must be enable by software. Enable the global interrupt control bit. ✓ External interrupts: Level or Edge triggered. Reset Interrupt Checklist for Interrupt Programs Example program Assembly Programming Assembly ProgrammingRESET is the highest priority interrupt. A high level on RESET pin for 24 master clock cycles while the oscillator is running resets the device. As result PC = 0000H, where must be defined the OC of the first instruction to be executed. RESET can be executed by software, using following program 9 1 1 Vector Address (INT0) 2 2 Stack redefinition 3 3 Interrupt priority 4 4 New address for ISR 5 5External interrupt: Falling edge 66 Interrupt enable
  • 12. Assembly Programming Assembly Programming 10 4 7 Used registers are saved 7 8 8 Used registers are restored: Inverse order (last in/first out)