Lecture 2 SCOPE – Local and Global variables

Slides:



Advertisements
Similar presentations
1/1/ / faculty of Electrical Engineering eindhoven university of technology Introduction Part 2: Data types and addressing modes dr.ir. A.C. Verschueren.
Advertisements

10/9: Lecture Topics Starting a Program Exercise 3.2 from H+P Review of Assembly Language RISC vs. CISC.
1 Nested Procedures Procedures that don't call others are called leaf procedures, procedures that call others are called nested procedures. Problems may.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
Kernighan/Ritchie: Kelley/Pohl:
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
Accessing parameters from the stack and calling functions.
Run time vs. Compile time
C Stack Frames / Pointer variables Stack: Local Variables Pass & Return values Frame Ptr linkage (R5) and PC linkage (R7) Pointer Variables: Defining &
Runtime Environments Compiler Construction Chapter 7.
ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson.
Big Endian vs. Little Endian Storage of Numeric Data Noah Mendelsohn Tufts University Web:
Number Representation Lecture Topics How are numeric data items actually stored in computer memory? How much space (memory locations) is.
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
CSE 251 Dr. Charles B. Owen Programming in C1 Pointers and Reference parameters.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
Operating Systems A Biswas, Dept. of Information Technology.
7-Nov Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Oct lecture23-24-hll-interrupts 1 High Level Language vs. Assembly.
23/07/2016CSE1303 Part B lecture notes 1 Introduction to computer systems Lecture B01 Lecture notes section B01.
Code Generation Instruction Selection Higher level instruction -> Low level instruction Register Allocation Which register to assign to hold which items?
Lecture 5 Pointers 1. Variable, memory location, address, value
Computers’ Basic Organization
Chapter 14 Functions.
Lecture 7 Macro Review Stack Frames
Stack and Heap Memory Stack resident variables include:
Implementing Subprograms Chapter 10
The Machine Model Memory
UNIT 5 C Pointers.
Functions and Pointers
A bit of C programming Lecture 3 Uli Raich.
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
EPSII 59:006 Spring 2004.
Windows Programming Lecture 07
Introduction to Compilers Tim Teitelbaum
Procedures (Functions)
Procedures (Functions)
In this lecture Global variables Local variables System stack
void Pointers Lesson xx
Pointers.
Functions and Pointers
Chapter 9 :: Subroutines and Control Abstraction
Instructor: Ioannis A. Vetsikas
Introduction to Abstract Data Types
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Pointers, Dynamic Data, and Reference Types
Assembly Language Programming II: C Compiler Calling Sequences
EECE.3170 Microprocessor Systems Design I
Procedure Activations
Multi-modules programming
EECE.3170 Microprocessor Systems Design I
Your questions from last session
Lecture 1. Program Surgery
Comp Org & Assembly Lang
EE 312 Exam I Review.
Runtime Environments What is in the memory?.
Where is all the knowledge we lost with information? T. S. Eliot
Computer Organization and Assembly Language
CSC 497/583 Advanced Topics in Computer Security
Pointer Arithmetic By Anand George.
EE 312 Exam I Review.
Chapter 10 Instruction Sets: Characteristics and Functions
Chapter 10 Def: The subprogram call and return operations of
EE 312 Exam I Review.
Presentation transcript:

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

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

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

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

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

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

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

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

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] 12ff68 b[1] 12ff6c ………. b[6] 12ff7c variable a 2019/1/18

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Example of Optimisation level 2019/1/18

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

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

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

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

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

0x9A0477F3 is represented as a 32-bit word: 1001 1010 0000 0100 0111 0111 1111 0011 In memory is different, say the address of this data is 0x0065fdf4 0x0065fdf4 f3 0x0065fdf5 77 0x0065fdf6 04 0x0065fdf7 9A Memory location in reverse order 2019/1/18

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

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 f3 77 04 9a, interesting 2019/1/18

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

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