Addendum to Chap 2 and Chap 3 Pointers

  • Slides: 7
Download presentation
Addendum to Chap 2 and Chap 3

Addendum to Chap 2 and Chap 3

Pointers • What is a pointer? – A special data that records memory address

Pointers • What is a pointer? – A special data that records memory address • Example in C int a = 3; int *p = NULL; p = &a; *p = 5; variable a p address 0 x 0 A 0 x 0 B value 3 5 0 0 x 0 A

“Function call” in instruction level • Function call is noting but an unconditional jump

“Function call” in instruction level • Function call is noting but an unconditional jump to another instruction. – Need to “jump back” when it returns. – Save the “program counter” in some special place. • Also the parameters. main() { int a = addone(5); printf(“a=%dn”, a); } int addone(int a){ return a+1; } 10 3 FFF # Suppose PC # is R 16 FF 12 2105 # Set R 1 = 5 14 31 FE # Store R 1 to FE 16 B 030 # jump to 0 X 30 18 14 FD # Load data in # FD to R 4 Nowadays, CPU has instructions: call and ret. 30 32 34 36 38 3 A 3 C 11 FE # Load data in FE to R 1 2201 # Set R 2 = 1 5312 # R 3 = R 1 + R 2 33 FD # Store R 3 to FD 11 FF # Load data in FF to R 1 313 C # store return address B 018 # jump to 0 X 18

Global variables vs Local variables • A global variable exists when the program is

Global variables vs Local variables • A global variable exists when the program is loaded to memory – Recall homework 3 – It’s address is fixed • A local variable is created when the function is called Global int a; main() { a = 5; printf(“%dn”, adda(a)); } int adda(int b){ int a = 3; Local return a+b; } – Will disappear when the function returns. – For C language, if a local variable has the same name as a global variable, it refers to the local variable (inside the function).

Interrupt • A signal from the hardware that tells the software to perform an

Interrupt • A signal from the hardware that tells the software to perform an operation. – Operation is handled by the ISR (Interrupt Service Routine) for that interrupt request (IRQ). • ISR is a special function, but – When to call it is decided by the interrupt, not by users’ program – All user program data need be saved, so that when the ISR finishes, user program can continue

Defragmentation • File system fragmentation: A B F C D F G E H

Defragmentation • File system fragmentation: A B F C D F G E H F H – Will slow down system performance • Defragmentation: a process to reduce the fragmentations A C F H – Thus improves the performance

Sleep vs Hibernation • Sleep; standby; suspend • The computer cuts power to all

Sleep vs Hibernation • Sleep; standby; suspend • The computer cuts power to all unneeded parts of the machine, aside from the memory which is required to restore the machine's state. • Hibernation • The data in memory is copied to the hard drive, and the computer is powered off • When power is on, the memory contents are restored from hard drive.