Assemblyy Language g g Programming g g Hsiao-Lung H i L Chan Ch Dept Electrical Engineering Chang Gung University, Taiwan chanhl@mail cgu edu tw [email protected] Elements of an assemblyy language g g statement Label Mnemonics Operands Comment 2 Label field Valid labels loop g _again c?gtm addwf addlw andlw 0x20,F,A 0x03 0x7F Invalid labels 30r5 clrf three-four cpfsgt isbig btfsc 0x16,A 0x14,A 0x15,0x07,B 3 Mnemonic field Either an assembly instruction mnemonic or an assembler directive (comment) Example p false loop: equ goto incf 0 ; equ is assembler directive start 0x20,W,A 4 Operand p field Operands for instructions or arguments for assembler directives true cpfseq equ movff 0x20,A 1 0x30,0x65 5 Assembler directives Most tell the assembler to do something other than creating the machine code Assembler Directives Control directives Data directives Listing directives Macro directives Object file directives 6 Control directives The directives for conditional assembly if <expr> else Endif Example if version movlw movwf else movlw movwf endif dif == 100 ; check current version 0x0a io_1,A 0x1a io_2,A 7 Control directives (cont.) ( ) The directive declares the beginning of a section of program code [<label>] code [<ROM address>] Example reset code goto 0x00 start The directive indicates the end of the program end 8 Control directives (cont.) ( ) The directive defines a text substitution string #define <name> [<string>] Example #define length 30 #define config 0x17,7,A #define sum3(x y z) sum3(x,y,z) (x + y + z) . . test dw sum3(1,length,200) ; place 1+20+200 here bsf config ; set bit 7 of register 0x17 to 1 9 Control directives (cont.) ( ) ifdef <label> Example #define test_val 2 . . ifdef test_val <execute test code> endif 10 Control directives (cont.) ( ) #undefine <label> ifndef <label> Example #define ; deletes a substitution string led_port 1 . . #undefine led_port . . ifndef led_port <execute code> endif 11 Control directives (cont.) ( ) The directive includes additional source file #include “<include_file>” ((or #include <include_file>) _ ) Search order if on fully qualified path is specified: current working directory, source file directory, MPASM executable bl directory di Example #include <p18F8680.inc> <p18F8680 inc> ; include p18F8680.inc from the installation directory of mplab. #include “lcd_util.asm” _ ; include lcd_util.asm file from current working directory 12 Control directives (cont.) ( ) The directive sets the default radix for data expression radix <default_radix> Valid radix values are: hex,, dec,, or oct Example radix dec 13 Control directives (cont.) ( ) while <expr> endw The lines between while and endw are assembled as long as <expr> is true Example p test_mac macro chk_cnt variable i i 0 i=0 while i<chk_cnt movlw i i+=1 endw endm start test_mac 6 end 14 Data directives: db, dw, dt Reserves program memory words db: define a data of one byte dw: define data of one word dt: define table Example led_pat msg1 array msg2 results db db db dw dw dt 0x30,0x80,0x6D,0x08 ‘b’, 0x22, ’\n’ “Please “ l enter your choice h (1/2):”,0 ( /2) ” 0x1234,0x2300,0x40,0x33 “The The humidity is “,0 0 1,2,3,4,5 ;generates a series of retlw instructions 15 Data directives: equ, q set, variable equ: define a assembly constant set: define an assembler variable variable: define symbols for assembler Example true false length width length equ q equ set set set variable variable 1 0 0x20 0x10 0x30 ; altered by other set directive i k=0, m, p=2 16 Macro directive Instructions are grouped together and assigned a name By entering the macro name, the same group of instructions can be duplicated p in anyy p place of the program User program is made more readable by using macros 17 Macro example p Define a macro ; WREG [arg1]+[arg2]+[arg3] sum of 3 sum_of_3 macro arg1, arg2, arg3 movf arg1,W,A addwf arg2 W A arg2,W,A addwf arg3,W,A endm Invoke the macro sum_of_3 f 3 0 0x20,0x21,0x22 20 0 21 0 22 18 Macro directive: exitm Force immediate return fro the macro Example test macro arg1 g if arg1 == 3 exitm else error “bad file assignment” endif endm 19 Macro directive: local Avoid conflict with another label declared outside the macro Example p len width abc len label l len equ equ macro local set res set endm 5 8 width len,width width len ; allocate 8-byte memory l 10 len-10 20 Listing g directives Control the MPASM listing file format list: turn on the listing file output if it had been previously turned off nolist: turn off the listing file output title: establish a text at the top of each page of the listing file title “Prime number generator, rev 2.0” error: issue an error message messg: create user defined message space: insert blank lines into the listing file space 3 page: insert a page eject into the listing file 21 Object j file directoryy Control the generation of object code processor <processor type> processor p18F8680 p p ; set processor p type yp to PIC18F8680 code: begin executable code section [[<label>]] org g <expr> p set subsequent code at the defined address reset start org 0x00 … goto start org 0x100 … 22 Object j file directory: y udata Declare the beginning of uninitialized data [<label>] udata [<RAM address>] var1 var2 udata res 1 res 2 ; starting address is assigned at the link time 23 Object j file directory: y banksel banksel <label> An instruction to the linker to generate movlb k to set active bank to the bank containing g <label> Example var1 vark udata res 1 … res 1 … code … banksel var1 … banksel vark 24 Assemblyy p program g template p org g goto org … retfie org 0x18 … retfie start … … end d 0x0000 ; p program g starting g address after p power on reset start 0x08 ; high-priority h h interrupt service routine ; return ; low-priority interrupt service routine ; return ; your program 25 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 #include <p18F8720.inc> ; can be other processor org 0x00 goto start org 0x08 retfie org 0x18 retfie start movf 0x20,W,A ; WREG [0x20] addwf 0x30,W,A ; WREG [0x20] + [0x30] addwf 0x40,W,A ; WREG [0x20] + [0x30] + [0x40] movwf 0x50,A ; 0x50 sum (in WREG) end d 26 Byte y order issue Big endian: stores the most significant byte at the lowest Big-endian: address and stores the least significant byte in the highest address. Little-endian: stores the most significant byte of the number at the highest g address and stores the least significant byte of the number in the lowest address. 27 Write a program to add two 24-bit numbers stored at 0x10~0x12 and 0x13~0x15 and leave the sum at 0x20..0x22 0x20 0x22 #include <p18F8720.inc> org 0x00 goto start org 0x08 retfie org 0x18 retfie start movf 0x10,W,A ; WREG [0x10] addwf 0x13,W,A ; WREG [0x13] + [0x10] movwf 0x20 A 0x20,A ; 0x20 [0x10] + [0x13] movf 0x11,W,A ; WREG [0x11] addwfc 0x14,W,A ; WREG [0x11] + [0x14] + C flag movwf 0x21,A ; 0x21 [WREG] movf 0x12,W,A ; WREG [0x12] addwfc 0x15,W,A ; WREG [0x12] + [0x15] + C flag movwf end 0x22,A ; 0x22 [WREG] 28 Write a program to subtract 5 from memory locations 0x10 to 0x13 start #include <p18F8720.inc> p org 0x00 goto start org 0x08 retfie org 0x18 retfie movlw 0x05 ; WREG 0x05 subwf 0x10,F,A ; 0x10 [0x10] – 0x05 subwf 0x11,F,A ; 0x11 [0x11] – 0x05 subwf b f 0x12,F,A 0 12 F A ; 0x12 0 12 [0x12] [0 12] – 0x05 0 05 subwf 0x13,F,A ; 0x13 [0x13] – 0x05 end 29 Write a program that subtracts the number stored at 0x20..0x23 from the number stored at 0x10..0x13 and leaves the difference at 0x30..0x33 start #include org goto org retfie org retfie movf subwf movwf movf subwfb b fb movwf movf subwfb movwf movf subwfb movwf end <p18F8720.inc> 0x00 start 0x08 0x18 0x20, 0x10, 0x30, 0x21, 0 11 0x11, 0x31, 0x22, 0x12 0x12, 0x32, 0x23, 0x13, 0x33, W, W, A W, W, W A W, W, W A W, W, A A A ; 0x30 [0x10] – [0x20] A A ; 0x31 [0x11] – [0x21] A A ; 0x32 [0x12] – [0x22] A A ; 0x33 [0x13] – [0x23] 30 Binaryy coded decimal ((BCD)) addition Decimal digits are encoded using 4 bits Let data register 0x24 and 0x25 holds BCD numbers, add these two BCD numbers and save the sum in 0x30 movf 0x24,W,A addwf 0x25,W,A daw movwf ; adjust j for valid BCD 0x30,A 31 BCD addition h31 h24 h36 h29 + h47 + h67 + h47 + h47 h78 h8B h7D h70 daw Add 0x6 to every sum digit greater than 9 Add 0x6 to every sum digit that produce a carry h31 h24 h36 h29 + h47 + h67 + h47 + h47 h78 h8B h7D h70 +h6 +h 6 +h6 h91 h83 h76 Digital carry (DC) in status register indicate there is a carry from bit 3 to b bitt 4 32 Write an instruction sequence that adds the decimal numbers stored at 0x10...0x13 and 0x14...0x17 and stores the sum in 0x20..0x23 start #include … movf addwf daw movwf movf addwfc daw movwf movf addwfc dd f daw movwf movf addwfc daw movwf end <p18F8720.inc> 0x10,W 0x14,W 0x20 0x11 0x15,W ; add the least significant byte ; adjust for valid BCD ; save in the destination ; add the second to least significant byte 0x21 0x12 0 16 0x16 ; add the second to most significant byte 0x22 0x13 0x17 ; add the most significant byte 0x23 33 Multiplication p 8-bit 8 bit multiplication instruction mulwf f mullw k Example movf mulwf movff movff 0x10,W,A 0x11,A PRODH,0x21 ; upper byte of the product PRODL,0x20 ; lower byte of the product 34 16-bit multiplication p P = PH P L Q = Q H Q L 35 16-bit multiplication: multiply two numbers at N1:N1+1 and M1:M1+1 n1_h n1_l m1_h m1_l M1 N1 PR #include <p18F8720.inc> p equ 0x37 ; first number equ 0x23 equ 0x66 ; second number equ 0x45 set 0x00 ; multiplicand set 0X02 ; multiplier set 0x06 ; product org 0X00 goto start org 0 08 0x08 retfie org 0x18 retfie 36 16-bit multiplication p (cont.) ( ) start movlw movwf movlw movwf movlw movwf movlw movwf m1_h M1+1,A m1_l M1,A n1_h N1+1 A N1+1,A n1_l N1,A ; set up p M1+1:M1 movf mulwf l f movff movff M1+1,W,A ; compute M1H N1H N1 1 A N1+1,A PRODL,PR+2 PRODH PR+3 PRODH,PR+3 ; set up N1+1:N1 37 16-bit multiplication p (cont.) ( ) movf mulwf movff movff M1,W,A ; compute M1L N1L N1,A PRODL,PR PRODH PR+1 PRODH,PR+1 movf mulwf movf addwf movf addwfc movlw addwfc M1,W,A N1+1,A PRODL,W,A PR+1,F,A , , PRODH,W,A PR+2,F,A 0 PR+3,F,A ; compute M1L N1H ; add M1L N1H to PR ; add carry 38 16-bit multiplication p (cont.) ( ) movf mulwf movf addwf movf addwfc movlw addwfc nop p end M1+1,W,A ; compute M1H N1L N1,A PRODL,W,A ; add M1H N1L to PR PR+1 F A PR+1,F,A PRODH,W,A PR+2,F,A 0 PR+3,F,A ; add carry 39 PIC18 memoryy access Inside the c chipp up to 2 MB of program memory 4096 registers Program P Memory Space (a portion of this space is on the c chip) 21-bit progam address Program counter (PC) 12-bit register address PIC18 CPU 16-bit instruction bus 8-bit data bus Data Memory Space (Special function registers and general purpose RAM)) Fi Figure 1.3 1 3 The Th PIC18 memory spaces 40 Instructions for changing program counter: goto vs. vs bra Jump p to absolute address goto k 32-bit instruction: 1110 1111 kkkk kkkk 1111 kkkk kkkk kkkk jump to address represented by k Jump to relative address BRA n ; n is branch offset 16-bit instruction: 1110 0nnn nnnn nnnn jump to the instruction with address equals to PC+2+n forward branch: bra 10 backward branch: bra -10 Use label to replace branch offset loop … … bra loop 41 Conditional branch vs. unconditional branch Unconditional branch BRA n Conditional branch BCC n: jump to the instruction with address equals to PC+2+n if the condition code CC is true CC can be anyy one of the following: g C: N: NN: NOV: NZ: OV: Z: C flag is set to 1 N flag is set to 1 which indicates that the previous operation result was negative N flag is 0 which indicates non-negative condition V flag is 0 which indicates there is no overflow condition Z flag is 0 which indicates the previous operation result was not zero V flag is 1 which indicates the previous operation caused an overflow Z flag is 1 which indicates the previous operation result was zero 42 Alternative conditional branch cpfseq p q cpfsgt cpfslt decfsz dcfsnz incfsz infsnz tstfsz btfsc btfss f,a , f,a f,a f,d,a f,d,a fda f,d,a f,d,a f,a f,b,a f,b,a ; ; ; ; ; ; ; ; ; ; compare p register g f with WREG,, skip p if equal q compare register f with WREG, skip if equal compare register f with WREG, skip if less than decrement f, skip if 0 decrement f, skip if not 0 increment ff, skip if 0 increment f, skip if not 0 test f, skip if 0 test bit b of register f, skip if clear test bit b of register f, skip if set 43 Example p n equ q lp_cnt set … movlw movwf loop … … decfsz goto 20 0x10 n lp_cnt ; n has the value of 20 ; assign file register 0x10 to lp_cnt ; prepare to repeat the loop for n times ; program loop lp_cnt,F,A ; decrement lp_cnt and skip if equal to 0 loop ; executed if lp_cnt 0 decf bnz lp_cnt,F,A ; decrement lp_cnt loop ; executed if lp_cnt 0 44 Write a program to compute 1 + 2 + 3 + … + n and save the sum at 0x00 and 0x01 sum=0; for (i = 1; i<=n; i++) sum += i;; 45 compute p 1 + 2 + 3 + … + n (cont.) ( ) #include <p18F8720.inc> n equ D'50' sum_hi set 0x01 ; high byte of sum sum lo set sum_lo 0x00 ; low byte of sum i set 0x02 ; loop index i org 0x00 ; reset vector goto start org 0x08 retfie org 0x18 retfie 46 compute p 1 + 2 + 3 + … + n (cont.) ( ) start clrf clrf clrf incf sum_lp movlw cpfsgt bra bra p movf add_lp addwf movlw addwfc incf bra exit_sum nop end sum_hi,A sum_lo,A i,A iFA i,F,A n i,A add_lp exit_sum i,W,A , , sum_lo,F,A 0 sum hi F A sum_hi,F,A i,F,A sum_lp ; initialize sum to 0 ; ; ; ; ; ; ; ; initialize i to 0 i starts from 1 place n in WREG compare i with n and skip if i > n perform addition when i 50 it is done when i > 50 place p i in WREG add i to sum_lo ; add carry to sum_hi sum hi ; increment loop index i by 1 47 Find the largest element stored in the array that is stored in data memory locations from 0x10 to 0x5F arr_max=arr[0]; [0] for (i=1; i<n; i++) if (arr[i]>arr_max) arr_max=arr[i]; 48 Find the largest g element (cont.) ( ) arr_max i n start equ q equ equ #include org goto org retfie org retfie movff ff lfsr clrf 0x00 0x01 D'80' ; the array count <p18F8720.inc> 0x00 start 0x08 0x18 0x10,arr_max 0 10 ; set arr[0] [0] as the h initial i i i l max FSR0,0x11 ; place address of arr[1] in FSR0 iA i,A ; initialize loop count i to 0 49 Find the largest g element (cont.) ( ) again g movlw cpfslt bra movf cpfsgt bra bra replace movwf next_i incf goto done nop end n-1 i,A done POSTINC0,W arr_max,A replace next_i arr_max,A i,F,A again ; skip if i < n-1 ; all comparisons have been done ; is arr_max > arr[i]? ; no ; yes ; update the array max 50 Reading g and writing g data in program p g memoryy Table write (TBLWT) Table read ( (TBLRD) ) 51 Write an instruction sequence to read a byte from program memory at 0x60 into TABLAT clrf clrf movlw movwf tblrd tblrd* TBLPTRU,A TBLPTRH,A 0x60 TBLPTRL,A 52 Table read and write instructions Table 2.11 PIC18 MCU table read and write instructions Mnemonic, operator TBLRD* TBLRD*+ TBLRD* TBLRD*TBLRD+* TBLWT* TBLWT*+ TBLWT + TBLWT*TBLWT+* Description Table read Table read with post-increment Table read with post-decrement post decrement Table read with pre-increment Table write Table write with post post-increment increment Table write with post-decrement Table write with pre-increment 16-bit instruction word 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 1000 1001 1010 1011 1100 1101 1110 1111 Status affected none none none none none none none none 53 Reading g the program p g memoryy location p prog_loc g ; Step 1. Place the address of prog_loc prog loc in TBLPTR registers movlw upper prog_loc movwf TBLPTRU,A movlw high prog_loc movwf TBLPTRH A TBLPTRH,A movlw low prog_loc movwf TBLPTRL A TBLPTRL,A ; Step 2. Perform a TBLRD instruction. tblrd tblrd* 54 Logic g instructions 55 Examples p To set bits 7, 6, and 0 of PORTA to 1 movlw B’11000001’ , , iorwf PORTA,F,A To clear bits 4, 2, and 1 of PORTB to 0 movlw B’11101001 andwf PORTB,F,A To toggle bits odd bits of PORTC movlw B’10101010’ xorwf PORTC 56 Write a program to find out the number of elements in an array of 8-bit elements that are a multiple of 8. The array is in the program memory 57 Number of elements of a multiple p of 8 (cont.) ( ) #include <p18F8720.inc> p ilimit equ 0x20 ; loop index limit count set 0x00 ii set 0x01 ; loop index mask equ 0x07 ; used to masked upper five bits org 0x00 goto start … ; interrupt service routines start clrf count,A movlw ilimit movwff ii ; initialize i i i li ii to ilimit ili i movlw upper array movwf TBLPTRU,A TBLPTRU A movlw high array movwf TBLPTRH,A 58 Number of elements of a multiple p of 8 (cont.) ( ) movlw low arrayy movwf TBLPTRL,A movlw mask i_loop tblrd*+ ; read an array element into TABLAT andwf TABLAT,F,A bnz next ; branch if not a multiple of 8 incf count,F,A ; is a multiple of 8 next decfsz ii,F,A ; decrement loop count bra i_loop nop array db 0 00 0 01 0 30 0 03 0 04 0 05 0 06 0 07 0 08 0 09 0x00,0x01,0x30,0x03,0x04,0x05,0x06,0x07,0x08,0x09 db 0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x10,0x11,0x12,0x13 db 0x14 0x15 0x16 0x17 0x18 0x19 0x1A 0x1B 0x1C 0x1D 0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1C,0x1D db 0x1E,0x1F end 59 Using g program p g loops p to create time delays y A Macro to repeat an instruction for certain number of times dup_nop macro kk ; duplicate the nop instruction kk times variable i i=0 while i < kk nop ; takes 1 instruction cycle time i += 1 endw endm d 60 Write a program to create a time delay of 100 ms for the demo board that uses a 40 MHz crystal oscillator to operate lp_cnt1 p lp_cnt2 loop1 loop2 equ q 0x21 One instruction cycle = 100 ns equ 0x22 movlw D’200’ movwf lp_cnt1,A movlw D’250’ Produce a delay of 0.5 ms movwf lp_cnt2,A lp cnt2 A dup_nop 17 ; 17 instruction cycles decfsz lp_cnt2,F,A ; 1 instruction cycle (2 when [lp_cnt1]=0) bra loop2 ; 2 instruction cycles decfsz lp_cnt1,F,A b bra l loop1 1 61 Rotate instructions 7 6 5 4 3 2 1 0 C Figure 2.17 Operation performed by the rlcf f,d,a instruction C 7 6 5 4 3 2 1 0 Figure 2.19 Operation performed by the rrcf f,d,a instruction 7 6 5 4 3 2 1 0 Figure 2.18 Operation performed by the rlncf f,d,a instruction 7 6 5 4 3 2 1 0 Figure 2.20 Operation performed by the rrncf f,d,a instruction 62 Examples The result is 0 1 1 0 0 1 1 0 0 1 1 0 0 0 0 1 1 Original value New value [0x10] = 1010 1001 [0x10] = 01010010 C=0 C=1 0 Figure 2.21 Operation of the RLCF 0x10,F,A instruction The result is 1 1 1 1 0 1 0 0 0 0 1 0 1 1 1 1 1 1 Original value New value [0x10] = 1100 0111 [0x10] = 1110 0011 C=1 C=1 Figure 2.22 Operation of the RRCF 0x10,F,A instruction 63 Examples p Th result The lt is i 0 1 1 0 1 1 1 0 original value new value [0x10] = 0110 1110 [0x10] = 0011 0111 0 0 1 1 0 1 1 1 Figure 2.23 2 23 Operation performed by the rrncf 0x10, 0x10 F, F A instruction Th result The lt is i 0 1 1 1 1 0 0 1 1 1 1 1 1 0 0 Before After [0x10] = 0110 1110 [0x10] = 1101 1100 0 Figure 2.24 2 24 Operation performed by the rlncf 0x10, 0x10 F, F A instruction 64 Bit operation p instructions bcf bsf btg f, b, a f, b, a f, b, a Examples bcf STATUS,C,A bsf sign,0,A sign 0 A btg sign,0,A ; clear bit b of register f ; set bit b of register f ; toggle bit b of register f ; clear the C flag of the STATUS register ; set the bit 0 of register sign to 1 ; toggle bit 0 of register sign ; (0 to 1 or 1 to 0) 65 Perform multiplication p byy shift left operations p Multiply the 3 3-byte byte number store at 0x00…0x02 by 8 p loop movlw bcf rlcf rlcf rlcf decfsz goto 0x03 STATUS,, C,, A 0x00, F, A 0x01, F, A 0x02, F, A WREG,W,A loop ; set loop count to 3 ; clear the C flag g ; shift left one place ; shift 3 places? 66 Perform division byy shifting g to the right g Divide the 3 3-byte byte number stored at 0x10…0x12 by 16 p loop movlw bcf rrcf rrcf rrcf decfsz goto 0x04 STATUS,, C,, 0x12, F, A 0x11, F, A 0x10, F, A WREG,W,A loop ; set loop count to 4 A ; shift right 1 place ; shifted right 4 places? 67 Reference Han Way Huang, PIC Microcontroller: An Introduction to Han-Way Software and Hardware Interfacing, Thomson Delmar Learning, 2005. 68
© Copyright 2026 Paperzz