3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! •...

39
3. ALU and jumps Rev 1.72 Dr Pierre-André Mudry [email protected] Information systems - µC

Transcript of 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! •...

Page 1: 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! • Modified by the ALU as the result of an instruction. Don't write there! • Not every

3. ALU and jumps

Rev 1.72Dr Pierre-André Mudry

[email protected]

Information systems - µC

Page 2: 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! • Modified by the ALU as the result of an instruction. Don't write there! • Not every

Objectives

P.-A. Mudry ALU and jumps 2

ALU and jumps in code

Assembly programming techniques ALU flags and implementation Jumps and conditional execution

Page 3: 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! • Modified by the ALU as the result of an instruction. Don't write there! • Not every

Disclaimer

The stack slides were adapted fromM. Bianchi's former course.

P.-A. Mudry ALU and jumps 3

Page 4: 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! • Modified by the ALU as the result of an instruction. Don't write there! • Not every

ASSEMBLY PROGRAMMINGTECHNIQUES

How does it work?

P.-A. Mudry ALU and jumps 4

Page 5: 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! • Modified by the ALU as the result of an instruction. Don't write there! • Not every

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/

Page 6: 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! • Modified by the ALU as the result of an instruction. Don't write there! • Not every

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

Page 7: 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! • Modified by the ALU as the result of an instruction. Don't write there! • Not every

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)

Page 8: 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! • Modified by the ALU as the result of an instruction. Don't write there! • Not every

P.-A. Mudry ALU and jumps 8

A simple loopbegin:

movlw .5

decrement:decf WREGtstfsz WREGgoto decrement

rest_of_code:…

Page 9: 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! • Modified by the ALU as the result of an instruction. Don't write there! • Not every

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

Page 10: 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! • Modified by the ALU as the result of an instruction. Don't write there! • Not every

2. THE ALUDive deep inside

P.-A. Mudry ALU and jumps 10

Page 11: 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! • Modified by the ALU as the result of an instruction. Don't write there! • Not every

Organisation

• What are all those things?

• Registers withspecialfunctions…

P.-A. Mudry ALU and jumps 12

Source: Microchip PIC18F6680 datasheet

Page 12: 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! • Modified by the ALU as the result of an instruction. Don't write there! • Not every

ALU in detail

P.-A. Mudry ALU and jumps 13

Page 13: 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! • Modified by the ALU as the result of an instruction. Don't write there! • Not every

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

Page 14: 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! • Modified by the ALU as the result of an instruction. Don't write there! • Not every

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."

Page 15: 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! • Modified by the ALU as the result of an instruction. Don't write there! • Not every

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

Page 16: 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! • Modified by the ALU as the result of an instruction. Don't write there! • Not every

STATUS bits description

• N

• OV

• Z

• DC

• C

P.-A. Mudry ALU and jumps 17

Page 17: 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! • Modified by the ALU as the result of an instruction. Don't write there! • Not every

P.-A. Mudry ALU and jumps 18

STATUS modificationclrf WREGmovlw 0x1addlw 1movlw 0x80addlw 0movlw 0x7faddlw 1movlw 0xffaddlw 1

Page 18: 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! • Modified by the ALU as the result of an instruction. Don't write there! • Not every

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

Page 19: 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! • Modified by the ALU as the result of an instruction. Don't write there! • Not every

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

Page 20: 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! • Modified by the ALU as the result of an instruction. Don't write there! • Not every

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

Page 21: 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! • Modified by the ALU as the result of an instruction. Don't write there! • Not every

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

Page 22: 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! • Modified by the ALU as the result of an instruction. Don't write there! • Not every

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

Page 23: 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! • Modified by the ALU as the result of an instruction. Don't write there! • Not every

P.-A. Mudry PIC18F6680 24

Serie 1

Page 24: 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! • Modified by the ALU as the result of an instruction. Don't write there! • Not every

JUMPS AND FUNCTION CALLSChanging the instruction flow

P.-A. Mudry ALU and jumps 25

Page 25: 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! • Modified by the ALU as the result of an instruction. Don't write there! • Not every

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

Page 26: 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! • Modified by the ALU as the result of an instruction. Don't write there! • Not every

Program adresses

P.-A. Mudry ALU and jumps 27

Page 27: 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! • Modified by the ALU as the result of an instruction. Don't write there! • Not every

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

Page 28: 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! • Modified by the ALU as the result of an instruction. Don't write there! • Not every

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

Page 29: 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! • Modified by the ALU as the result of an instruction. Don't write there! • Not every

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

Page 30: 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! • Modified by the ALU as the result of an instruction. Don't write there! • Not every

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

Page 31: 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! • Modified by the ALU as the result of an instruction. Don't write there! • Not every

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, …)

Page 32: 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! • Modified by the ALU as the result of an instruction. Don't write there! • Not every

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

Page 33: 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! • Modified by the ALU as the result of an instruction. Don't write there! • Not every

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

Page 34: 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! • Modified by the ALU as the result of an instruction. Don't write there! • Not every

The stack

P.-A. Mudry ALU and jumps 36

Page 35: 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! • Modified by the ALU as the result of an instruction. Don't write there! • Not every

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

Page 36: 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! • Modified by the ALU as the result of an instruction. Don't write there! • Not every

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

Page 37: 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! • Modified by the ALU as the result of an instruction. Don't write there! • Not every

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

Page 38: 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! • Modified by the ALU as the result of an instruction. Don't write there! • Not every

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

Page 39: 3. ALU and jumpssin.begincoding.net/lectures/3 ALU and jumps.pdf• Our first SFR in detail! • Modified by the ALU as the result of an instruction. Don't write there! • Not every

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