COMP3221: Microprocessors and Embedded Systems

Slides:



Advertisements
Similar presentations
COMP3221: Microprocessors and Embedded Systems
Advertisements

Deeper Assembly: Addressing, Conditions, Branching, and Loops
ECE 353 Introduction to Microprocessor Systems Michael G. Morrow, P.E. Week 6.
Msc. Ivan A. Escobar Broitman Microprocessors 1 1 The 8051 Instruction Set.
COMP3221: Microprocessors and Embedded Systems--Lecture 7 1 COMP3221: Microprocessors and Embedded Systems Lecture 7: Arithmetic and logic Instructions.
COMP3221: Microprocessors and Embedded Systems--Lecture 7 1 COMP3221: Microprocessors and Embedded Systems Lecture 7: Arithmetic and logic Instructions.
COMP3221: Microprocessors and Embedded Systems Lecture 16: Interrupts II Lecturer: Hui Wu Session 2, 2005.
COMP3221: Microprocessors and Embedded Systems Lecture 12: Functions I Lecturer: Hui Wu Session 2, 2005.
CSC Timers Since this is a microcontroller it mainly finds itself in embedded devices Quite often embedded devices need to synchronize events The.
COMP3221: Microprocessors and Embedded Systems--Lecture 8 1 COMP3221: Microprocessors and Embedded Systems Lecture 8: Program Control Instructions
COMP3221: Microprocessors and Embedded Systems Lecture 2: Instruction Set Architecture (ISA) Lecturer: Hui Wu Session.
COMP3221: Microprocessors and Embedded Systems--Lecture 4 1 COMP3221: Microprocessors and Embedded Systems Lecture 4: Number Systems (II)
Chapter 9 & 10 Subroutines and Interrupts. JSR Instruction: JSR offset (11 bit) xxxxxxxxxxx [PC ]  R7, JMP Offset Jump to Subroutine at offset.
COMP3221: Microprocessors and Embedded Systems
COMP3221: Microprocessors and Embedded Systems--Lecture 9 1 COMP3221: Microprocessors and Embedded Systems Lecture 9: Data Transfer Instructions
COMP3221: Microprocessors and Embedded Systems Lecture 13: Functions II Lecturer: Hui Wu Session 2, 2005.
Embedded Systems 7763B Mt Druitt College of TAFE
CS-280 Dr. Mark L. Hornick 1 Calling subroutines in assembly And using the Stack.
ECE 265 – LECTURE 8 The M68HC11 Basic Instruction Set The remaining instructions 10/20/ ECE265.
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
Lecture Set 4 Programming the 8051.
Dec Hex Bin 14 E ORG ; FOURTEEN Interrupts In x86 PC.
Ass Prof Dr Masri Ayob TK 2633: Microprocessor & Interfacing Lecture 6: Control Instructions.
Stacks & Subroutines Content. Stacks A stack is a Last In First Out (LIFO) buffer containing a number of data items usually implemented as a block of.
Functions/Methods in Assembly
ECE 353 Introduction to Microprocessor Systems Michael G. Morrow, P.E. Week 5.
Picoblaze Overview EENG Introduction 8-bit microcontroller for Xilinx devices Soft Core – Soft Processor 5% of the resources of spartan 3 (3S200.
ECE 447 Fall 2009 Lecture 4: TI MSP430 Architecture and Instruction Set.
COMP3221: Microprocessors and Embedded Systems--Lecture 10 1 COMP3221: Microprocessors and Embedded Systems Lecture 10: Shift and Bit-set Instructions.
EE/CS-352: Embedded Microcontroller Systems Part V The 8051 Assembly Language Interrupts.
BRANCHES SUBROUTINES AND MEMORY USAGE AVR Assembler M. Neil - Microprocessor Course 1.
The AVR microcontroller
“ INSTRUCTIONS SET OF AVR MICROCONTROLLER ” SIGMA INSTITUTE OF ENGINEERING Prepared By: SR.NO NAME OF STUDENT ENROLLMENT 1 Abhishek Lakhara
Chapter 12 Processor Structure and Function. Central Processing Unit CPU architecture, Register organization, Instruction formats and addressing modes(Intel.
Deeper Assembly: Addressing, Conditions, Branching, and Loops
COMP2121: Microprocessors and Interfacing
COMP2121: Microprocessors and Interfacing
Introduction to Smart Systems
ECE 3430 – Intro to Microcomputer Systems
Format of Assembly language
Data Transfers, Addressing, and Arithmetic
Microprocessor and Assembly Language
Overview of Architecture Assembly Programming Concepts
Assembly Language Programming of 8085
Microprocessor T. Y. B. Sc..
The Stack.
ARM Registers Register – internal CPU hardware device that stores binary data; can be accessed much more rapidly than a location in RAM ARM has.
3.Instruction Set of 8085 Consists of 74 operation codes, e.g. MOV
COMP2121: Microprocessors and Interfacing
Lecture Set 5 The 8051 Instruction Set.
Subroutines and the Stack
Microprocessor and Assembly Language
Machine control instruction
Assembly Language Programming Part 2
COMP3221: Microprocessors and Embedded Systems
Chapter 10 The Stack.
CS 301 Fall 2002 Control Structures
COMP2121: Microprocessors and Interfacing
Branching Instructions
Subroutines and the Stack
Addressing Modes Register Direct, with 1 and 2 registers I/O Direct
COMP2121: Microprocessors and Interfacing
Tim Sumner, Imperial College, Rm: 1009, x47552
COMP3221: Microprocessors and Embedded Systems
Subroutines and the Stack
Some Assembly (Part 2) set.html.
CS501 Advanced Computer Architecture
Computer Organization and Assembly Language
COMP3221: Microprocessors and Embedded Systems
Presentation transcript:

COMP3221: Microprocessors and Embedded Systems Lecture 8: Program Control Instructions http://www.cse.unsw.edu.au/~cs3221 Lecturer: Hui Wu Session 2, 2004 COMP3221: Microprocessors and Embedded Systems--Lecture 8

COMP3221: Microprocessors and Embedded Systems--Lecture 8 Overview Program control instructions in AVR Stacks Sample AVR assembly programs using program control instructions COMP3221: Microprocessors and Embedded Systems--Lecture 8

COMP3221: Microprocessors and Embedded Systems--Lecture 8 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? COMP3221: Microprocessors and Embedded Systems--Lecture 8

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 the main textbook and AVR Instruction Set for a complete list. COMP3221: Microprocessors and Embedded Systems--Lecture 8

COMP3221: Microprocessors and Embedded Systems--Lecture 8 Jump Syntax: jmp k Operands: 0 ≤ k < 4M               Operation: PCk Flag affected: None Encoding: 1001 010k kkkk 110k kkkk kkkk kkkk kkkk Words: 2 Cycles: 3 Example: mov r1, r0             ; Copy r0 to r1 jmp farplc              ; Unconditional jump ... farplc: inc r20                    ; Jump destination COMP3221: Microprocessors and Embedded Systems--Lecture 8

COMP3221: Microprocessors and Embedded Systems--Lecture 8 Relative Jump Syntax: rjmp k Operands: -2K ≤ k < 2K               Operation: PCPC+k+1 Flag affected: None Encoding: 1100 kkkk kkkk kkkk Words: 1 Cycles: 2 Example: 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 COMP3221: Microprocessors and Embedded Systems--Lecture 8

COMP3221: Microprocessors and Embedded Systems--Lecture 8 Indirect Jump 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 Flag affected: None Encoding: 1001 0100 0000 1001 Words: 1 Cycles: 2 COMP3221: Microprocessors and Embedded Systems--Lecture 8

Indirect Jump (Cont.) Example: ldi r20, 2 ; Load jump table offset ldi r30, low(Lab<<1) ; High byte of the starting address (base) of jump table ldi r31, high(Lab<<1) ; Low byte of the starting address (base) of jump table add r30, r20 ; Base + offset is the address of the jump table entry lpm r0, Z+ ; Load low byte of the the jump table entry lpm r1, Z ; Load high byte of the jump table entry movw r31:r30, r1:r0 ; Set the pointer register Z to point the target instruction ijmp ; Jump to the target instruction … Lab: .dw jt_l0 ; The first entry of the jump table .dw jt_l1 ; The second entry of the jump table jt_l0: nop jt_l1: nop

COMP3221: Microprocessors and Embedded Systems--Lecture 8 Stacks A stack is an area of memory that supports two operations push – put something on the top of the stack 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 COMP3221: Microprocessors and Embedded Systems--Lecture 8

COMP3221: Microprocessors and Embedded Systems--Lecture 8 Stacks (Cont.) A stack will grow after push is executed. 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. COMP3221: Microprocessors and Embedded Systems--Lecture 8

COMP3221: Microprocessors and Embedded Systems--Lecture 8 AVR and Stacks 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 SPH (high byte) (IO register $3E) 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. COMP3221: Microprocessors and Embedded Systems--Lecture 8

AVR Stack Initialization .include "m64def.inc" .def temp=r20 .cseg ldi temp, low(RAMEND) out spl, temp ldi temp, high(RAMEND) out sph, temp 1 RAMEND–1 RAMEND SP COMP3221: Microprocessors and Embedded Systems--Lecture 8

COMP3221: Microprocessors and Embedded Systems--Lecture 8 AVR Stack Operations .include "m64def.inc" .def temp=r20 .cseg ldi temp, low(RAMEND) out spl, temp ldi temp, high(RAMEND) out sph, temp ldi r1, 0xff push r1 1 RAMEND–1 RAMEND 0xff SP COMP3221: Microprocessors and Embedded Systems--Lecture 8

AVR Stack Operations (Cont.) .include "m64def.inc" .def temp=r20 .cseg ldi temp, low(RAMEND) out spl, temp ldi temp, high(RAMEND) out sph, temp ldi r1, 0xff push r1 pop r2 ; r2=0xff 1 RAMEND–1 RAMEND 0xff SP COMP3221: Microprocessors and Embedded Systems--Lecture 8

Relative Call to Subroutine Syntax: rcall k Operands: -2K ≤ k < 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 Flag affected: None. Encoding: 1101 kkkk kkkk kkkk Words: 1 Cycles: 3 (Devices with 16-bit PC) 4 (Devices with 22-bit PC) COMP3221: Microprocessors and Embedded Systems--Lecture 8

Relative Call to Subroutine (Cont.) Example: 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 COMP3221: Microprocessors and Embedded Systems--Lecture 8

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 Flag affected: None. Encoding: 1001 0101 0000 1001 Words: 1 Cycles: 3 (Devices with 16-bit PC) 4 (Devices with 22-bit PC) COMP3221: Microprocessors and Embedded Systems--Lecture 8

Indirect Call to Subroutine (Cont.) Example: ldi r20, 2 ; Load call table offset ldi r30, low(Lab<<1) ; High byte of the starting address (base) of call table ldi r31, high(Lab<<1) ; Low byte of the starting address (base) of call table add r30, r20 ; Base + offset is the address of the call table entry lpm r0, Z+ ; Load low byte of the the call table entry lpm r1, Z ; Load high byte of the call table entry movw r31:r30, r1:r0 ; Set the pointer register Z to point the target function icall ; Call the target function … Lab: .dw ct_l0 ; The first entry of the call table .dw ct_l1 ; The second entry of the call table ct_l0: nop ct_l1: nop

Long Call to Subroutine Syntax: call k Operands: 0 ≤ k < 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 Flag affected: None. Encoding: 1001 010k kkkk 111k kkkk kkkk kkkk kkkk Words: 2 Cycles: 4 (Devices with 16-bit PC) 5 (Devices with 22-bit PC) COMP3221: Microprocessors and Embedded Systems--Lecture 8

Long Call to Subroutine (Cont.) Example: mov r16, r0          ; Copy r0 to r16 call check             ; Call subroutine nop                       ; Continue (do nothing) ... check: cpi r16, $42          ; Check if r16 has a special value breq error              ; Branch if equal … error: ldi r1, 1 … ; put the code for handling the error here ret                          ; Return from subroutine COMP3221: Microprocessors and Embedded Systems--Lecture 8

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 Flag affected: None Encoding: 1001 0101 0000 1000 Words: 1 Cycles: 4 (Devices with 16-bit PC) 5 (Devices with 22-bit PC) Example: routine:     push r14     ; Save r14 on the stack ... ; Put the code for the subroutine here. pop r14      ; Restore r14 ret           ; Return from subroutine COMP3221: Microprocessors and Embedded Systems--Lecture 8

COMP3221: Microprocessors and Embedded Systems--Lecture 8 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 Words: 1 Cycles: 4 (Devices with 16-bit PC) 5 (Devices with 22-bit PC) COMP3221: Microprocessors and Embedded Systems--Lecture 8

Return from Interrupt (Cont.) Example: ... extint:    push   r0       ; Save r0 on the stack pop     r0      ; Restore r0 reti             ; Return and enable interrupts Will cover details later COMP3221: Microprocessors and Embedded Systems--Lecture 8

COMP3221: Microprocessors and Embedded Systems--Lecture 8 Branch If Equal Syntax: breq k Operands:      -64 ≤ k < 63 Operation: If Rd = Rr (Z = 1) then PC  PC + k + 1, else PC  PC + 1 Flag affected: None Encoding: 1111 00kk kkkk k001 Words: 1 Cycles: 1 if condition is false 2 if conditional is true Example: cp r1, r0           ; Compare registers r1 and r0 breq   equal      ; Branch if registers equal ... equal: nop                  ; Branch destination (do nothing) COMP3221: Microprocessors and Embedded Systems--Lecture 8

Branch If Same or Higher (Unsigned) Syntax: brsh k Operands:      -64 ≤ k < 63 Operation: if rd  Rr (unsigned comparison) then PC  PC + k + 1, else PC  PC + 1 Flag affected: none Encoding: 1111 01kk kkkk k000 Words: 1 Cycles: 1 if condition is false 2 if conditional is true Example: sbi r26, $56             ; subtract $56 from r26 brsh test                ; branch if r26  $56 ... Test: nop                      ; branch destination … COMP3221: Microprocessors and Embedded Systems--Lecture 8

Branch If Lower (Unsigned) Syntax: brlo k Operands:      -64 ≤ k < 63 Operation: If Rd < Rr (unsigned comparison) then PC  PC + k + 1, else PC  PC + 1 Flag affected: None Encoding: 1111 00kk kkkk k000 Words: 1 Cycles: 1 if condition is false 2 if conditional is true Example: 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) COMP3221: Microprocessors and Embedded Systems--Lecture 8

Branch If Less Than (Signed) Syntax: brlt k Operands:      -64 ≤ k < 63 Operation: If Rd < Rr (signed comparison) then PC  PC + k + 1, else PC  PC + 1 Flag affected: None Encoding: 1111 00kk kkkk k100 Words: 1 Cycles: 1 if condition is false 2 if conditional is true Example: cp r16, r1               ; Compare r16 to r1 brlt less                  ; Branch if r16 < r1 (signed) ... less: nop                        ; Branch destination (do nothing) COMP3221: Microprocessors and Embedded Systems--Lecture 8

Branch If Greater or Equal (Signed) Syntax: brge k Operands:      -64 ≤ k < 63 Operation: If Rd  Rr (signed comparison) then PC  PC + k + 1, else PC  PC + 1 Flag affected: None Encoding: 1111 01kk kkkk k100 Words: 1 Cycles: 1 if condition is false 2 if conditional is true Example: cp     r11, r12        ; Compare registers r11 and r12 brge   greateq        ; Branch if r11 ≥ r12 (signed) ... greateq: nop                        ; Branch destination (do nothing)   COMP3221: Microprocessors and Embedded Systems--Lecture 8

COMP3221: Microprocessors and Embedded Systems--Lecture 8 Branch If Overflow Set Syntax: brvs k Operands:      -64 ≤ k < 63 Operation: If V=1 then PC  PC + k + 1, else PC  PC + 1 Flag affected: None Encoding: 1111 00kk kkkk k011 Words: 1 Cycles: 1 if condition is false 2 if conditional is true Example:   add r3, r4                   ; Add r4 to r3 brvs overfl                ; Branch if overflow ... overfl: nop                  ; Branch destination (do nothing) COMP3221: Microprocessors and Embedded Systems--Lecture 8

Branch If Overflow Clear Syntax: brvc k Operands:      -64 ≤ k < 63 Operation: If V=0 then PC  PC + k + 1, else PC  PC + 1 Flag affected: None Encoding: 1111 01kk kkkk k011 Words: 1 Cycles: 1 if condition is false 2 if conditional is true Example:   add r3, r4                   ; Add r4 to r3 brvs noover                 ; Branch if no overflow ... noover: nop                 ; Branch destination (do nothing) COMP3221: Microprocessors and Embedded Systems--Lecture 8

Branch if Global Interrupt is Enabled Syntax: brie k Operands:      -64 ≤ k < 63 Operation: If I=1 then PC  PC + k + 1, else PC  PC + 1 Flag affected: None Encoding: 1111 00kk kkkk k111 Words: 1 Cycles: 1 if condition is false 2 if conditional is true Example:   brvs inten                 ; Branch if the global interrupt is enabled ... inten: nop                 ; Branch destination (do nothing) COMP3221: Microprocessors and Embedded Systems--Lecture 8

Branch if Global Interrupt is Disabled Syntax: brid k Operands:      -64 ≤ k < 63 Operation: If I=0 then PC  PC + k + 1, else PC  PC + 1 Flag affected: None Encoding: 1111 00kk kkkk k111 Words: 1 Cycles: 1 if condition is false 2 if conditional is true Example:   brid intdis               ; Branch if the global interrupt is enabled ... intdis: nop                 ; Branch destination (do nothing) COMP3221: Microprocessors and Embedded Systems--Lecture 8