LSN 3 Assembly Language Programming
Transcript of LSN 3 Assembly Language Programming
![Page 1: LSN 3 Assembly Language Programming](https://reader033.fdocuments.in/reader033/viewer/2022060317/62949149d574725d28033cfc/html5/thumbnails/1.jpg)
Department of
Engineering Technology
LSN 3
Assembly Language Programming
ECT358 – Microprocessors II
![Page 2: LSN 3 Assembly Language Programming](https://reader033.fdocuments.in/reader033/viewer/2022060317/62949149d574725d28033cfc/html5/thumbnails/2.jpg)
ECT 358 Microprocessors II [email protected]
LSN 3 – Program Structure
• Assembler directives
• Assembly language instructions
• Comments
![Page 3: LSN 3 Assembly Language Programming](https://reader033.fdocuments.in/reader033/viewer/2022060317/62949149d574725d28033cfc/html5/thumbnails/3.jpg)
ECT 358 Microprocessors II [email protected]
LSN 3 – Program Structure
• Elements of an Assembly Language Statement
– Label
• Start with alphabetic character or underscore
– May contain any alphanumeric character, underscores, and question
marks
– Mnemonics
• Can be an assembly instruction mnemonic or assembly directive
– Operands
– Comment
![Page 4: LSN 3 Assembly Language Programming](https://reader033.fdocuments.in/reader033/viewer/2022060317/62949149d574725d28033cfc/html5/thumbnails/4.jpg)
ECT 358 Microprocessors II [email protected]
LSN 3 – Assembler Directives
• MPASM directives
– Control directives – permit sections of conditionally
assembled code
– Data directives – control allocation of memory and refer to
data items symbolically
– Listing directives – control MPASM listing file format
– Macro directives – control execution and data allocation
within macro body definitions
– Object file directives – used when creating an object file
![Page 5: LSN 3 Assembly Language Programming](https://reader033.fdocuments.in/reader033/viewer/2022060317/62949149d574725d28033cfc/html5/thumbnails/5.jpg)
ECT 358 Microprocessors II [email protected]
LSN 3 – Assembler Directives
• Control directives
if version == 100
movlw D’10’
movwf io1,A
else
movlw D’26’
movwf io2,A
endif
end ; end of program
radix dec ; set default radix to decimal
![Page 6: LSN 3 Assembly Language Programming](https://reader033.fdocuments.in/reader033/viewer/2022060317/62949149d574725d28033cfc/html5/thumbnails/6.jpg)
ECT 358 Microprocessors II [email protected]
LSN 3 – Assembler Directives
• Control directives (cont.)
reset code 0x00
goto start
#define loop_cnt 30
#define sum3(x,y,z) (x + y + z)
#define seed 103
#include <p18F4520.inc> ;include the file
p18F8680.inc from
the installation
directory of mplab
![Page 7: LSN 3 Assembly Language Programming](https://reader033.fdocuments.in/reader033/viewer/2022060317/62949149d574725d28033cfc/html5/thumbnails/7.jpg)
ECT 358 Microprocessors II [email protected]
LSN 3 – Assembler Directives
• Data directives led_pat db 0x30,0x80,0x6D,9x40,0x79,0x20
msg1 db “Please enter your choice (1/2):”,0
array dw 0x1234,0x2300,0x40,0x33
msg2 dw “The humidity is “,0
TH equ 200
variable
![Page 8: LSN 3 Assembly Language Programming](https://reader033.fdocuments.in/reader033/viewer/2022060317/62949149d574725d28033cfc/html5/thumbnails/8.jpg)
ECT 358 Microprocessors II [email protected]
LSN 3 – Assembler Directives
• Macro directives
sum_of_3 macro arg1, arg2, arg3 ; WREG[arg1]+[arg2]+
; [arg3]
movf arg1,W,A
addwf arg2,W,A
addwf arg3,W,A
endm
sum_of_3 0x01,0x02,0x03 ; WREG[0x01]+[0x02]+
; [0x03]
![Page 9: LSN 3 Assembly Language Programming](https://reader033.fdocuments.in/reader033/viewer/2022060317/62949149d574725d28033cfc/html5/thumbnails/9.jpg)
ECT 358 Microprocessors II [email protected]
LSN 3 – Assembler Directives
test_mac macro chk_cnt
variable i
i = 0
while i < chk_cnt
movlw i
i += 1
endw
endm
start
test_mac 6
end
![Page 11: LSN 3 Assembly Language Programming](https://reader033.fdocuments.in/reader033/viewer/2022060317/62949149d574725d28033cfc/html5/thumbnails/11.jpg)
ECT 358 Microprocessors II [email protected]
LSN 3 – Arithmetic Operations
• PIC18 MCU provides instructions for 8-bit addition,
subtraction, and multiplication
• Addition instructions addwf 0x30,W,A
addwfc 0x30,F,A
addlw 5
• Multi-byte addition
– Operated from LSB to MSB
– Must use carry bit in addition operations
![Page 12: LSN 3 Assembly Language Programming](https://reader033.fdocuments.in/reader033/viewer/2022060317/62949149d574725d28033cfc/html5/thumbnails/12.jpg)
ECT 358 Microprocessors II [email protected]
LSN 3 – Arithmetic Operations
• Subtraction instructions subfwb 0x30,W,A
subwf 0x30,W,A
subwfb 0x30,F,A
sublw 5
• Multi-byte subtraction
– Operated from LSB to MSB
– Must use borrow bit in subtraction operations
![Page 13: LSN 3 Assembly Language Programming](https://reader033.fdocuments.in/reader033/viewer/2022060317/62949149d574725d28033cfc/html5/thumbnails/13.jpg)
ECT 358 Microprocessors II [email protected]
LSN 3 – Arithmetic Operations
• Multiplication instructions mullw 5
mulwf 0x30,A
![Page 18: LSN 3 Assembly Language Programming](https://reader033.fdocuments.in/reader033/viewer/2022060317/62949149d574725d28033cfc/html5/thumbnails/18.jpg)
ECT 358 Microprocessors II [email protected]
LSN 3 – Table Operations
• Ability to store data in the 16-bit program memory
space
• Two instructions are provided that allow processor to
move bytes between program memory and data
memory
– Table read (TBLRD)
– Table write (TBLWT)
![Page 19: LSN 3 Assembly Language Programming](https://reader033.fdocuments.in/reader033/viewer/2022060317/62949149d574725d28033cfc/html5/thumbnails/19.jpg)
ECT 358 Microprocessors II [email protected]
LSN 3 – Table Operations
• The table pointer addresses a byte within the program
memory and consists of three registers
![Page 20: LSN 3 Assembly Language Programming](https://reader033.fdocuments.in/reader033/viewer/2022060317/62949149d574725d28033cfc/html5/thumbnails/20.jpg)
ECT 358 Microprocessors II [email protected]
LSN 3 – Logic Operations
• Applications of Logic Instructions
– Set a few bits in a byte
– Clear certain bits in a byte
– Toggle certain bits in a byte
![Page 21: LSN 3 Assembly Language Programming](https://reader033.fdocuments.in/reader033/viewer/2022060317/62949149d574725d28033cfc/html5/thumbnails/21.jpg)
ECT 358 Microprocessors II [email protected]
LSN 3 – Programming Timing
• Instruction execution time is measured by using the
instruction cycle clock
– 1 instruction cycle = 4 crystal oscillator clock period
• Time delays are often needed to synchronize
operations or provide a set period of execution
– Hardware (timers)
– Software (instructions)
![Page 22: LSN 3 Assembly Language Programming](https://reader033.fdocuments.in/reader033/viewer/2022060317/62949149d574725d28033cfc/html5/thumbnails/22.jpg)
ECT 358 Microprocessors II [email protected]
LSN 3 – Rotate Operations
• Used to manipulate bit fields and multiply/divide a
number
![Page 23: LSN 3 Assembly Language Programming](https://reader033.fdocuments.in/reader033/viewer/2022060317/62949149d574725d28033cfc/html5/thumbnails/23.jpg)
ECT 358 Microprocessors II [email protected]
LSN 3 – Rotate Operations
• Divide and multiply by 2 can be performed by shifting
to the right and left
• Shift instruction make use of the rotate through carry
instructions with a cleared carry flag
![Page 24: LSN 3 Assembly Language Programming](https://reader033.fdocuments.in/reader033/viewer/2022060317/62949149d574725d28033cfc/html5/thumbnails/24.jpg)
ECT 358 Microprocessors II [email protected]
LSN 3 – Homework
• Reading
– 2.1 – 2.15
– Is Assembly Language Obsolete.pdf
• Assignment – HW2
– E2.3, E2.7, E2.14, E2.19, E2.23
• References
– Microchip MCU2121 Course Notes
– Huang, H., PIC Microcontroller: An Introduction to
Software & Hardware Interfacing, Delmar Cengage
Learning 2007