COMP2121: Microprocessors and Interfacing

Similar documents
AVR ISA & AVR Programming (I) Lecturer: Sri Parameswaran Notes by: Annie Guo

AVR ISA & AVR Programming (I) Lecturer: Sri Parameswaran Notes by: Annie Guo

AVR ISA & AVR Programming (I)

Module 8: Atmega32 Stack & Subroutine. Stack Pointer Subroutine Call function

APPENDIX B AVR INSTRUCTIONS EXPLAINED OVERVIEW

Assembly Programming (III)

COMP2121: Microprocessors and Interfacing. AVR Assembly Programming (I) Basic AVR Instructions

8-bit Instruction Set

Assembly Programming (III) Lecturer: Sri Parameswaran Notes by: Annie Guo Dr. Hui Wu

8-bit Instruction Set

COMP3221: Microprocessors and Embedded Systems

COMP2121: Microprocessors and Interfacing. Instruction Formats and Addressing Modes

AVR Control Transfer -AVR Branching

By: Dr. Hamed Saghaei

EE 308: Microcontrollers

CN310 Microprocessor Systems Design

Chapter 4: Atmel s AVR 8-bit Microcontroller Part 1 Assembly Programming

AVR Subroutine Basics

COMP2121: Microprocessors and Interfacing

AVR Instruction Set Encoding

Interrupts (II) Lecturer: Sri Parameswaran Notes by: Annie Guo

Microprocessors & Interfacing

Review on Lecture-1. ICT 6641: Advanced Embedded System. Lecture 2 Branch, Call and Delay Loops, AVR I/O port programming

COMP3221: Microprocessors and Embedded Systems

ECE 375 Computer Organization and Assembly Language Programming Winter 2018 Solution Set #2

EE 308: Microcontrollers

AT90S Bit Microcontroller with 1K bytes Downloadable Flash AT90S1200. Features. Description. Pin Configuration

COMP2121: Microprocessors and Interfacing

[TUT] Newbie's Guide to AVR Interrupts

Using SRAM in AVR assembler language

Gates and flip-flops: glue logic, simple FSMs, registers Two-level PLDs: FSMs, muxes, decoders. Programmable logic devices (CSE370, CSE467)

COMP2121: Microprocessors and Interfacing. I/O Devices (II)

Programming. A. Assembly Language Programming. A.1 Machine Code. Machine Code Example: Motorola ADD

EE 308: Microcontrollers

Today s Menu. >Use the Internal Register(s) >Use the Program Memory Space >Use the Stack >Use global memory

COMP3221: Microprocessors and. and Embedded Systems. Overview. Variable Types and Memory Sections. Types of Variables in C

8-Bit Microcontroller with 1K bytes In-System Programmable Flash AT90S1200. Features. Description. Pin Configuration

8-bit Microcontroller with 8K Bytes Programmable Flash AT90C8534. Preliminary

APPENDIX A FOR SKEE3732 LABORATORY 1 SHEET

;Compiler Options.NOLIST.INCLUDE "C:\Program Files (x86)\atmel\avr Tools\AvrAssembler2\Appnotes\m8515def.inc"

COMP2121: Microprocessors and Interfacing

Building A RISC Microcontroller in an FPGA. Yap Zi He

8-Bit Microcontroller with 2K Bytes of In-System Programmable Flash. AT90S2323 AT90LS2323 AT90S2343 AT90LS2343 Preliminary AT90S/LS2323.

Lecture 20: AVR Programming, Continued. AVR Program Visible State (ones we care about for now)

COMP2121: Microprocessors and Interfacing. Instruction Set Architecture (ISA)

Microprocessors 1. The 8051 Instruction Set. Microprocessors 1 1. Msc. Ivan A. Escobar Broitman

Data Transfer Instructions

ก AVR Microcontrollers

AVR Assembler Examples

Microcontroller Intel [Instruction Set]

Q. Classify the instruction set of 8051 and list out the instructions in each type.

8-Bit Microcontroller with 2K bytes Downloadable Flash. AT90S2313 Preliminary. Features. Description. Pin Configuration

Microprocessors & Interfacing

It is possible to define a number using a character or multiple numbers (see instruction DB) by using a string.

Interrupts (I) Lecturer: Sri Notes by Annie Guo. Week8 1

AVR and MeggyJr Simple

DESIGN NOTE #032. AVR Boot Loader. Introduction. Overview AUTHOR: MARIANO BARRÓN RUIZ KEYWORDS: BOOT LOADER, SPM, SELF-PROGRAMMING

8085 INSTRUCTION SET INSTRUCTION DETAILS

Grundlagen Microcontroller Processor Core. Günther Gridling Bettina Weiss

Input/Output Devices. Lecturer: Sri Parameswaran Notes by: Annie Guo

8-bit Microcontroller with 2K Bytes of In-System Programmable Flash. ATtiny22 ATtiny22L. Preliminary. Features. Description

CprE 288 Introduction to Embedded Systems ARM Assembly Programming: Translating C Control Statements and Function Calls

Buses and Parallel Input/Output

Mechatronics and Microcomputers. Stipendium Hungaricum 2018/2019 Autumn Semester Szilárd Aradi, PhD

Digital Systems - Mini AVR 3

ECED 3204 Microprocessor Midterm Reference Solution

Logic Instructions and Programs READING

Microprocessors & Interfacing

MAHALAKSHMI ENGINEERING COLLEGE TIRUCHIRAPALLI

8-bit Microcontroller with 2K Bytes of In-System Programmable Flash AT90S2323 AT90LS2323 AT90S2343 AT90S/LS2323. Features.


CprE 288 Introduction to Embedded Systems Course Review for Exam 3. Instructors: Dr. Phillip Jones

Motion Sensing with the Pyroeletric Sensor

Microcontroller VU

!" # Today Class administration, overview of course web, and logistics What is an embedded system? What will we be doing in this class?

Xuan Guo. Lecture XIX: Subroutines (2) CSC 3210 Computer Organization and Programming Georgia State University. March 31, 2015.

(2) Explain the addressing mode of OR What do you mean by addressing mode? Explain diff. addressing mode for 8085 with examples.

ECED3204: Microprocessor Part I--Introduction

Part 7. Stacks. Stack. Stack. Examples of Stacks. Stack Operation: Push. Piles of Data. The Stack

8-bit Microcontroller with 2K Bytes of In-System Programmable Flash. ATtiny22L. Preliminary

CprE 288 Translating C Control Statements and Function Calls, Loops, Interrupt Processing. Instructors: Dr. Phillip Jones Dr.

Computer Systems Lecture 9

SOEN228, Winter Revision 1.2 Date: October 25,

Trap Vector Table. Interrupt Vector Table. Operating System and Supervisor Stack. Available for User Programs. Device Register Addresses

Computer Organization & Assembly Language Programming. CSE 2312 Lecture 15 Addressing and Subroutine

Programming Model 2 A. Introduction

Accumulator and memory instructions 1. Loads, stores, and transfers 2. Arithmetic operations 3. Multiply and divide 4. Logical operations 5. Data test

appendix a The LC-3 ISA A.1 Overview

OSIAC Read OSIAC 5362 posted on the course website

8051 Overview and Instruction Set

Course Administration

ECE232: Hardware Organization and Design

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

Assembly AVR Boot loader

University of Florida EEL 4744 Dr. Eric M. Schwartz. Page 1/11 Revision 0 20-Feb-14 Mixed C and Assembly (for Atmel XMEGA)

AT90S Bit Microcontroller with 8K bytes Downloadable Flash. AT90S8515 Preliminary. Features. Description. Pin Configurations

Register-Level Programming

AVR MICROCONTROLLER ARCHITECTURTE

Programming of 8085 microprocessor and 8051 micro controller Study material

CPU: SOFTWARE ARCHITECTURE INSTRUCTION SET (PART

Transcription:

Interfacing Lecture 9: Program Control Instructions http://www.cse.unsw.edu.au/~cs2121 Lecturer: Hui Wu Session 1, 2006 Program control instructions in AVR Stacks Overview Sample AVR assembly programs using program control instructions 1 2 Motivations Arithmetic and logic instructions cannot change the program control flow. How to implement if some condition holds then do task A else do task B? How to call a subroutine? How to return to the caller from a function (subroutine)? How to return from an interrupt handler? Selected Program Control Instructions Unconditional jump: jmp, rjmp, ijmp Subroutine call: rcall, icall, call Subroutine and interrupt return: ret, reti Conditional branching: breq, brne, brsh, brlo, brge, brlt, brvs, brvc, brie, brid Refer to AVR Instruction Set for a complete list. 3 4

Jump Relative Jump Syntax: jmp k Operands: 0 k < 4M Operation: PC k Encoding: 1001 010k kkkk 110k kkkk kkkk kkkk kkkk Words: 2 Cycles: 3 mov r1, r0 ; Copy r0 to r1 jmp farplc ; Unconditional jump farplc: inc r20 ; Jump destination Syntax: rjmp k Operands: -2Kk < 2K Operation: PC PC+k+1 Encoding: 1100 kkkk kkkk kkkk Cycles: 2 cpi r16, $42 ; Compare r16 to $42 brne error ; Branch to error if r16 $42 rjmp ok ; jump to ok error: add r16, r17 ; Add r17 to r16 inc r16 ; Increment r16 ok: mov r2, r20 ; Jump destination 5 6 Indirect Jump Indirect Jump (Cont.) Syntax: ijmp Operation: (i) PC Z(15:0) Devices with 16 bits PC, 128K bytes program memory maximum. (ii) PC(15:0) Z(15:0)Devices with 22 bits PC, 8M bytes program memory maximum. PC(21:16) <- 0 Encoding: 1001 0100 0000 1001 Cycles: 2 7 clr r10 ldi r20, 2 ldi r30, low(lab<<1) ldi r31, high(lab<<1) add r30, r20 adc r31, r10 lpm r0, Z+ lpm r1, Z movw r31:r30, r1:r0 ijmp Lab:.dw jt_l0.dw jt_l1 jt_l0: nop jt_l1: nop ; Clear r10 ; Load jump table offset ; High byte of the starting address (base) of jump table ; Low byte of the starting address (base) of jump table ; Base + offset is the address of the jump table entry ; Load low byte of the the jump table entry ; Load high byte of the jump table entry ; Set the pointer register Z to point the target instruction ; Jump to the target instruction ; The first entry of the jump table ; The second entry of the jump table 8

Stacks Stacks (Cont.) A stack is an area of memory that supports two operations push put something on the top of the stack A stack will grow after push is executed. pop take something off the top of the stack (LIFO last in, first out) Every processor has a stack of some kind Used for procedure calls (or subroutines) and interrupts Used to store local variables in C Special register called a Stack Pointer (SP) stores the address of the top of the stack A stack will shrink after pop is executed. A stack may grow upwards (from a lower address to a higher address) or downwards (from a higher address to a lower address). The direction in which a stack grows is determined by the hardware. 9 10 AVR and Stacks AVR Stack Initialization Stacks are part of SRAM space. Stacks grow downwards (from a higher address to a lower address). SP needs to hold addresses (therefore 16 bits wide). Made up of two 8 bit registers o SPH (high byte) (IO register $3E) o SPL (low byte) (IO register $3D) First thing to do in any program is to initialize the stack pointer. Typically stacks use the top of SRAM space. 0 1 RAMEND 1 RAMEND SP.include "m64def.inc".def temp=r20.cseg ldi temp, low(ramend) out spl, temp ldi temp, high(ramend) out sph, temp 11 12

AVR Stack Operations AVR Stack Operations (Cont.).include "m64def.inc".include "m64def.inc" 0 1.def temp=r20.cseg 0 1.def temp=r20.cseg ldi temp, low(ramend) ldi temp, low(ramend) out spl, temp out spl, temp ldi temp, high(ramend) ldi temp, high(ramend) out sph, temp out sph, temp RAMEND 1 RAMEND SP 0xff ldi r1, 0xff push r1 RAMEND 1 RAMEND SP 0xff ldi r1, 0xff push r1 pop r2 ; r2=0xff 13 14 Relative Call to Subroutine Relative Call to Subroutine (Cont.) Syntax: rcall k Operands: -2Kk < 2K Operation: (i) STACK PC + 1 (Store return address) (ii) SP SP 2 (2 bytes, 16 bits) for devices with 16 bits PC SP SP 3 (3 bytes, 22 bits) for devices with 22 bits PC (iii) PC PC + k + 1. Encoding: 1101 kkkk kkkk kkkk Cycles: 3 (Devices with 16-bit PC) 4 (Devices with 22-bit PC) rcall routine ; Call subroutine routine: push r14 ; Save r14 on the stack push r15 ; Save r15 on the stack ; Put the code for the subroutine here. pop r15 ; Restore r15 pop r14 ; Restore r14 ret ; Return from subroutine 15 16

Indirect Call to Subroutine Syntax: icall Operation: (i) STACK PC + 1 (Store return address) (ii) SP SP 2 (2 bytes, 16 bits) for devices with 16 bits PC SP SP 3 (3 bytes, 22 bits) for devices with 22 bits PC (iii) PC(15:0) Z(15:0) for devices with 16 bits PC PC(15:0) Z(15:0) and PC(21:16) 0 for devices with 22 bits PC. Encoding: 1001 0101 0000 1001 Cycles: 3 (Devices with 16-bit PC) 4 (Devices with 22-bit PC) 17 Indirect Call to Subroutine (Cont.) clr r10 ldi r20, 2 ldi r30, low(lab<<1) ldi r31, high(lab<<1) add r30, r20 adc r31, r10 lpm r0, Z+ lpm r1, Z movw r31:r30, r1:r0 icall Lab:.dw ct_l0.dw ct_l1 ct_l0: nop ct_l1: nop ; Clear r10 ; Load call table offset ; High byte of the starting address (base) of call table ; Low byte of the starting address (base) of call table ; Base + offset is the address of the call table entry ; Load low byte of the the call table entry ; Load high byte of the call table entry ; Set the pointer register Z to point the target function ; Call the target function ; The first entry of the call table ; The second entry of the call table 18 Long Call to Subroutine Long Call to Subroutine (Cont.) Syntax: call k Operands: 0k < 64K Operation: (i) STACK PC + 1 (Store return address) (ii) SP SP 2 (2 bytes, 16 bits) for devices with 16 bits PC SP SP 3 (3 bytes, 22 bits) for devices with 22 bits PC (iii) PC k. Encoding: 1001 010k kkkk 111k kkkk kkkk kkkk kkkk Words: 2 Cycles: 4 (Devices with 16-bit PC) 5 (Devices with 22-bit PC) mov r16, r0 call check nop check: cpi r16, $42 breq error error: ldi r1, 1 ret ; Copy r0 to r16 ; Call subroutine ; Continue (do nothing) ; Check if r16 has a special value ; Branch if equal ; put the code for handling the error here ; Return from subroutine 19 20

Return from Subroutine Syntax: ret Operation: (i) SP SP + 2 (2 bytes, 16 bits) for devices with 16 bits PC SP SP + 3 (3 bytes, 22 bits) for devices with 22 bits PC (ii) PC(15:0) STACK for devices with 16 bits PC PC(21:0) STACK Devices with 22 bits PC Encoding: 1001 0101 0000 1000 Cycles: 4 (Devices with 16-bit PC) 5 (Devices with 22-bit PC) routine: push r14 ; Save r14 on the stack ; Put the code for the subroutine here. pop r14 ; Restore r14 ret ; Return from subroutine Return from Interrupt Syntax: reti Operation: (i) SP SP + 2 (2 bytes, 16 bits) for devices with 16 bits PC SP SP + 3 (3 bytes, 22 bits) for devices with 22 bits PC (ii) Set global interrupt flag I (Bit 7 of the Program Status Register). (ii) PC(15:0) STACK for devices with 16 bits PC PC(21:0) STACK Devices with 22 bits PC Flag affected: I 1 Encoding: 1001 0101 0001 1000 Cycles: 4 (Devices with 16-bit PC) 5 (Devices with 22-bit PC) 21 22 Return from Interrupt (Cont.) Branch If Equal extint: push r0 ; Save r0 on the stack pop r0 ; Restore r0 reti ; Return and enable interrupts Will cover details later Syntax: breq k Operation: If Rd = Rr (Z = 1) then PC PC + k + 1, else PC PC + 1 Encoding: 1111 00kk kkkk k001 cp r1, r0 ; Compare registers r1 and r0 breq equal ; Branch if registers equal equal: nop 23 24

Branch If Same or Higher (Unsigned) Syntax: brsh k Operation: if Rd Rr (unsigned comparison) then PC PC + k + 1, else PC PC + 1 Flag affected: none Encoding: 1111 01kk kkkk k000 sbi r26, $56 ; subtract $56 from r26 brsh test ; branch if r26 $56 Test: nop ; branch destination 25 Branch If Lower (Unsigned) Syntax: brlo k Operation: If Rd < Rr (unsigned comparison) then PC PC + k + 1, else PC PC + 1 Encoding: 1111 00kk kkkk k000 eor r19, r19 ; Clear r19 loop: inc r19 ; Increase r19 cpi r19, $10 ; Compare r19 with $10 brlo loop ; Branch if r19 < $10 (unsigned) nop ; Exit from loop (do nothing) 26 Branch If Less Than (Signed) Branch If Greater or Equal (Signed) Syntax: brlt k Operation: If Rd < Rr (signed comparison) then PC PC + k + 1, else PC PC + 1 Encoding: 1111 00kk kkkk k100 cp r16, r1 ; Compare r16 to r1 brlt less ; Branch if r16 < r1 (signed) less: nop Syntax: brge k Operation: If Rd Rr (signed comparison) then PC PC + k + 1, else PC PC + 1 Encoding: 1111 01kk kkkk k100 cp r11, r12 ; Compare registers r11 and r12 brge greateq ; Branch if r11 r12 (signed) greateq: nop 27 28

Branch If Overflow Set Syntax: brvs k Operation: If V=1 then PC PC + k + 1, else PC PC + 1 Encoding: 1111 00kk kkkk k011 Branch If Overflow Clear Syntax: brvc k Operation: If V=0 then PC PC + k + 1, else PC PC + 1 Encoding: 1111 01kk kkkk k011 add r3, r4 brvs overfl overfl: nop ; Add r4 to r3 ; Branch if overflow add r3, r4 brvs noover noover: nop ; Add r4 to r3 ; Branch if no overflow 29 30 Branch if Global Interrupt is Enabled Syntax: brie k Operation: If I=1 then PC PC + k + 1, else PC PC + 1 Encoding: 1111 00kk kkkk k111 Branch if Global Interrupt is Disabled Syntax: brid k Operation: If I=0 then PC PC + k + 1, else PC PC + 1 Encoding: 1111 00kk kkkk k111 brvs inten inten: nop ; Branch if the global interrupt is enabled brid intdis intdis: nop ; Branch if the global interrupt is enabled 31 32

Reading Material 1. AVR Instruction Set. 33