COMP3221: Microprocessors and Embedded Systems

Slides:



Advertisements
Similar presentations
CPU Structure and Function
Advertisements

Registers of the 8086/ /2002 JNM.
AT94 Training 2001Slide 1 FPSLIC- Embedded MCU Core 8 Bit RISC MCU Industry’s Highest 8-bit Performance A Real 8-Bit RISC Architecture Low Power ( idle/power.
Microprocessors.
Parul Polytechnic Institute Subject Code : Name Of Subject : Microprocessor and assembly language programming Name of Unit : Introduction to Microprossor.
Presented By Dr. Shazzad Hosain Asst. Prof. EECS, NSU
SOFTWARE ARCHITECTURE OF THE 8088 AND 8086 MICROPROCESSORS
ARITHMETIC, LOGIC INSTRUCTIONS, AND PROGRAMS
Parul Polytechnic Institute Subject Code : Name Of Subject : Microprocessor and assembly language programming Name of Unit : Introduction to Microprossor.
COMP3221: Microprocessors and Embedded Systems Final Exam Lecturer: Hui Wu Session 1, 2005.
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.
TK 2633 Microprocessor & Interfacing
COMP3221: Microprocessors and Embedded Systems--Lecture 8 1 COMP3221: Microprocessors and Embedded Systems Lecture 8: Program Control Instructions
1 Lecture 2: Review of Computer Organization Operating System Spring 2007.
COMP3221: Microprocessors and Embedded Systems Lecture 2: Instruction Set Architecture (ISA) Lecturer: Hui Wu Session.
COMP3221 lec06-numbers-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 6: Number Systems - II
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
Room: E-3-31 Phone: Dr Masri Ayob TK 2633 Microprocessor & Interfacing Lecture 1: Introduction to 8085 Assembly Language.
COMP3221: Microprocessors and Embedded Systems--Lecture 4 1 COMP3221: Microprocessors and Embedded Systems Lecture 4: Number Systems (II)
ARM programmer’s model and assembler Embedded Systems Programming.
COMP3221 lec07-numbers-III.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 7: Number Systems - III
COMP3221: Microprocessors and Embedded Systems--Lecture 9 1 COMP3221: Microprocessors and Embedded Systems Lecture 9: Data Transfer Instructions
Gursharan Singh Tatla Block Diagram of Intel 8086 Gursharan Singh Tatla 19-Apr-17.
Rabel Talpur:12BME#025.  40-pin chip  Developed by Motorola in 1975  16 address lines and 8 data lines  Used only +5V.
Basic Architecture Lecture 15. In general, if the number of bits is n, then the number of different combinations of 0s and 1s that can be made is 2 n.
Lecture 2: Basic Instructions CS 2011 Fall 2014, Dr. Rozier.
Stack Stack Pointer A stack is a means of storing data that works on a ‘Last in first out’ (LIFO) basis. It reverses the order that data arrives and is.
Week 2.  Understand what the processor is and what it does.  Execute basic LMC programs.  Understand how CPU characteristics affect performance.
Introduction to AVR Chapter 1
George W. Woodruff School of Mechanical Engineering, Georgia Tech ME4447/6405 ME 4447/6405 Microprocessor Control of Manufacturing Systems and Introduction.
Computer Architecture Lecture 03 Fasih ur Rehman.
COMPUTER ORGANIZATION AND ASSEMBLY LANGUAGE Lecture 21 & 22 Processor Organization Register Organization Course Instructor: Engr. Aisha Danish.
Lecture 1: Review of Computer Organization
October 1, 2003Serguei A. Mokhov, 1 SOEN228, Winter 2003 Revision 1.2 Date: October 25, 2003.
Computer Architecture Lecture 4 by Engineer A. Lecturer Aymen Hasan AlAwady 17/11/2013 University of Kufa - Informatics Center for Research and Rehabilitation.
Execution Architecture MTT CPU08 Core M CPU08 INTRODUCTION.
Introduction to Microprocessors Chapter 3. Programming Model (8086)  Shows the various internal registers that are accessible to the programmer.
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.
ECE 447: Lecture 11 Introduction to Programming in Assembly Language.
An Adder A Subtractor. A and B are the inputs of the adder/ subtractor R is the output of the adder/ subtractor F is the control to tell it to add or.
MICROPROCESSOR DETAILS 1 Updated April 2011 ©Paul R. Godin prgodin gmail.com.
Computer Organization Instructions Language of The Computer (MIPS) 2.
1 Basic Processor Architecture. 2 Building Blocks of Processor Systems CPU.
1 Computer Architecture. 2 Basic Elements Processor Main Memory –volatile –referred to as real memory or primary memory I/O modules –secondary memory.
8085 INTERNAL ARCHITECTURE.  Upon completing this topic, you should be able to: State all the register available in the 8085 microprocessor and explain.
AVR Architecture Prepared By: Avdhesh Soni ( ) Sarthak Patel ( ) Akshay Parekh ( ) Fenil Sachla ( ) Guided.
Vadodara Institute of Engineering Subject: Microcontroller interfacing Prepared by: Bipin Malani ( ) Vishwa Pandit ( ) Branch : Electronics.
“ INSTRUCTIONS SET OF AVR MICROCONTROLLER ” SIGMA INSTITUTE OF ENGINEERING Prepared By: SR.NO NAME OF STUDENT ENROLLMENT 1 Abhishek Lakhara
COMP2121: Microprocessors and Interfacing
Overview of Architecture Assembly Programming Concepts
Gunjeet Kaur Dronacharya Group of institutions
MCI PPT AVR MICROCONTROLLER Mayuri Patel EC-1 5th sem
CS-401 Assembly Language Programming
COMP3221: Microprocessors and Embedded Systems
Introduction to Assembly Chapter 2
Addressing Modes Register Direct, with 1 and 2 registers I/O Direct
And conditional branching
COMP3221: Microprocessors and Embedded Systems
University of Gujrat Department of Computer Science
Introduction to Assembly Chapter 2
MIPS Assembly.
Introduction to Microprocessor Programming
COMP3221: Microprocessors and Embedded Systems
Computer Operation 6/22/2019.
ME 4447/6405 Microprocessor Control of Manufacturing Systems and
ME 4447/6405 Microprocessor Control of Manufacturing Systems and
Presentation transcript:

COMP3221: Microprocessors and Embedded Systems Lecture 7: Arithmetic and logic Instructions http://www.cse.unsw.edu.au/~cs3221 Lecturer: Hui Wu Session 2, 2005 COMP3221: Microprocessors and Embedded Systems--Lecture 7

COMP3221: Microprocessors and Embedded Systems--Lecture 7 Overview Arithmetic and Logic Instructions in AVR Sample AVR Assembly Programs Using AL instructions COMP3221: Microprocessors and Embedded Systems--Lecture 7

AVR Instruction Overview Load/store architecture At most two operands in each instruction Most instructions are two bytes long Some instructions are 4 bytes long Four Categories: Arithmetic and logic instructions Program control instruction Data transfer instruction Bit and bit test instructions COMP3221: Microprocessors and Embedded Systems--Lecture 7

General-Purpose Registers in AVR named r0, r1, …, r31 in AVR assembly language Broken into two parts: with 16 registers each, r0 to r15 and r16 to r31. Each register is also assigned a memory address in SRAM space. Register r0 and r26 through r31 have additional functions. r0 is used in the instruction LPM (load program memory) Registers x (r27 : r26), y (r29 : r28) and z (r31 : r30) are used as pointer registers Most instructions that operate on the registers have direct, single cycle access to all general registers. Some instructions such as sbci, subi, cpi, andi, ori and ldi operates only on a subset of registers. COMP3221: Microprocessors and Embedded Systems--Lecture 7

General-Purpose Registers in AVR (Cont.) Address 0x00 r0 0x01 r1 0x1A r26 x register low byte 0x1B r27 x register high byte 0x1C r28 y register low byte 0x1D r29 y register high byte 0x1E r30 z register low byte 0x1F r31 z register high byte COMP3221: Microprocessors and Embedded Systems--Lecture 7

The Status Register in AVR The Status Register (SREG) contains information about the result of the most recently executed arithmetic instruction. This information can be used for altering program flow in order to perform conditional operations. SREG is updated after all ALU operations. SREG is not automatically stored when entering an interrupt routine and restored when returning from an interrupt. This must be handled by software. COMP3221: Microprocessors and Embedded Systems--Lecture 7

The Status Register in AVR (Cont.) Z C Bit 7 6 5 4 3 2 1 0 Bit 7 – I: Global Interrupt Enable Used to enable and disable interrupts. 1: enabled. 0: disabled. The I-bit is cleared by hardware after an interrupt has occurred, and is set by the RETI instruction to enable subsequent interrupts. COMP3221: Microprocessors and Embedded Systems--Lecture 7

The Status Register in AVR (Cont.) Z C Bit 7 6 5 4 3 2 1 0 Bit 6 – T: Bit Copy Storage The Bit Copy instructions BLD (Bit LoaD) and BST (Bit STore) use the T-bit as source or destination for the operated bit. A bit from a register in the Register File can be copied into T by the BST instruction, and a bit in T can be copied into a bit in a register in the Register File by the BLD instruction. COMP3221: Microprocessors and Embedded Systems--Lecture 7

The Status Register in AVR (Cont.) Z C Bit 7 6 5 4 3 2 1 0 Bit 5 – H: Half Carry Flag The Half Carry Flag H indicates a Half Carry (carry from bit 4) in some arithmetic operations. Half Carry is useful in BCD arithmetic. COMP3221: Microprocessors and Embedded Systems--Lecture 7

The Status Register in AVR (Cont.) Z C Bit 7 6 5 4 3 2 1 0 Bit 4 – S: Sign Bit Exclusive OR between the Negative Flag N and the Two’s Complement Overflow Flag V ( S = N V). Bit 3 – V: Two’s Complement Overflow Flag The Two’s Complement Overflow Flag V supports two’s complement arithmetic. COMP3221: Microprocessors and Embedded Systems--Lecture 7

The Status Register in AVR (Cont.) Z C Bit 7 6 5 4 3 2 1 0 Bit 2 – N: Negative Flag N is the most significant bit of the result. • Bit 1 – Z: Zero Flag Z indicates a zero result in an arithmetic or logic operation. 1: zero. 0: Non-zero. COMP3221: Microprocessors and Embedded Systems--Lecture 7

The Status Register in AVR (Cont.) Z C Bit 7 6 5 4 3 2 1 0 Bit 0 – C: Carry Flag Its meaning depends on the operation. For addition X+Y, it is the carry from the most significant bit. In other words, C= Rd7 • Rr7 +Rr7 • NOT(R7) + NOT(R7) • Rd7, where Rd7 is bit 7 of x, Rr7 is bit 7 of y, R7 is bit 7 of x+y, • is the logical AND and + is the logical OR. For subtraction x-y, where x and y are unsigned integer, it indicates if x<y. If x<y, the C=1; otherwise, C=0. In other words, C = NOT(Rd7) • Rr7+ Rr7 • R7 +R7 • NOT(Rd7).

Selected Arithmetic and Logic Instructions add, adc, inc sub, sbc, dec mul, muls, mulsu and, or, eor clr, cbr, cp, cpc, cpi, tst com, neg Refer to the main textbook (Pages 63~67) and AVR Instruction Set for the complete list of AL instructions. COMP3221: Microprocessors and Embedded Systems--Lecture 7

COMP3221: Microprocessors and Embedded Systems--Lecture 7 Add without Carry Syntax: add Rd, Rr Operands: Rd, Rr {r0, r1, …, r31} Operation: RdRd + Rr Flags affected: H, S, V, N, Z, C Encoding: 0000 11rd dddd rrrr Words: 1 Cycles: 1 Example: add r1, r2 ; Add r2 to r1 add r28, r28 ; Add r28 to itself COMP3221: Microprocessors and Embedded Systems--Lecture 7

COMP3221: Microprocessors and Embedded Systems--Lecture 7 Add with Carry Syntax: adc Rd, Rr Operands: Rd, Rr {r0, r1, …, r31} Operation: RdRd + Rr + C Flags affected: H, S, V, N, Z, C Encoding: 0001 11rd dddd rrrr Words: 1 Cycles: 1 Example: Add r1 : r0 to r3 : r2 add r2, r0 ; Add low byte adc r3, r1 ; Add high byte Comments: adc is used in multi-byte addition. COMP3221: Microprocessors and Embedded Systems--Lecture 7

COMP3221: Microprocessors and Embedded Systems--Lecture 7 Increment Syntax: inc Rd Operands: Rd {r0, r1, …, r31} Operation: RdRd+1 Flags affected: S, V, N, C Encoding: 1001 010d dddd 1011 Words: 1 Cycles: 1 Example: clr r22 ; clear r22 loop: inc r22 ; Increment r22 cpi r22, $4F ; compare r22 to $4F brne loop ; Branch to loop if not equal nop ; Continue (do nothing COMP3221: Microprocessors and Embedded Systems--Lecture 7

Subtract without Carry Syntax: sub Rd, Rr Operands: Rd, Rr {r0, r1, …, r31} Operation: RdRd–Rr Flags affected: H, S, V, N, Z, C Encoding: 0001 10rd dddd rrrr Words: 1 Cycles: 1 Example: sub r13, r12 ; Subtract r12 from r13 COMP3221: Microprocessors and Embedded Systems--Lecture 7

COMP3221: Microprocessors and Embedded Systems--Lecture 7 Subtract with Carry Syntax: sbc Rd, Rr Operands: Rd, Rr {r0, r1, …, r31} Operation: RdRd–Rr–C Flags affected: H, S, V, N, Z, C Encoding: 0000 10rd dddd rrrr Words: 1 Cycles: 1 Example: Subtract r1:r0 from r3:r2 sub r2, r0 ; Subtract low byte sbc r3, r1 ; Subtract with carry high byte Comments: sbc is used in multi-byte subtraction COMP3221: Microprocessors and Embedded Systems--Lecture 7

COMP3221: Microprocessors and Embedded Systems--Lecture 7 Decrement Syntax: dec Rd Operands: Rd {r0, r1, …, r31} Operation: RdRd–1 Flags affected: S, V, N, Z Encoding: 1001 010d dddd 1010 Words: 1 Cycles: 1 Example: ldi r17, $10 ; Load constant in r17 loop: add r1, r2 ; Add r2 to r1 dec r17 ; Decrement r17 brne loop; ; Branch to loop if r170 nop ; Continue (do nothing) COMP3221: Microprocessors and Embedded Systems--Lecture 7

COMP3221: Microprocessors and Embedded Systems--Lecture 7 Multiply Unsigned Syntax: mul Rd, Rr Operands: Rd, Rr {r0, r1, …, r31} Operation: r1, r0Rr*Rd (unsignedunsigned * unsigned ) Flags affected: Z, C Encoding: 1001 11rd dddd rrrr Words: 1 Cycles: 2 Example: mul r6, r5 ; Multiply r6 and r5 mov r6, r1 mov r5, r0 ; Copy result back in r6 : r5 COMP3221: Microprocessors and Embedded Systems--Lecture 7

COMP3221: Microprocessors and Embedded Systems--Lecture 7 Multiply Signed Syntax: muls Rd, Rr Operands: Rd, Rr {r16, r17, …, r31} Operation: r1, r0Rr*Rd (signedsigned * signed ) Flags affected: Z, C Encoding: 0000 0010 dddd rrrr Words: 1 Cycles: 2 Example: mul r17, r16 ; Multiply r17 and r16 movw r17:r16, r1:r0 ; Copy result back to r17 : r16 COMP3221: Microprocessors and Embedded Systems--Lecture 7

Multiply Signed with Unsigned Syntax: mulsu Rd, Rr Operands: Rd, Rr {r16, r17, …, r23} Operation: r1, r0Rr*Rd (signedsigned * unsigned ) Flags affected: Z, C C is set if bit 15 of the result is set; cleared otherwise. Encoding: 0000 0011 0ddd 0rrr Words: 1 Cycles: 2 COMP3221: Microprocessors and Embedded Systems--Lecture 7

Multiply Signed with Unsigned (Cont.) Example: Signed multiply of two 16-bit numbers stored in r23:r22 and r21:r20 with 32-bit result stored in r19:r18:r17:r16 How to do? Let ah and al be the high byte and low byte, respectively, of the multiplicand and bh and bb the high byte and low byte, respectively, of the multiplier. ah : al * bh : bl = (ah* 28+ al) * (bh* 28+bl) = ah*bh*216 + al*bh* 28 + ah*bl*28 + al*bl COMP3221: Microprocessors and Embedded Systems--Lecture 7

Multiply Signed with Unsigned (Cont.) Example: Signed multiply of two 16-bit numbers stored in r23:r22 and r21:r20 with 32-bit result stored in r19:r18:r17:r16 muls16x16_32: clr r2 muls r23, r21                  ; (signed) ah * (signed) bh movw r19 : r18, r1 : r0 mul r22, r20 ; (unsigned) al * (unsigned) bl movw r17 : r16, r1: r0 mulsu r23, r20                  ; (signed) ah * (unsigned) bl sbc r19, r2 ; Trick here (Hint: what does the carry mean here?) add r17, r0 adc r18, r1 adc r19, r2 mulsu r21, r22                  ; (signed) bh * (unsigned) al sbc r19, r2 ; Trick here add r17, r0 adc r18, r1 adc r19, r2 ret COMP3221: Microprocessors and Embedded Systems--Lecture 7

Lower-Case to Upper-Case .include "m64def.inc" .equ size =5 .def counter =r17 .dseg .org 0x100 ; Set the starting address of data segment to 0x100 Cap_string: .byte 5 .cseg Low_string: .db "hello" ldi zl, low(Low_string<<1) ; Get the low byte of the address of "h" ldi zh, high(Low_string<<1) ; Get the high byte of the address of "h" ldi yh, high(Cap_string) ldi yl, low(Cap_string) clr counter ; counter=0

COMP3221: Microprocessors and Embedded Systems--Lecture 7 Lower-Case to Upper-Case (Cont.) main: lpm r20, z+ ; Load a letter from flash memory subi r20, 32 ; Convert it to the capital letter st y+,r20 ; Store the capital letter in SRAM inc counter cpi counter, size brlt main loop: nop rjmp loop COMP3221: Microprocessors and Embedded Systems--Lecture 7

COMP3221: Microprocessors and Embedded Systems--Lecture 7 Bitwise AND Syntax: and Rd, Rr Operands: Rd, Rr {r0, r1, …, r31} Operation: RdRr · Rd (Bitwise AND Rr and Rd) Flags affected: S, V, N, Z Encoding: 0010 00rd dddd rrrr Words: 1 Cycles: 1 Example: ldi r2, 0b00110101           ldi r16, 1           and r2, r16        ; r2=0b00000001 COMP3221: Microprocessors and Embedded Systems--Lecture 7

COMP3221: Microprocessors and Embedded Systems--Lecture 7 Bitwise OR Syntax: or Rd, Rr Operands: Rd, Rr {r0, r1, …, r31} Operation: RdRr v Rd (Bitwise OR Rr and Rd) Flags affected: S, V, N, Z Encoding: 0010 10rd dddd rrrr Words: 1 Cycles: 1 Example: ldi r15, 0b11110000 ldi r16, 0b00001111 or r15, r16      ; Do bitwise or between registers ; r15=0b11111111 COMP3221: Microprocessors and Embedded Systems--Lecture 7

COMP3221: Microprocessors and Embedded Systems--Lecture 7 Bitwise Exclusive-OR Syntax: eor Rd, Rr Operands: Rd, Rr {r0, r1, …, r31} Operation: RdRr  Rd (Bitwise exclusive OR Rr and Rd) Flags affected: S, V, N, Z Encoding: 0010 01rd dddd rrrr Words: 1 Cycles: 1 Example: eor r4, r4               ; Clear r4 eor r0, r22              ; Bitwise exclusive or between r0 and r22 ; If r0=0b101011 and r22=0b01001000 ; then r0=0b11100011 COMP3221: Microprocessors and Embedded Systems--Lecture 7

COMP3221: Microprocessors and Embedded Systems--Lecture 7 Clear Bits in Register Syntax: cbr Rd, k Operands: Rd {r16, r17, …, r31} and 0  k  255 Operation: RdRd · ($FF-k) (Clear the bits specified by k ) Flags affected: S, V, N, Z Encoding: 0111 wwww dddd wwww (wwwwwwww=$FF-k) Words: 1 Cycles: 1 Example: cbr r4, 11               ; Clear bits 0 and 1 of r4. COMP3221: Microprocessors and Embedded Systems--Lecture 7

COMP3221: Microprocessors and Embedded Systems--Lecture 7 Compare Syntax: cp Rd, Rr Operands: Rd {r0, r1, …, r31} Operation: Rd–Rr (Rd is not changed) Flags affected: H, S, V, N, Z, C Encoding: 0001 01rd dddd rrrr Words: 1 Cycles: 1 Example: cp r4, r5                 ; Compare r4 with r5 brne noteq                ; Branch if r4  r5 ... noteq: nop                       ; Branch destination (do nothing)  COMP3221: Microprocessors and Embedded Systems--Lecture 7

COMP3221: Microprocessors and Embedded Systems--Lecture 7 Compare with Carry Syntax: cpc Rd, Rr Operands: Rd {r0, r1, …, r31} Operation: Rd–Rr–C (Rd is not changed) Flags affected: H, S, V, N, Z, C Encoding: 0001 01rd dddd rrrr Words: 1 Cycles: 1 Example: ; Compare r3:r2 with r1:r0 cp r2, r0              ; Compare low byte cpc r3, r1            ; Compare high byte brne noteq          ; Branch if not equal ... noteq: nop                     ; Branch destination (do nothing) COMP3221: Microprocessors and Embedded Systems--Lecture 7

Compare with Immediate Syntax: cpi Rd, k Operands: Rd {r16, r17, …, r31} and 0 k  255 Operation: Rd – k (Rd is not changed) Flags affected: H, S, V, N, Z, C Encoding: 0011 kkkk dddd kkkk Words: 1 Cycles: 1 Example: cp r19, 30                 ; Compare r19 with 30 brne noteq                ; Branch if r19  30 ... noteq: nop                       ; Branch destination (do nothing)  COMP3221: Microprocessors and Embedded Systems--Lecture 7

COMP3221: Microprocessors and Embedded Systems--Lecture 7 Test for Zero or Minus Syntax: tst Rd Operands: Rd {r0, r1, …, r31} Operation: RdRd · Rd Flags affected: S, V, N, Z Encoding: 0010 00dd dddd dddd Words: 1 Cycles: 1 Example: tst r0                    ; Test r0 breq zero             ; Branch if r0=0 ... zero:   nop                      ; Branch destination (do nothing) COMP3221: Microprocessors and Embedded Systems--Lecture 7

COMP3221: Microprocessors and Embedded Systems--Lecture 7 One's Complement Syntax: com Rd Operands: Rd {r0, r1, …, r31} Operation: Rd$FF – Rd Flags affected: S, V, N, Z Encoding: 1001 010d dddd 0000 Words: 1 Cycles: 1 Example: com r4                 ; Take one's complement of r4 breq zero             ; Branch if zero ... zero:  nop                      ; Branch destination (do nothing) COMP3221: Microprocessors and Embedded Systems--Lecture 7

Two's Complement Syntax: neg Rd Operands: Rd {r0, r1, …, r31} Operation: Rd$00 – Rd (The value of $80 is left unchanged) Flags affected: H, S, V, N, Z, C H: R3 + Rd3 Set if there is a borrow from bit 3; cleared otherwise Encoding: 1001 010d dddd 0001 Words: 1 Cycles: 1 Example: sub r11,r0     ;Subtract r0 from r11 brpl positive ;Branch if result positive neg r11        ;Take two's complement of r11 positive: nop            ;Branch destination (do nothing)

COMP3221: Microprocessors and Embedded Systems--Lecture 7 Reading Material AVR Instruction Set. COMP3221: Microprocessors and Embedded Systems--Lecture 7