3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! •...
Transcript of 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! •...
Objectives
P.-A. Mudry ALU and jumps 2
ALU and jumps in code
Assembly programming techniques ALU flags and implementation Jumps and conditional execution
Disclaimer
The stack slides were adapted fromM. Bianchi's former course.
P.-A. Mudry ALU and jumps 3
ASSEMBLY PROGRAMMINGTECHNIQUES
How does it work?
P.-A. Mudry ALU and jumps 4
Instructions seen so far movwf f
• Move (W) to register f movf f, W
• Move register f to (W) movff fs, fd
• Move register (fs) to register (fd) movlw k
• Move literal value (an 8 bits number) to (W) goto k
• Load k (20 bits) into PC[20..1]. k usually corresponds to a label addlw k
• w (w) + k, where k is 8 bits addwf f,d
• d (w) + (f), where d can be either 0 or 1
P.-A. Mudry ALU and jumps 5
Brief overview http://www.mikroe.com/chapters/view/10/chapter-9-instruction-set/
P.-A. Mudry ALU and jumps 6
Destination bit d, 0 or 1 ?aka W or F
x equ 0x3
begin:movlw .5movff WREG, xmovlw .3addwf x, W
Skipping instructionsto implement an if
• Conditions are easy to implement in assembly skip next instruction if some condition is true
P.-A. Mudry ALU and jumps 7
Mnemonic EffectTSTFSZ f Test register (f), skip next instruction if content is zeroCPFSEQ f CPFSGT f CPFSLT f
Compare register (f) with WREG, skip if equal, > or <
DECFSZ f DCFSNZ fINCFSZ f INCFSNZ f
Increment/decrement (f), skip if == 0 (or skip if ≠ 0)
BTFSC f, x BTFSS f, x
Test bit X from register (f), skip if == 0 (or skip if ≠ 0)
P.-A. Mudry ALU and jumps 8
A simple loopbegin:
movlw .5
decrement:decf WREGtstfsz WREGgoto decrement
rest_of_code:…
P.-A. Mudry ALU and jumps 9
i equ 0x equ 1
movlw 5 ; The value to checkmovwf ibtfss i, 0 ; Check if even or oddgoto evengoto odd
even:movlw .10movwf xgoto loop
odd:movlw .20movwf xgoto loop
loop:goto loop
Adresses between 0 and 95 are located in data memory
2. THE ALUDive deep inside
P.-A. Mudry ALU and jumps 10
Organisation
• What are all those things?
• Registers withspecialfunctions…
P.-A. Mudry ALU and jumps 12
Source: Microchip PIC18F6680 datasheet
ALU in detail
P.-A. Mudry ALU and jumps 13
Exercise 3.0
• What is the content of the first byte of memory (which contains the value 10 at the beginning) and of Wafter the following code:
P.-A. Mudry ALU and jumps 14
movlw 3addwf 0x00, 1
ALU presented is not 100% complete
• More stuff is required for multiplication Why ?
• Comparisons• …
P.-A. Mudry ALU and jumps 15
Word of the wise : "Even the simplest thing can be complexto implement."
The STATUS register
• Our first SFR in detail!• Modified by the ALU as the result of
an instruction. Don't write there!• Not every bit modified by every
operation
P.-A. Mudry ALU and jumps 16
STATUS bits description
• N
• OV
• Z
• DC
• C
P.-A. Mudry ALU and jumps 17
P.-A. Mudry ALU and jumps 18
STATUS modificationclrf WREGmovlw 0x1addlw 1movlw 0x80addlw 0movlw 0x7faddlw 1movlw 0xffaddlw 1
The STATUS register (2)
• Serves for implementing if in higher language, conditions values comparisons out of range computations
• Coding techniques will be discussed in detail later!
P.-A. Mudry ALU and jumps 19
Operations NOT affecting status
• Some move operations movlw, movwf, movff
• Branch operations• Increment/decrement with skip incfsz, incfsnz
P.-A. Mudry ALU and jumps 20
Exercise 3.1
• Consider that WREG = 1, address a = 255 All status flags are equal to 0.
• What is the status of C, Z, OV, N afterthe following (independent) operations?1. clrf WREG2. movlw .53. subwf a4. addwf a
P.-A. Mudry ALU and jumps 21
Exercise 3.2
• With the instructions seen so far, how can youcheck that memory locations 0x10 and 0x21have the same content? Explain with code
• toto is a byte declared in memory. What's the purpose of this operation:
movf toto, 1
P.-A. Mudry ALU and jumps 22
Remark: values at power-up / reset
• The content of the data memory (GPR) at power-up/reset is UNKNOWN.
• The content of the SFRs (specialregisters, such as TRISA, STATUS, PORTA, …) is … complicated One must look at the datasheet
P.-A. Mudry ALU and jumps 23
P.-A. Mudry PIC18F6680 24
Serie 1
JUMPS AND FUNCTION CALLSChanging the instruction flow
P.-A. Mudry ALU and jumps 25
The program counter register (PC)
• Contains the address of nextinstruction that will be executed
• Serves for loops / function calls• Width of the PC: • Reason:
P.-A. Mudry ALU and jumps 26
Program adresses
P.-A. Mudry ALU and jumps 27
PC characteristics
• Several ways to update it 1. Automatic update (every cycle, +2) 2. Load a value into it
• Problem : 21 bits is much more than 8 bit (datapath) !
P.-A. Mudry ALU and jumps 28
Source: Microchip
PIC18F6680 datasheet
Accessing PC contents
• PC is composed of PCL: the lower byte, writable. Last bit
set to 0 in hardware PCH: higher byte, accessible trough the
PCLATH register PCU: the upper byte, accessible
throught the PCLATU
P.-A. Mudry ALU and jumps 29
Relative and absolute jumps
• Absolute jumps (absolute value loaded into PC) PC absolute value
• Relative jumps (relative to currentPC address), PC PC + offset
ALU and jumps 30P.-A. Mudry
Situation 1- GOTO instructionJumping anywhere
P.-A. Mudry ALU and jumps 31
var1 equ 0x10var2 equ 0x21
movlw 0x1movwf var1
loop:incf var1, fgoto loop
0000 0E01 MOVLW 0x1 movlw 0x10002 6E10 MOVWF 0x10, ACCESS movwf var10004 2A10 INCF 0x10, F, ACCESS incf var1, f0006 EF02 GOTO 0x4 goto loop0008 F000 NOP
Source: Microchip PIC18F6680 datasheet
Situation 2 - Relative branchJumping near the current location
P.-A. Mudry ALU and jumps 32
var1 equ 0x10var2 equ 0x21
movlw 0x1movwf var1
loop:incf var1, fbra loop
0000 0E01 MOVLW 0x1 movlw 0x10002 6E10 MOVWF 0x10, ACCESS movwf var10004 2A10 INCF 0x10, F, ACCESS incf var1, f0006 D7FE BRA 0x4 bra loop
• Addresses computedby the assembler
• Can be negativeaddresses (literal issigned, 11 bits)
• Various forms: branch if (zero, negative, …)
Function callsAssembler perspective
• Until now, we did not have function calls.• A function (without parameter at the
moment) should
P.-A. Mudry ALU and jumps 33
Internal stack
• The PIC18F has a stack• Is a special data structure to store
data.• Operations: PUSH, put something on top POP, remove something from top
• LIFOP.-A. Mudry ALU and jumps 35
The stack
P.-A. Mudry ALU and jumps 36
Function calls (2)Assembler perspective
• Special instruction for this : call Stores the return PC address
onto the stack Jumps to an address with
goto Stores WREG, STATUS, BSR
(in FAST mode, 1 level deep)P.-A. Mudry ALU and jumps 37
Source: Microchip PIC18F6680 datasheet
Function calls (3)Assembler perspective
• When function is over return Restores the PC Restores WREG, STATUS, BSR (is FAST mode is used)
P.-A. Mudry ALU and jumps 38
Source: Microchip PIC18F6680 datasheet
Fast modeFor calling sub-routines
• Allows to store a copy of WREG, STATUS and BSR register.
• Limited Only one level Can't be used in nested calls Does not work with interrupts
P.-A. Mudry ALU and jumps 40
Function calls (4)
• Return values? Could use WREG, but limited
• Arguments? Could use WREG, but limited
• Recursion Very limited (max 31 levels deep)We will let the compiler do this later
P.-A. Mudry ALU and jumps 41
What we’ve seen in this lecture
• How the ALU works• How are jumps and calls implemented• YOUR JOB FOR NEXT LESSON: Read in PIC18 datasheet, available on
http://sin.begincoding.net/documentation/• Page 78 (reset state of registers)• Pages 93-97 (instructions encoding)
What's next?Instruction pipeline, IOs
Instructions encoding 42P.-A. Mudry