Clark University Computer Science Department CSCI 140 Computer Organization Introduction to PIC Instruction Set Architecture Part I - The Software - PowerPoint PPT Presentation

1 / 60
About This Presentation
Title:

Clark University Computer Science Department CSCI 140 Computer Organization Introduction to PIC Instruction Set Architecture Part I - The Software

Description:

Clark University Computer Science Department CSCI 140 Computer Organization Introduction to PIC Instruction Set Architecture Part I - The Software Organizational ... – PowerPoint PPT presentation

Number of Views:148
Avg rating:3.0/5.0
Slides: 61
Provided by: JerryBr
Category:

less

Transcript and Presenter's Notes

Title: Clark University Computer Science Department CSCI 140 Computer Organization Introduction to PIC Instruction Set Architecture Part I - The Software


1
Clark UniversityComputer Science DepartmentCSCI
140 Computer OrganizationIntroduction to
PICInstruction Set ArchitecturePart I- The
Software
2
Organizational Issues
  • There should be place for the following
  • Concept of registers
  • Instruction syntax and format - RISC
  • Immediates, constants, locals
  • Dissassembly, pseudo instructions, multiply and
    divide
  • Instruction timing
  • Memory addressing direct, indirect, relative
  • Map ifs into conditional branching
  • Map inequalities into conditional branching
  • Use the keywords bitwise and logical
    operators

3
A Translation Hierarchy
  • High Level Language (HLL) programs first compiled
    (possibly into assembly), then linked and finally
    loaded into main memory.

4
PIC Instruction Set Architecture (Summary)
  • Machine Environment Target
  • Instruction Categories
  • Load/Store
  • Computational

Registers
W
PC
Data
5
Harvard Architecture
  • Data memory is separate from Program memory.
  • (Intel et.al, have von Neumann integrated
    memory.)
  • Allows data and program information to move
    separately and so is faster than von Neumann.

6
Detours Memory and bases
  • What is memory?
  • Words,
  • Bytes,
  • Nibbles,
  • Instruction size (number of bits)
  • Data size (number of bits)
  • What quantity of instructions or data can be
    addressed?
  • Bases We typically use
  • decimal,
  • Binary,
  • Hex
  • Lets practice all of these a bit today.

7
Review C Operators/operands
  • Operators , -, , /, (mod) (7/41, 743)
  • Operands
  • Variables fahr, celsius
  • Constants 0, 1000, -17, 15.4
  • In C (and most High Level Languages) variables
    declared and given a type first
  • Example int fahr, celsius int a, b, c, d, e

8
C Operators/operands
  • This is the Computation Model by
    State-Effects''.
  • Programs move from state to state by means of
    assignments changing their state
  • Assignment Statement
  • Variable expression, e.g.,
  • celsius 5(fahr-32)/9
  • a bcd-e

9
Assembly Operators
  • Syntax of Assembly Operator
  • 1) operation by name Mnemonics''
  • 2) operand getting result Register or Memory
  • 3) 1st operand for operation
  • 4) 2nd operand for operation
  • Ex. add b to c and put the result in b
  • addwf Location,w
  • Called an Assembly Language Instruction
  • Equivalent assignment statement in C
  • b b c

10
Assembly Operators/instructions
  • How to do the following C statement? a b c
    d - e
  • Break into multiple instructions
  • To right of semicolon () is a comment terminated
    by end of the line. Applies only to current
    line.
  • C comments have format / comment / , can span
    many lines
  • movfw b Get the value of b in the
    register
  • addw c,w Add the value in b to the
    value in c and move to register
  • movwf a,f Store this first sum in a
  • movfw d Get the value of d into the
    register
  • addw a,f Add this new value to a
  • movfw e Get the value of e into the
    register
  • subwf a,f Subtract e from a and keep
    the value in location a.

11
Assembly Operators/instructions
  • Note Unlike C (and most other HLLs), each line
    of assembly contains at most one
    instruction add a,b,c add d,e,f
    WRONG add a,b,c add d,e,f RIGHT

12
Compilation
  • How to turn the notation that programmers prefer
    into notation computer understands?
  • Program to translate C statements into Assembly
    Language instructions called a compiler
  • Example compile by hand this C code a b
    c
  • Easy
  • movfw a
  • addw b, w
  • movwf c
  • Big Idea compiler translates notation from
    one level of computing abstraction to lower level

13
Compilation -- Summary
  • C statement (5 operands, 3 operators) f (g
    h) - (i j)
  • Becomes 7 assembly instructions (6 unique
    operands, 3 operators)
  • In general, each line of C produces many assembly
    instructions
  • One reason why people program in C vs. Assembly
    fewer lines of code
  • Other reasons? (many!)
  • movfw g Get the value of g in the
    register
  • addw h,w Add the value in g to the
    value in h and move to register
  • movwf f Store this first sum in total
  • movfw i Get the value of i into the
    register
  • subw f,f Subtract this new value from
    total
  • movfw j Get the value of j into the
    register
  • subwf f,f Subtract j from total and
    keep the value in total.

14
These are the 35 instructions available on some
PIC processors.
15
(No Transcript)
16
A Simple First Program
The goal is to do a loop 256 times
New Instructions clrf decfsz goto
org 0x10 Counter db 0 clrf
Counter Loop decfsz Counter goto
Loop ---- ----
17
New Instructions clrf decfsz goto
18
New Instructions clrf decfsz goto
19
New Instructions clrf decfsz goto
20
New Instructions clrf decfsz goto
Related Instructions clrw incfsz decf incf
21
New Instructions clrf decfsz goto
Related Instructions clrw incfsz decf incf
22
New Instructions clrf decfsz goto
Related Instructions clrw incfsz decf incf
23
New Instructions clrf decfsz goto
Related Instructions clrw incfsz decf incf
24
So What Is This Simulator?
MPLAB IDE v7.50
  • Microchip is the manufacturer of both PIC
    microcontroller and of software tools to support
    that hardware. Its to their advantage to make
    it easy to develop software for the chips so that
    people will buy more.
  • (They arent going to get rich based on what we
    buy!)
  • MPLAB is software that includes
  • Assembler taking code from mnemonics to HEX
  • Simulator produces an environment similar to
    that on the hardware but its running on YOUR PC.
    Essential for debugging and understanding.
  • Programmer gets the code onto the chip.

You can get it here and run it on your own PC at
home.
25
A Simple Second Program
New Instructions movlw subwf btfss
It does the same thing as the first program!
The goal is to do a loop 256 times
Related Instructions addlw sublw addwf btfsc
org 0x10 Counter db 0 clrf
Counter movlw 1 Loop subwf
Counter,f btfss STATUS,Z goto
Loop ---- ----
Previous Instructions clrf clrw decfsz incfsz dec
f incf goto
26
New Instructions movlw subwf btfss
Previous Instructions clrf clrw decfsz incfsz dec
f incf goto
27
New Instructions movlw subwf btfss
Previous Instructions clrf clrw decfsz incfsz dec
f incf goto
28
New Instructions movlw subwf btfss
Our Example
btfss STATUS,Z
Previous Instructions clrf clrw decfsz incfsz dec
f incf goto
29
New Instructions movlw subwf btfss
Related Instructions addlw sublw addwf btfsc
Previous Instructions clrf clrw decfsz incfsz dec
f incf goto
30
New Instructions movlw subwf btfss
Related Instructions addlw sublw addwf btfsc
Previous Instructions clrf clrw decfsz incfsz dec
f incf goto
31
New Instructions movlw subwf btfss
Related Instructions addlw sublw addwf btfsc
Previous Instructions clrf clrw decfsz incfsz dec
f incf goto
32
New Instructions movlw subwf btfss
Related Instructions addlw sublw addwf btfsc
Previous Instructions clrf clrw decfsz incfsz dec
f incf goto
33
Can You Guess The Final Number?
New Instructions andlw iorlw xorlw
What value is in W after we execute these
instructions?
Related Instructions andwf iorwf xorwf
movlw 0x37 andlw 0xF3 iorlw 0x82 xorlw
0x38 ---- ----
Could be written movlw 0x37 movlw .55
movlw B00110111
Previous Instructions addlw addwf subwf sublw btf
sc btfss clrf clrw decfsz incfsz decf incf movlw g
oto
These are literal instructions. These are logical
instructions. What do these words mean???
34
New Instructions andlw iorlw xorlw
Previous Instructions addlw addwf subwf sublw btf
sc btfss clrf clrw decfsz incfsz decf incf movlw g
oto
35
New Instructions andlw iorlw xorlw
Previous Instructions addlw addwf subwf sublw btf
sc btfss clrf clrw decfsz incfsz decf incf movlw g
oto
36
New Instructions andlw iorlw xorlw
Previous Instructions addlw addwf subwf sublw btf
sc btfss clrf clrw decfsz incfsz decf incf movlw g
oto
37
New Instructions andlw iorlw xorlw
Related Instructions andwf iorwf xorwf
Previous Instructions addlw addwf subwf sublw btf
sc btfss clrf clrw decfsz incfsz decf incf movlw g
oto
38
New Instructions andlw iorlw xorlw
Related Instructions andwf iorwf xorwf
Previous Instructions addlw addwf subwf sublw btf
sc btfss clrf clrw decfsz incfsz decf incf movlw g
oto
39
New Instructions andlw iorlw xorlw
Related Instructions andwf iorwf xorwf
Previous Instructions addlw addwf subwf sublw btf
sc btfss clrf clrw decfsz incfsz decf incf movlw g
oto
40
Lets Try Another Program
New Instructions movfw movwf
Given a number N, write a program that produces
the sum of the numbers 1 .. N S. For
example, if N 5, then S 1 2 3 4 5
15. We can write this program in lots of ways.
org 0x10 Sum db 0 Counter db
0 clrf Sum This will hold
sum movwf Counter Holds the value
of N Loop movfw Counter addwf Sum
Keep the running sum decfsz
Counter,f Are we done? goto Loop
---- ----
Previous Instructions Previous Instructions
addlw addwf subwf sublw btfsc btfss clrf clrw decfsz incfsz decf incf movlw goto andlw iorlw xorlw andwf iorwf xorwf
Does this program do what we want???
41
New Instructions movfw movwf
Previous Instructions Previous Instructions
addlw addwf subwf sublw btfsc btfss clrf clrw decfsz incfsz decf incf movlw goto andlw iorlw xorlw andwf iorwf xorwf
42
New Instructions movfw movwf
Previous Instructions Previous Instructions
addlw addwf subwf sublw btfsc btfss clrf clrw decfsz incfsz decf incf movlw goto andlw iorlw xorlw andwf iorwf xorwf
43
Lets Try Another Program
Given a number N, write a program that produces
the sum of the numbers 1 .. N S. For
example, if N 5, then S 1 2 3 4 5
15. We can write this program in lots of ways.
New Instructions call retlw
movlw 5 Holds the value of N
call GetSum ----
---- GetSum addwf PCL,f Add to prog.
Cntr. retlw 1 N 0
retlw 1 N 1 retlw 3
N 2 retlw 6 N 3
retlw 10 N 4 retlw 15
N 5 retlw 21 N 6
retlw 28 N 7
Previous Instructions Previous Instructions
addlw addwf subwf sublw btfsc btfss clrf clrw decfsz incfsz decf incf andlw iorlw xorlw andwf iorwf xorwf movlw movfw movwf goto
Does this program do what we want??? How can we
break this routine?
44
New Instructions call retlw
Previous Instructions Previous Instructions
addlw addwf subwf sublw btfsc btfss clrf clrw decfsz incfsz decf incf andlw iorlw xorlw andwf iorwf xorwf movlw movfw movwf goto
So, what is a stack? Do a detour to explain
this.
45
New Instructions call retlw
Previous Instructions Previous Instructions
addlw addwf subwf sublw btfsc btfss clrf clrw decfsz incfsz decf incf andlw iorlw xorlw andwf iorwf xorwf movlw movfw movwf goto call retlw
46
Lets Try Another Program
Given a number N, write a program that produces
the sum of the numbers 1 .. N S. For
example, if N 5, then S 1 2 3 4 5
15. We can write this program in lots of ways.
New Instructions return
org 0x10 Sum db 0 movlw
5 Holds the value of N clrf
Sum This will hold sum Loop call
DoSum sublw 1 btfss STATUS,Z
Are we done? goto Loop ----
---- DoSum addwf Sum,f
Add our value to sum return
Previous Instructions Previous Instructions
addlw addwf subwf sublw btfsc btfss clrf clrw decfsz incfsz decf incf andlw iorlw xorlw andwf iorwf xorwf movlw movfw movwf goto call retlw
Does this program do what we want???
47
New Instructions return
Previous Instructions Previous Instructions
addlw addwf subwf sublw btfsc btfss clrf clrw decfsz incfsz decf incf andlw iorlw xorlw andwf iorwf xorwf movlw movfw movwf goto call retlw
48
Can You Guess The Final Number?
New Instructions comf swapf
What value is in W after we execute these
instructions?
org 0x10 Value db 0 movlw
0x37 movwf Value comf
Value,F decf Value,F swapf
Value,W ---- ----
Could be written movlw 0x37 movlw .55
movlw B00110111
Previous Instructions Previous Instructions
addlw addwf subwf sublw btfsc btfss clrf clrw decfsz incfsz decf incf andlw iorlw xorlw andwf iorwf xorwf movlw movfw movwf goto call retlw return
After these instructions are executed What is
the number in Value? What is the number in W ?
49
New Instructions comf swapf
Previous Instructions Previous Instructions
addlw addwf subwf sublw btfsc btfss clrf clrw decfsz incfsz decf incf andlw iorlw xorlw andwf iorwf xorwf movlw movfw movwf goto call retlw return
50
New Instructions comf swapf
Previous Instructions Previous Instructions
addlw addwf subwf sublw btfsc btfss clrf clrw decfsz incfsz decf incf andlw iorlw xorlw andwf iorwf xorwf movlw movfw movwf goto call retlw return
51
(No Transcript)
52
(No Transcript)
53
(No Transcript)
54
New Instructions call movwf
Previous Instructions Previous Instructions
addlw addwf subwf sublw btfsc btfss clrf clrw decfsz incfsz decf incf andlw iorlw xorlw andwf iorwf xorwf movlw movfw movwf goto
55
(No Transcript)
56
(No Transcript)
57
(No Transcript)
58
Some real code. This code was produced by a
compiler. Can we analyze what its doing?
SOURCE CODE int main() int
a, b 7, c 4, i for ( i 0 i lt
1000 i ) a b c
LISTING processor
16F877A 16 17
psect text0 18 07DF
main 19 a assigned to
main0 20 0000 maina set
main 21 b assigned to
main2 22 0000 mainb set
main2 23 c assigned to
main4 24 0000 mainc set
main4 25 i assigned to
main6 26 0000 maini set
main6
59
LISTING 27 foo.c 1 int
main() 28 07DF 3007 movlw
7 29 07E0 1283 bcf
STATUS,5 Set for Bank 0 30 07E1 1303
bcf STATUS,6 31 07E2 00A2
movwf main2 b - lsb 32
07E3 01A3 clrf main3 33
07E4 3004 movlw 4 34 07E5
00A4 movwf main4 35 07E6
01A5 clrf main5 36
foo.c 3 for ( i 0 i lt 1000 i ) 37
07E7 01A6 clrf main6 i -
lsb 38 07E8 01A7 clrf main7
i - msb 39 07E9 l2 40
foo.c 4 a b c 41 07E9 0824
movf main4,w c - lsb 42
07EA 0722 addwf main2,w b -
lsb 43 07EB 00A0 movwf main
a - lsb 44 07EC 0825 movf
main5,w c - msb 45 07ED 1803
btfsc STATUS,C Is there a carry? 46
07EE 0A25 incf main5,w 47
07EF 0723 addwf main3,w b -
msb 48 07F0 00A1 movwf
main1 a - msb
60
LISTING Add 1 to i
49 07F1 0AA6 incf main6 i
- lsb 50 07F2 1903 btfsc
STATUS,Z 51 07F3 0AA7 incf
main7 i - msb 52 07F4 0827
movf main7,w i msb
Determine if i is lt 1000 53
07F5 3A80 xorlw 128 54 07F6
00F0 movwf btemp 55 07F7
3083 movlw 131 56 07F8 0270
subwf btemp,w 57 07F9 30E8
movlw 232 59 07FA 1903
btfsc STATUS,Z 60 07FB 0226
subwf main6,w i - lsb 61 07FC
1C03 btfss STATUS,C 62 07FD
2FE9 goto l2 63
foo.c 5 64 07FE 0183 clrf
STATUS 65 07FF 2800 ljmp
start Program done
Write a Comment
User Comments (0)
About PowerShow.com