Presentation is loading. Please wait.

Presentation is loading. Please wait.

1. University of Energy and Natural Resources, Sunyani Institute of Distance Learning Dept. of Comp. & Elect. Eng. CENG 365 – Microprocessor DR. NANA.

Similar presentations


Presentation on theme: "1. University of Energy and Natural Resources, Sunyani Institute of Distance Learning Dept. of Comp. & Elect. Eng. CENG 365 – Microprocessor DR. NANA."— Presentation transcript:

1 1

2 University of Energy and Natural Resources, Sunyani Institute of Distance Learning Dept. of Comp. & Elect. Eng. CENG 365 – Microprocessor DR. NANA DIAWUO (PHD) TWENEBOAH-KODUAH S. 2

3 Chapter 3 – logic gates and assembly language 3

4 Multilevel machines Level 0: The digital logic level Operations at the digital logic level are performed by circuits called logic gates. Each gate has one or more digital inputs (signals representing 0 or 1) and computes as output some simple function of these inputs, such as AND, OR, Exclusive OR, etc. The digital logic level is concerned with electrical current and lack of current. Each instruction that the computer executes sets the lines that control the processor, either by allowing power to run through a line, or by preventing power from running through a line. Each instruction has a different configuration for which lines get power and which ones don't. Level 1: Microprogram Level The Intel chips have all used a microprogram to interpret machine language instructions into hardware instructions. In this case, the microprogram contains actual hardware instructions, and each machine language instruction is executed by calling a subroutine in the microprogram. Level 2: The machine language This is an abstract representation of which lines get power and which ones don't. It is a simple abstraction: a one represents power, and a zero represents no power. We can write a machine instruction, such as 00000011001010, and the processor will set and clear the appropriate lines to perform the indicated operation. 4 Or Hardware level

5 Multilevel machines Level 3: The operating system This is just a collection of machine language subroutines. Subroutine is a collection of computer codes that performs a certain in a program. Level 4: Assembly Language Level This level has a one-to-one correspondence with the machine language. Instead of using ones and zeros to indicate an instruction, symbolic names are used. So the machine language instruction 00000011001010 can be written as ADD CX,DX. The change from assembly language to machine language is done by translation 5

6 Multilevel machines Level 4: Assembly Language Level This level has a one-to-one correspondence with the machine language. Instead of using ones and zeros to indicate an instruction, symbolic names are used. So the machine language instruction 00000011001010 can be written as ADD CX,DX. The change from assembly language to machine language is done by translation Level 5: High Level Language This would be a language like C++, where one statement would be translated into several machine language instructions. For instance if (A > B) C = 1; would translate into these instructions (represented in assembler instead of machine) mov ax,A cmp ax,B jle next mov C,1 next: 6

7 Changing from one level to another Two things can happen when an instruction from one level is changed to a lower level 1.There is a change from a more readable format to a less readable format. For example, changing assembly to machine. 2.One instruction is changed into several instructions at the new level. For example, changing C++ to machine. Imagine how many hardware instructions are executed for one instruction in an application program. There are two ways major steps to go through when changing from a higher level to a lower level. a.Translation b.Interpretation Translation Each high instruction is translated into the low level and the new low instructions are saved. After all the high instructions have been changed to the low level, then all the new low instructions are executed. Interpretation Each high instruction is translated and executed in the low level before continuing with the next high instruction. The new low level instructions are not saved. 7 What is a virtual machine?

8 PIC18 Assembly language Structure A program written in assembly language consists of a sequence of statements that tell the computer to perform the desired operations Consist of three directives ◦Assembler directives ◦Assembly language instruction ◦And comments Source code fields ◦Label ◦Mnemonic ◦Operand (s) and Comment 8

9 Code structure 9 Could be: colon (:), space, tab, or the end of line

10 Order Instruction Labels must start in column 1. Mnemonics may start in column 2 or beyond. Operands follow the mnemonic. Comments may follow the operands, mnemonics, or labels and can start in any column. The maximum column width is 256 characters. Spaces or a colons are used to separate the label and the mnemonic and use space(s) to separate the mnemonic and the operand(s). Multiple operands must be separated by commas. 10

11 Label Fields A label must start in column 1. It may be followed by a colon (:), space, tab, or the end of line. Labels must begin with an alphabetic character or an underscore (_) May contain alphanumeric characters, the underscore, and the question mark. Example The following instructions contain valid labels: (a)loop addwf 0x20,F,A (b)_again addlw 0x03 (c)c?gtm andlw 0x7F (d)may2June bsf 0x07, 0x05,A 11

12 Label Fields Example The following instructions contain valid labels: (a)loop addwf 0x20,F,A (b)_again addlw0x03 (c)c?gtm andlw0x7F (d)may2June bsf 0x07, 0x05,A The following instructions contain invalid labels: (e)isbig btfsc 0x15,0x07,B ;label starts at column 2 (f)3or5 clrf 0x16,A ;label starts with a digit (g)three-four cpfs 0xl4,A ;label contains illegal character 12

13 Mnemonic Field This field can be either an assembly instruction mnemonic or an assembler directive and must begin in column 2 or greater. If there is a label on the same line, instructions must be separated from that label by a colon or by one or more spaces or tabs. Examples of mnemonic field: (a)false equ 0 ;equ is an assembler directive (b)goto start ;goto is the mnemonic (c)loop: incf 0x20,W,A ;incf is the mnemonic 13

14 Operand Field If an operand field is present, it follows the mnemonic field. The operand field may contain operands for instructions or arguments for assembler directives. Operands must be separated from mnemonics by one or more spaces or tabs. Multiple operands are separated by commas. The following examples include operand fields: (a)cpfseq 0x20,A ; “0x20” is the operand (b)true equ 1 ; “1” is the operand (c)movff 0x30, 0x65 ; “0x30” and “0x65” are operands 14

15 Comment Field The comment field is optional and is added for documentation purpose. It starts with a semicolon “ ; “. All characters following the semicolon are ignored through the end of the line. The two types of comments are illustrated in the following examples. (a)decf 0x20,F,A ; decrement the loop count (b); the whole line is comment 15

16 Assembler directives Assembler directives look just like instructions in an assembly language program. Most assembler directives tell the assembler to do something other than creating the machine code for an instruction. Assembler directives provide the assembly language programmer with a means to instruct the assembler how to process subsequent assembly language instructions. Directives also provide a way to define program constants and reserve space for dynamic variables. 16

17 Types of assembler directives Control directives These set flow control of instruction in the code i.e. decision makers Example: if else endif The if directive begins a conditionally assembled code block. If evaluate to true, the code immediately following if will assemble. Otherwise, subsequent code is skipped until else directive or an endif directive is encountered. endif marks the end of condition block 17

18 Example if version == 100 ;check current version movlw 0x0a movwf io_1, A else movlw 0x1a movwf io_2, A endif 18

19 Control directives 19

20 Macro directives There are situations in which the same sequence of instructions need to be included in several places. This sequence of instructions may operate on different parameters. These instruction can be put in macro and call at anytime with writing additional codes Syntax macro [, …, ] endm 20

21 Other macro directives 21 Object file and Listing directives may also be important

22 Arithmetic operation using PIC18 Addition Write a program that adds the three numbers stored in data registers at 0x20, 0x30, and 0x40 and places the sum in data register at 0x50. Solution: The algorithm for adding three numbers is as follows: Step 1: Load the number stored at 0x20 into the WREG register. Step 2: Add the number stored at 0x30 and the number in the WREG register and leave the sum in the WREG register. Step 3: Add the number stored at 0x40 and the number in the WREG register and leave the sum in the WREG register. Step 4: Store the contents of the WREG register in the memory location at 0x50. The program that implements this algorithm is as follows: 22

23 Arithmetic operation using PIC18 Addition The program that implements this algorithm is as follows: 23

24 Arithmetic operation using PIC18 Subtraction Write a program to subtract 5 from memory locations 0x10 to 0x13. Solution: The algorithm for this problem is as follows: Step 1: Place 5 in the WREG register. Step 2: Subtract WREG from the memory location 0x10 and leave the difference in the memory location 0x10. Step 3: Subtract WREG from the memory location 0x11 and leave the difference in the memory location 0x11. Step 4: Subtract WREG from the memory location 0x12 and leave the difference in the memory location 0x12. Step 5: Subtract WREG from the memory location 0x13 and leave the difference in the memory location 0x13. The assembly program that implements this algorithm is as follows: 24

25 Arithmetic operation using PIC18 Subtraction The assembly program that implements this algorithm is as follows: 25

26 Arithmetic operation using PIC18 Subtraction – Example Write a program that substract the number stored at 0x20 …. 0x23 from the number stored at 0x10 …. 0x13 and leaves the difference at 0x30 … 0x33 Solution The program implementation is: 26

27 Arithmetic operation using PIC18 Subtraction – Example Write a program that substracte the number stored at 0x20 …. 0x23 from the number stored at 0x10 …. 0x13 and leaves the difference at 0x30 … 0x33 Solution The program implementation is: 27

28 Arithmetic operation using PIC18 28

29 Arithmetic operation using PIC18 Multiplication – Example Write an instruction sequence to multiply two 8-bit numbers stored in data memory locations 0x10 and 0x11, respectively, and place the product in data memory location 0x20 and 0x21 29

30 PIC18 Loop and logic instructions The PIC MCU provide group of instruction that perform logical operations such as AND, OR, exclusive-OR and complementing 8-bit number. Loops can be use to create a time delay by repeating instructions 30 Logical operations are useful for looking for array elements with certain properties (e.g., divisible by power of 2) and manipulating I/O pin values (e.g., set certain pins to high, clear a few pins, toggle a few signals, and so on).

31 PIC18 Loop and logic instructions Example: Write an instruction sequence to perform the following task ◦Set bits 7, 6, and 0 of the PORTA register to high ◦Clear bits 4, 2, and 1 of the PORTB register to low ◦Toggle bits 7, 5,3, and 1 of the PORTC register 31

32 PIC18xxx Basic instructions description MnemonicDescriptionExample ADDLWAdd Literal and W ADDLW 0x15 ADDWFAdd W and fADDWF FSR, 0 ANDLWAnd Literal with WANDLW 0x5F ANDWFAnd W with fANDWF FSR, 1 BCFBit Clear fBCF FLAG_REG, 7 BSFBit Set fBSF FLAG_REG, 7 BTFSCBit Test, Skip if ClearBTFSC FLAG, 4 BTFSSBit Test f, Skip if SetBTFSS FLAG, 4 CALLCall Subroutine CLRFClear fCLRF FLAG_REG CLRWClear WCLRW COMFComplement fCOMF REG1, 0 DECFDecrement fDECF INDF, 1 DECFSZDecrement f, Skip if 0 32


Download ppt "1. University of Energy and Natural Resources, Sunyani Institute of Distance Learning Dept. of Comp. & Elect. Eng. CENG 365 – Microprocessor DR. NANA."

Similar presentations


Ads by Google