Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 1. Program Surgery

Similar presentations


Presentation on theme: "Lecture 1. Program Surgery"— Presentation transcript:

1 Lecture 1. Program Surgery
Wonder of Execution The Visual C++ Debugger Data type, integer, short Addresses, content, pointer 2019/2/17

2 Program Surgery * Means how to debug a program:
we need to master the Visual C++ debugger, and understand how data (one byte, two bytes..) is stored in the memory, the address to store the data the characteristics of function call (call a function) and the program code. In USA, a programmer does two things – make the program faster and debug it. 2019/2/17

3 Short Type - data and address
Short - occupy 2 bytes 12 1234 Here, a memory location of 1234 contains a value of is address and 12 is the content. 13 Memory address 1236 14 1238 As it occupies two bytes, the address is incremented by 2. Say 1234, = 1236 2019/2/17

4 Integer Type - data and address
Integer - occupy 4 bytes 12 1234 Here, a memory location of 1234 contains a value of is address and 12 is the content. 13 Memory address 1238 14 1242 As it occupies four bytes, the address is incremented by 4. Say 1234, = 1238 2019/2/17

5 Difficult to add two numbers in Computer
To add two numbers such as a and b from human viewpoints is very simple. It is simply to add them. However, from computer viewpoint, it is quite complicated involving a lot of steps and intermediate utility. 2019/2/17

6 Explanation No need to memorise
c = a + b in a programming language, the steps for a computer Look in an "index" to see in which "sheet" it has jotted down the current value of a. Look in the "index" to see in which "sheet" it has jotted down the current value of b. Copy the two values to a working pad, one on top of the other. Add the two numbers, digit by digit, paying attention to carries. Look in the "index" to see in which "sheet" it keeps the current value of c Copy the result to the sheet where it keeps the value of c. 2019/2/17

7 An "abstraction” No need to memorise It is a notion that describes multiple instances of objects but that is clear that the instances For example, driving a car, you don’t need to know how combustion engine works, but you know how to control. For example, c = a + b, you don’t need to tell when to get a, b and add together to produce c, (internally, CPU controls the electronic gate to do c = a + b, step by step) 2019/2/17

8 Compilers and Debuggers
For the following simple loop in C++ for (i = 0; i < 4; i++) { sum += array[i]; } The Visual C++ compiler will translate as follows: It is difficult to understand 2019/2/17

9 the location of the variable array
is given by the number , which we now highlight in the original machine code: 2019/2/17

10 Debugger – a tool to determine the contents of variables
A tool that allows us to stop the program and inspect its variables in the middle of its execution is called a debugger. It helps us (user) determine anything goes wrong. 2019/2/17

11 Steps to debug a simple program
Look at the memory location 2019/2/17

12 Visual Environment Visual C++ is a very large utility and has many components that we will not be using in this course Demonstration 2019/2/17

13 The value i will be incremented by one
Visual C++ debugger Press F10 will move by one Demonstration: how to set break point F9, F10, and dump memory data The value i will be incremented by one 2019/2/17

14 Variable and Addresses
Global (access by all sub-routines), local (only to its routine) int c  This is a variable occupying 4 bytes char c  This is a variable occupying 1 byte, the remaining three are 0xcccccc &c, the address of variable c (usually 4 bytes (unsigned )) that contains name of variable 2019/2/17

15 Memory location, address
Note that in Visual, the above occupy 4 bytes, it means that some are not used. 2019/2/17

16 int var (var is a variable and occupies 4 bytes )
Pointer int var (var is a variable and occupies 4 bytes ) int *var (*var is a pointer that points to an integer) 2019/2/17

17 Example 1 2019/2/17

18 & means address of varpt
Example 2 Pointer to an integer int var = 108; //define var as an integer and assign a value of 108 int *varpt; //define varpt as a pointer varpt = &var; //content of varpt is address of var & means address of varpt 2019/2/17

19 Explanation of variable and address
address 0x0012ff78, value = 0x0012ff7c address 0x0012ff7c, value = 108, 0x c looking from this direction 2019/2/17

20 Example 3 Point to location 0x0012FF7C Value is 0x61 = ‘a’ 2019/2/17

21 Same result, different expression
Array and Pointer * char a[11]=“ ”; //reserve a[11] as ‘\0’ a[0] =“1”; a[1] =“2”; char *ptr; //point to a single byte, not an integer (4 bytes) ptr = &a; (or ptr = a in C) //now the content of ptr is the address of variable a *ptr -> // (a[0]) *(ptr + 1) ->2 // (a[1]) array[i] * (array + i) &array[i] array + i array[i + j] * (array + i + j) &array[i + j] array + i + j Same result, different expression 2019/2/17

22 Example –look at the result
2019/2/17

23 Example – look at the ptr++
The pointer will be incremented by one, together with + i, it increments by 2 Result 2019/2/17

24 Multi-Dimensional Arrays – pointer of pointer
array2[6][2] or *(*(array2 + 6) +2) 5 6 1 2 3 4 1 2 2019/2/17

25 Example int a[2][3] = {0, 1, 2, 3, 4, 5}; a[0][0] = “0”; a[0][1] = “1”
2019/2/17

26 Example (1), note the value
2019/2/17

27 Example (2), the value of a[0][2]
2019/2/17

28 Naughty Pointers The value pointed by pointer is modified. (for example, you write a program that uses pointer to modify the content of your program, it will destroy the program and is not recommended.) However, if you can master pointer, you can write a very elegant program. (writing pointer is the essential skill) 2019/2/17

29 Summary : Integer Integer : 4 bytes such as int a = 3;
In hex 0x Integer : 4 bytes such as int a = 3; The memory location (address of a is 0x0065FDF4) 0x0065FDF4:03 0x0065FDF5:00 0x0065FDF6:00 0x0065FDF7:00 0x0065FDF4 0x0065FDF7 2019/2/17

30 Summary: Short Short: two bytes Short a = 3; 0x0065FDF4: 03
Not used Short: two bytes Short a = 3; 0x0065FDF4: 03 0x0065FDF3: 00 Short b = 4; 0x0065FDF0: 04 0x0065FDF1: 00 As short uses 2 bytes, remaining two bytes in memory 0x0065FDF2 (0xCC) and 0x0065FDF3 (0xCC) are not used, 0xCC means 2019/2/17

31 Summary: Pointer address of a (means &a) is 0x0065FDF4, content is 0x03 address of pointer is 0x0065FDF0, content 0x0065FDF4 (you have to look at from right hand), address of variable a 2019/2/17


Download ppt "Lecture 1. Program Surgery"

Similar presentations


Ads by Google