Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 2 SCOPE – Local and Global variables

Similar presentations


Presentation on theme: "Lecture 2 SCOPE – Local and Global variables"— Presentation transcript:

1 Lecture 2 SCOPE – Local and Global variables
Reference passed – passed by value or pointer Recursion: calling itself Stack pointer – temporary memory location Subroutine More about pointer Data Representation 2019/1/18

2 SCOPE Variables have scopes, that is, a variable name may refer to different values depending on where it is used. – variable a in subroutine c and subroutine d is different even the same name If it is used inside a function that declares a local variable of that name, the name will refer to a data item that is private to that function It is defined as global, it means it can be accessed by others. 2019/1/18

3 Example – abc program name
#include <stdio.h> int first; int second; void callee ( int first ) { int second; second = 1; first = 2; printf("callee: first = %d second = %d\n", first, second); } int main (int argc, char *argv[]) { first = 1; second = 2; callee(first); printf("caller: first = %d second = %d\n", first, second); return 0; } Same variable “second”, but different memory location DOS>abc 12 34 Here, argc = 3, argv[0] = abc argv[1]= 12 argv[2] =34 2019/1/18

4 Demonstration – memory dump
Address of first is 0x004235bc in main() 2019/1/18

5 Demonstration – memory dump
Address of first is 0x0065fda8 in callee() 2019/1/18

6 Formal Parameters: Value and Reference*
void callee ( int first ) { int second; second = 1; first = 2; printf("callee: first = %d second = %d\n", first, second); } first = 1 … callee(first) // pass the value of first to callee 2019/1/18

7 Example (passed by pointer) *
void callee ( int * first ) //not a variable, but an address { int second; second = 1; *first = 2; printf("callee: first = %d second = %d\n", *first, second); } int main (int argc, char *argv[]) { first = 1; second = 2; callee(&first); //passed by address printf("caller: first = %d second = %d\n", first, second); return 0; } Content by address 2019/1/18

8 Why should we dump memory?
Look at the program the memory , variable a is 12ff7c, b[0] 12ff68…, it will loop more than 5, interesting 2019/1/18

9 Why should we dump memory?
The software will modify the content of variable a such that this program will loop more than 6. b[0] ff68 b[1] ff6c ………. b[6] ff7c variable a 2019/1/18

10 Recursion* Calling itself, say
callee (n) { if n = 0 return printf(“,,,,,,,”); callee(n-1); } It will create local variable (n) for each call, n , n-1, n – 2…. Until 0 2019/1/18

11 Activation Records and Stacks
When a function is called, it will put the variables and return the address to the stack ( a special memory chunk in the operating system). 2019/1/18

12 The stack pointer It holds the address where the stack ends. It is here that a new activation record will be allocated. The frame pointer holds the address where the previous activation record ends. It is to this value that the stack holds. 2019/1/18

13 Procedure for stack operation
When a function is called, the compiler and hardware: push the frame pointer into the stack set the frame pointer equal to the stack pointer decrement the stack pointer by as many memory addresses as are required to store the local state of the callee. No need to memorise the sequence 2019/1/18 First-in last-out principle for stack

14 Diagram - stack push (create)
Before After 2019/1/18

15 Diagram - stack pop (return)
Before After 2019/1/18

16 How to find the return address in a routine?
For the prorgam, the return address is next address of the variable in the stack pointer Return Address 004010AF Address of variable first 2019/1/18

17 The code itself It means the program code that occupies the address, how to find it? You can dump the memory content or write a simple program. 2019/1/18

18 Example of this simple prorgam – here, assumes that the proggrams start from 0x0040b7d8
#include <stdio.h> int a; int main (int argc, char *argv[]) { int i; unsigned char * c; a = 5; c = ((unsigned char *) 0x0040b7d8); for (i = 0; i < 10; i++) { printf("%02X ", *c); c++; } printf("\n"); } Note the address here 2019/1/18

19 Assembly Code The language that uses by computer directly.
You should understand the difficulty and its relationship with high level language 2019/1/18

20 Its equivalent assembly language
C and assembly if (a == 0) { a = 5; } High level C language Its equivalent assembly language Very complicate 2019/1/18

21 Pointers to an integer and a character
int *pointer char *pointer Can be a function as follows: int (*newvar)(char) newvar = &existing_function; int existing_function (char c) { } 2019/1/18

22 The CPU also has Memory The CPU also maintains its own banks of memory called registers. They temporarily hold the data As a result, the program is faster. faster register Memory hierarchy Cache memory Main memory 2019/1/18 Disk

23 Example of register, look at the following program, a = b + 3*c
int a; int b = 4; int c = 3; int main ( int argc, char * argv[]) { a = b + 3 * c; return 0; } 2019/1/18

24 Set a break Point at a = b + 3*c;
Mov eax, [c (00421b98)] where eax is the register (CPU’s memory) register 2019/1/18

25 Register Allocation and Compiler Optimization
CPU has a few registers, the compiler needs to decide when to assign the registers to speed up the operation. Compiler can set the optimisation level. Question, why don’t we set it faster?, The answer: the program code might be faster. 2019/1/18

26 Example of Optimisation level
2019/1/18

27 Representation of Data
Bits and Bit Manipulation Integers – 32 bits Character – 8 bits 2019/1/18 27

28 Bits and Bit Manipulation
The world in Zeros and Ones Grouping Bits into Words Bit Operations Computers use binary 0 and 1 to represent data and manipulate data Human finds it difficult to understand 1s or 0s 2019/1/18

29 The World in Zeros and Ones
A single unit of this form of data is called a binary digit, or bit for short It can be 1 or 0 It is used to represent the voltage of “high” or “low” 2019/1/18 29

30 Grouping Bits into Words
It is to group bits together, For example, Boolean a; //binary 1 or 0 int i; // 32 bits char a; //8 bits float f; // 32 bits 2019/1/18

31 Data representation – group bits
1 or 0 //binary // 0, hex, group 4 bits // 1 // 2 0011 …… //f (15 decimal) Group 4 bits together 2019/1/18

32 0x9A0477F3 is represented as a 32-bit word:
In memory is different, say the address of this data is 0x0065fdf4 0x0065fdf f3 0x0065fdf 0x0065fdf 0x0065fdf A Memory location in reverse order 2019/1/18

33 Example showing the data in reverse order
2019/1/18

34 In memory, it will be arranged in
Code void main() { int i = 0x9a0477f3; } Understand this, as this is related to your exam. In memory, it will be arranged in f a, interesting 2019/1/18

35 Exercise – to display the contents
#include <stdlib.h> #include <stdio.h> void main() { int i = 0x9a0477f3; char *ptr; ptr = (char *)&i; //type casting for (int j = 0; j <4; j++) printf("The address is %x , value of byte %d is %x\n", ptr + j, j, *(ptr +j) ); } 2019/1/18

36 Summary SCOPE – can use the same name, but different memory locations
Reference passed – when a routine has been called, values of variables will be destroyed. Recursion: very elegant, but wastes CPU time Stack pointer – first-in and last out Subroutine – return address Pointer – very powerful, but difficult to implement Data representation in memory 2019/1/18


Download ppt "Lecture 2 SCOPE – Local and Global variables"

Similar presentations


Ads by Google