46
Bitwise Operators Pointers Functions Structs Interrupts (in C)

Bitwise Operators Pointers Functions Structs Interrupts (in C)

  • View
    227

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Bitwise Operators Pointers Functions Structs Interrupts (in C)

Bitwise Operators

Pointers

Functions

Structs

Interrupts

(in C)

Page 2: Bitwise Operators Pointers Functions Structs Interrupts (in C)

table_07_00

Bitwise operators: useful for dealing with signals of different widths;NOTE: these are LOGICAL operators and variables are declared UNSIGNED—why?(example: homework 1, problem 1)

Additional useful reference: http://www.cs.cf.ac.uk/Dave/C/node13.html

Page 3: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_00

Examples of c operations at the byte level

Page 4: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_01

Example of c program—”portShadow” mirrors what is on port;Note use of parentheses to guarantee order of bitlevel operations (even if we are using default order)

Page 5: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_02

Using shift operators (remember these are logical):

Page 6: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_03

Redoing the previous problem with shifts:

Page 7: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_04

Getting data from the port:

Page 8: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_05

Using bitlevel operations for arithmetic;Are there any problems with this approach?

C: on signed data, left shift undefined if overflow occurs, right shift is implementation-dependent;Java: all integers are signed

Page 9: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_06

Can use shifts for slightly more complex multiplies and divides;More complex operations (e.g., with 3 1’s in multiplier or divider) are probably not efficient

Page 10: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_07

Pointers: example data

Page 11: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_09

Example instruction:

Page 12: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_10

Example 2 of Instruction execution

Page 13: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_11

Example: what is output?

Page 14: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_12a

Pointer arithmetic—not for beginners!

Page 15: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_12b

Pointer arithmetic: additional examples

Page 16: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_13

Constant pointers:

Page 17: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_14

Using constants and constant pointers:

Page 18: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_14

Note: pointers can be “generic” or NULL:

Generic:Type void: pointer can point to a variable of any type

example: 7.3, p. 265

NULL:Pointer needs to be assigned a valid address before dereferencing, otherwise hard-to-find bugs can occur

Address 0 is not acceptable

Solution: use null pointer address, (void*) 0

Page 19: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_15

C functions:

Page 20: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_16

Example:

Page 21: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_17

Function call: note order of parameters on stack

Page 22: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_18

Using stack to return result:

Function body in memory:

Page 23: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_20

Pass by value (default in c):

Page 24: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_21

Pass by reference:

Page 25: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_22

Information hiding: static qualifier prevents visibility outside this file (even to linker):

Page 26: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_23

Function documentation: template for header

Page 27: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_24

We can also define pointers to functions:

Dereferencing methods:

Page 28: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_26

Example:

Page 29: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_27

Example 2:

Page 30: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_28

Pointing to add:

Page 31: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_29

Pointing to subtract: pointer does not know functionality, only address

Page 32: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_31

User-defined data structure: struct

Page 33: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_30

User-defined data structure: struct

Example:

Page 34: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_34

Can also define a type and use it repeatedly:

Page 35: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_35

Syntax for using struct:

Page 36: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_36

Example: define a rectangle

Page 37: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_37

One struct using another:

Page 38: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_38

C code for this example:

Page 39: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_39

Definign the rectangle type:

Page 40: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_40

Using point and rectangle definitions:

Page 41: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_41

Rectangle functions:

Page 42: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_42

Example program:

Page 43: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_43

Example: passing a struct to a function:

Page 44: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_44

Interrupt service routines: ISR—needs to be SHORT and SIMPLE

Page 45: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_45

How an interrupt occurs:

Interrupt may be disabled under certain conditions

Page 46: Bitwise Operators Pointers Functions Structs Interrupts (in C)

fig_07_46

Enable / disable control mechanisms:

Global

Maskingmethod 1: use prioritiesmethod 2: use mask register

(bitwise masks)

Note: interrupts are transient, if we choose to ignore one we may not be able to service it later