Class Note for ECE 372 at UA
Popular in Course
Popular in Department
This 27 page Class Notes was uploaded by an elite notetaker on Friday February 6, 2015. The Class Notes belongs to a course at University of Arizona taught by a professor in Fall. Since its upload, it has received 20 views.
Reviews for Class Note for ECE 372 at UA
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 02/06/15
Chapter 2 PC18 Assembly Language Programming Original slides by HanWay Huang Modified by Susan Lysecky 21 Objectives 0 Explain the structure of an assembly language program 0 Use assembler directives to allocate memory blocks and de ne constants 0 Write assembly programs to perform simple arithmetic operations 0 Write program loops to perform repetitive operations 0 Use a owchart to describe program ow 0 Create time delays of any length using program loops 23 Assembly Language Program Structure Components of an Assembly Program 0 Assembler directives 0 Assembly language instructions 0 Comments Elements of an Assembly Language Statement 0 Label 0 Mnemonics o Operands 0 Comment 23 Assembly Language Program Structure The Label Fields Must start from column 1 and followed by a tab a space a colon or the end ofa line Must start with an alphabetic character or underscore May contain alphanumeric characters underscores and question marks 7 May contain up to 32 characters and is casesensitive by default wait btfss sum7 wait is a label again decf loopcntF again is a label 23 Assembly Language Program Structure The Mnemonic Field 0 Can be an assembly instruction mnemonic or assembly directive 0 Must begin in column two or greater 0 Must be separated from the label by a colon one or more spaces or tabs addlw 0x10 addlwis the mnemonic field loop incf 0x30WA incf is a mnemonic false equ 0 equ is the mnemonic eld 23 Assembly Language Program Structure The Operand Field o The operand s follows the instruction mnemonic 0 Provides the operands for an instruction or arguments for an assembler directive 0 Must be separated from the mnemonic eld by one or more spaces or tabs 0 Multiple operands are separated by commas movff 0x300x400 0x300x400quot is the operand field decf loopcntF label loopcnt is the operand true equ 1 1 is the argument for equ 23 Assembly Language Program Structure The Comment Field o Optional 0 A comment starts with a semicolon o All characters to the right of the semicolon are ignored bythe assembler 0 Comments provide documentation to the instruction or assembler directives o A comment may explain the function ofa single statement or the function of a group of instructions toohigh decf meanFA prepare to search in the lower half label mnemonic operand comment eld 24 Assembler Directives 0 Look like instructions in the assembly language program o Most directives tell assembler to do something other than create machine code o Provide programmer with a way to tell assembler how to process the assembly code 0 MPASM directives 0 Control directives permit sections of conditionally assembled code 0 Data directives control alloca ion of memory and refer to data items symbolically 0 Listing directives control MPASM listing le format 0 Macro directives control execution and data allocation within macro body de nitions 0 Object le directives used when creating an object le 24 Assembler Directives Control Directives o Directives for conditional assembly if version 100 WWW D lO mova lol A else mole D 26 mova lOZ A endif end end of program if ltexprgt else endif 24 Assembler Directives Control Directives o Declares the beginning of a section of program code 0 lfno label is speci ed the section is named codequot o The starting address ofthe section is either included in the directive or assigned at link time ifnot speci ed in the directive o De nes a text substitution 5 Wherever name is encountered in the code string is substituted 0 Delete a substitution string ltlabegt code ltROM addressgt code 0x00 goto start reset ll de nes a text substitution string de ne ltnamegt ltstn39nggt 30 Xyz 103 de ne loopcnt de ne sum3xyz de ne seed ll deletes a substitution string undefine ltlabelgt 24 Assembler Directives Control Directives 0 Includes additional source le include ltincludefilegtquot include ltincludefilegt ii iciude iodiutii asm ii39iciude the led utiasm file from current directory ii iciude ltpi8F8680 ii icgt ii39iciude the ie p18F8680inc from the installation directory of mplab 0 Sets the default radix for data expression radix ltdefautradixgt hex dec or oct radix dec Set default radix to decimal 24 Assembler Directives Control Directives o The lines between while and endw are assembled While ltexprgt as long as ltexprgt is true endw testimac macro chkicnt variable i i o whileilt chk cnt moviw i i i endw endm start testimac 6 end 24 Assembler Directives Data Directives 0 Data Directives db ltexprgtltexprgt de ne 1 or multiple byte values db textstringquot de ne a string dw ltexprgtltexprgt de ne 1 or multiple word constants dw textstringquot de ne a string at ltexprgt ltexprgt generates a series of retlw instructions ltlabelgt set ltexprgt assign a value ltexprgt to label ltlabelgt equ ltexprgt de nes a constant 24 Assembler Directives Data Directives 0 Data Directive Examples edpat db 0X300X800X6D9X400x790x200x330x100X5B0X08 msg1 db Please enter your choice 12quot0 array dw 0x12340x23000x400x33 msg2 dw The humidity is 0 results dt 12345 sumhi set 0x01 sumlo set 0x00 TH equ 200 TL equ 30 24 Assembler Directives Macro Directives o What is a macro o A group of instructions that are grouped ltabelgt macro ltagr1gt ltarggtl together and assigned a name One or multiple arguments can be input to a endm macro By entering he macro name the same group of instructions can be duplicated in any place ofthe program o User program is made more readable by using macros User becomes more productive by saving the text entering time macro endm exitm expand local noexpand 15 24 Assembler Directives Macro Directives 0 Macro definition and call example Macro De nition Example eeritual macro macro name is eeritual movlw 0x55 instruction 1 movwf EECONZ instruction 2 movlw DXAA instruction 3 movwf EECONZ instruction 4 endm Macro Call Example eeritual this macro call causes the assembler to insert instruction 1 instruction 4 24 Assembler Directives Macro Directives o More macro examples sumof3 macro arg1 argZ arga WREG arg1ar92ar93 movf arg1wA addwf ar92wA addwf ar93wA endm sumof3 0x01 0x02 0x03 WREG 0x01 0x02 0x03 24 Assembler Directives Object File Directives o generate the instruction sequence to set active bankseI ltabegt data bank tothe one where ltIabelgt is located o ltIabelgt must have been de ned before the banksel directive is invoked bigq set 0x300 banksel bigq this directive will cause the assembler to insert the instruction movlb 0X03 24 Assembler Directives Object File Directives sets the program origin for subsequent code Habeb at the address de ned in ltexprgt ltabegt will be assigned the value of ltexprgt org ltexprgt reset org 0x00 goto start start led pat org 0x1000 led pathas the value ofOxlOOO db 0x750xao0x6D0x790x330x530x5l0x700x7l0x73 24 Assembler Directives Object File Directives sets the processor type processor ltprocessortypegt processor p18F8680 set processortype to learasao 2n 10 25 Representing the Program Logic 0 Program Development Procedure 0 Problem de nition Flowchart Symbols 0 Algorithm development using pseudo code or owchart Converting algorithm into assembly instruction sequence lnput or uutput a Testing program using normal data marginal data and erroneous data 0 Algorithm provides overall plan for solving problem as well as documentation to the so ware to be developed lt onepage connector Subruutine 0 Many representations uffrpage connector 21 26 A Template for Writing Assembly Programs org exoooo program starting address after power on reset goto start org 0x08 highpriority interrupt service routine org 0x18 lowpriority interrupt service routine start your program end 22 26 A Template for Writing Assembly Programs 0 Program template for interrupts have been covered 0 r9 g oto 0 r9 ret e 0 r9 ret e start end OXOOOO start 0X08 0X18 program starting address after power on reset highpriority interrupt service routine lowpriority interrupt service routine your program 23 27 Case Issue 0 The PC18 instructions can be written in either uppercase or lowercase o MPASM allows the user to include p18Fxxxxincquot le to provide register de nitions for the speci c processor 0 All special function registers and bits are de ned in uppercase o The convention followed in this text is using lowercase for instructions and directives using uppercase for special function registers 24 I2 Byte Order Issue This issue concerns how bytes are stored for multibyte numbers 0 The bigendian method stores the most significant byte at the lowest address and stores the least signi cant byte in the highest address 0 The littleendian method stores the most signi cant byte ofthe number at the highest address and stores the least signi cant byte of the number in the lowest address The 32bit number 0x12345678 will stored as follows with two methods BigEndian Method LittleEndian Method address P P1 P2 P3 P P1 P2 P3 value 12 34 56 78 78 56 34 12 in hex 25 28 Writing Programs to Perform Arithmetic Computations Example 24 Example 24 Write a program hat adds the three numbers stored in data registers at 0x20 0x30 and 0x40 and places the sum in data register at 0x50 A399 quot quotquot include ltplaFa7zoincgt can be otherprocessor Step 1 org 0x00 Load the number stored at 0x20 into gm start the WREG register org 0x08 Step 2 retrle Add the numberstored at 0x30 and org me the numberm the WREG registerand leave the sum in the WREG register rem start rriovr 0xzowA WREG 0x20 Step 3 Add the number stored at 0x40 and add OXSO W A WREG OXZOHOXSO the numberm the WREG registerand addwf 0x4owA WREGlt 0x200x300x40 leave the sum W the WREG register m VWf 0X5 iA 0X50 lt Sum quot1 WREG end Step 4 store the contents ofthe WREG register in the memory location at 0x50 26 13 28 Writing Programs to Perform Arithmetic Computations Example 25 Example 25 Write a program to add two 24bit numbers stored at 0x100x12 and 0x130x15 and leave the sum at 0x200x22 include ltp18FB720incgt org 0x00 goto start org 0x08 ret e org 0x18 ret e start movf 0x10wA WREG 0x10 addwf 0x13wA WREG 0x13 0x10 movwf 0xzoA 0x20 0x100x13 movf 0x11wA WREG 0x11 addwfc 0x14wA WREG 0x11 0x14 5 flag movwf 0x21A 0x21 WREG movf 0x12wA WREG 0x12 addwfc 0x15wA WREG 0x12 0x15 5 flag movwf 0x22A 0x22 WREG end 27 28 Writing Programs to Perform Arithmetic Computations Example 26 Example 26 Write a program to subtract 5 from memory locations 0x10 to 0x13 Algorithm Step 1 Place 5 W the WREG reglslel39 im39m e lt913F3720iquot gt org 0x00 Step 2 Subtract WREG from the memory 9 t start locatloh 0x10 and leaye the oltrerehce m the org 0x08 memory locatloh 0x10 ret e org 0x18 ret e Step 3 SubtractWREG from the memory locatloh um and leaye the oltrerehce m the start movlw 0x05 WREG 0xos memorylocatlorl ow subwf 0x10lA 0X10 0X100X05 subwf 0x11lA 0x11 0x11 0x05 subwf 0x12lA 0x12 oml 0x05 Step 4 SubtractWREG fromthe memory subwf 0x13lA 0X13 0X13 0X05 locatloh 0x12 and leaye the oltrerehce m the end memory locatloh 0x12 Step 5 SublrachREG from the memory locallol l OMB and leave the dlffel el lce W the memorylocatlorl 0x13 28 28 Writing Programs to Perform Arithmetic Computations Example 26 Example 27 Write a program that subtracts the number stored at 0x200x23 from the number stored at 0x100x13 and leaves the difference at 0x300x33 WREG EIXZEI EIgtlt3E lt umeWREG WREG lt Mi EIgtlt31 lt Elxi 1 r WREGL a WREG 4 Mi EIXSZ lt uxwz r WREGL a WREG lt um Egtlt33 lt Elxi a r WREG r a Threeruperand subtractmn Threeruperand subtractmn Threeruperand subtractmn 29 28 Writing Programs to Perform Arithmetic Computations Example 26 include ltp18FB720incgt org 0x00 goto start retfie org 0x18 MED UX1U7WREG start movf 0x20 w A 0x30 1 0x10 0x20 subwf cm W A H movwf 0x30 A H movf 0x21wA 0X31 0X11 0X21 W mm39MREGI39E subwfb 0x11 w A H movf cm W A 0x32 4 0x12 0x22 subwrb 0x12wA ux32Dlt12rVVREGE TWEBDperand movwf 0x32 A H sumracnun movf 0x23 w A 0x33 4 0x13 0x23 subwfb cm W A H movwf cm A H end WREG lt Ma ma 4 may WREGLE Threeruperand subtraction an 28 Writing Programs to Perform Arithmetic Computations o Binary Coded Decimal BCD Addition 0 Decimal digits are encoded using 4 bits 0 Two decimal digits are packed into a byte in memory 0 Alter each addition one needs to use the daw instruction to adjust and correct the result II Let data registerOXZA and 0X25 holds BCD numbers the following instruction II sequence adds these two BCD numbers and saves the sum in 0X30 movf 0x24 w A addwf 0x25 w A daw movwf 0x30 A 31 28 Writing Programs to Perform Arithmetic Computations Example 29 Example 29 Write an instruction sequence that adds the decimal numbers stored at 0x100x13 and 0x140x17 and stores the sum in 0x200x23 itinclude ltp18F8720inc start niovr oxiow add the least signi cant byte addwr 0x14w H daw adjust for valid and inon 0x20 save in the destination niovr 0x11 add the second to least signi cant byte addwrc 0x15w H daw H inon 0x21 H niovr 0x12 add the second to most signi cant byte addwrc 0x16 H daw H inon 0x22 H niovr 0x13 add the most signi cant byte addwrc 0x17 H daw H inon 0x23 H end 32 28 Writing Programs to Perform Arithmetic Computations Multiplication o PC18 has two instructions for 8bit movf oxlowA multiplication mulwf oxllA mulwff and mullw k movff PRODH0X21 upper byte ofthe product movff PRoDL0x20 lower byte of the product a The products are stored ln the PRODHPRODL register pair 0 The multiplication ofnumbers larger than 8 bits must be synthesized c To perform multiplication operation on numbers longerthan 8 bits the operand must be broken down into 8bit chunks 0 Multiple 8bit multiplications are performed and the resultant partial products are aligned properly and added together 0 Two 16bit numbers P and Q can be broken down into as follows P PHPL QQHQL as 28 Writing Programs to Perform Arithmetic Computations Multiplication movwf NA 39 0 Instruction sequence to multiply two numbers that movf M1wA are stored at NN1 and M M1 mulwf N1A compute MN x N movff PRODLPR2 movff PRODHPR3 quotlon MwA compute ML x NL mulwf NA upper parllal pruduct 39quot quotfr PRDDL39PR E pLQL movff PRODHPR1 movf MwA upper luwer mulwf N1A compute ML x NH i Inon PRODLWA add ML x NH to PR addwf PR1FA 39 I parllal gpuult mow PRODHVWYA r L quot addwrc PR2FA quot parllal plpuult quot V39W quot mop addwfc PR3FA add carry quot1on M1wA mulwf NA compute MHxNL ADD Final llmduct Po movf PRODLWA add MH x NL to PR addwf PR1FA H quotlon PRODHWA H addwfc PR2FA H l rblt by l rblt multlpllcatlol l 39quot quotquot addwfc PR3FA add carry nop end 34 29 Program Loops 0 Enable the microcontroller to perform repetitive operations 0 A loop may be executed a nite number of times or in nite number of times 1 Do S forever 2 fori n1 to n2 Do S or fori n2 downto n1 do S 35 29 Program Loops 3 while C do S 4 repeat S until C initiaiiZE 0 ME raise as I8 29 Program Loops Changing the Program Counter MmrucuntruHer Executes ms tructmn sequermaHy m nurma ebnm mm P CWEhasa21rbwtpvugvamcuumevPCwhmhws dmded mbtmee vegwslevs PCL PCH and PCU PCLcanbeaccesseddwec y PCH and PCU are nut enemy accessbxe One ean aeeesseu tne va ues uf F39CH and PCU muremy by aeeessmg tne F39CLATH and F39CLATU ReadmgthePCmecausetheva uesuiPCHand PCU tb be named mtb tbe PCLATH and PCLATU Wmmgtbe PCL Wm causetbe va uesuiPCLATCH and PCLATU tb bewvmen mtb tbe PCH and PCU n nurma prugram Executmn tne F39C va ue s ncremented by Ewther 2 m4 Tbrmptementabrbgrambbbtbebmcessbrneeustb cbangetbe PC va ue by a va ue btbenban 2 by a 37 29 Program Loops Changing the Program Counter nstrucbons forchangmg the program counter Desunatmn uf a branen ur gum ms trumun s nurrmHy spem ed by a abe Mrwmcmizs 5mm mourn Dnscnmlan 1mm lnsxmcuon mm menu ae 19
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'