70 likes | 386 Views
Addendum to Chap 2 and Chap 3. 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;. 3. 5. 0. 0x0A. “Function call” in instruction level.
E N D
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; 3 5 0 0x0A
“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=%d\n”,a); } int addone(int a){ return a+1; } 10 12 14 16 18 3FFF # Suppose PC # is R16FF 2105 # Set R1 = 5 31FE # Store R1 to FE B030 # jump to 0X30 14FD # Load data in # FD to R4 30 32 34 36 38 3A 3C 11FE # Load data in FE to R1 2201 # Set R2 = 1 5312 # R3 = R1 + R2 33FD # Store R3 to FD 11FF # Load data in FF to R1 313C # store return address B018 # jump to 0X18 Nowadays, CPU has instructions: call and ret.
Global variables vs Local variables Global • A global variable exists when the program is loaded to memory • Recall homework3 • It’s address is fixed • A local variable is created when the function is called • 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). Local int a; main() { a = 5; printf(“%d\n”,adda(a)); } int adda(int b){ int a = 3; return a+b; }
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: • Will slow down system performance • Defragmentation: a process to reduce the fragmentations • Thus improves the performance A B F C F D E G H F H A C F H
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.