Lecture 1. Program Surgery

Slides:



Advertisements
Similar presentations
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Advertisements

Week 3. Assembly Language Programming  Difficult when starting assembly programming  Have to work at low level  Use processor instructions >Requires.
Programming and Data Structure
SPIM and MIPS programming
1 Today’s lecture  Last lecture we started talking about control flow in MIPS (branches)  Finish up control-flow (branches) in MIPS —if/then —loops —case/switch.
IT253: Computer Organization Lecture 6: Assembly Language and MIPS: Programming Tonga Institute of Higher Education.
Chap.2: Instructions: Language of the computer Jen-Chang Liu, Spring 2006 Adapted from
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
Chapter 7 Low-Level Programming Languages. 2 Chapter Goals List the operations that a computer can perform Discuss the relationship between levels of.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 1: Basic Concepts (c) Pearson Education, All rights reserved. You may modify.
Data Transfer & Decisions I (1) Fall 2005 Lecture 3: MIPS Assembly language Decisions I.
Homework Reading –Finish K&R Chapter 1 (if not done yet) –Start K&R Chapter 2 for next time. Programming Assignments –DON’T USE and string library functions,
Topic 8: Data Transfer Instructions CSE 30: Computer Organization and Systems Programming Winter 2010 Prof. Ryan Kastner Dept. of Computer Science and.
Homework Reading Programming Assignments
Assembly Language for x86 Processors 7th Edition
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Spring 2005, Gülcihan Özdemir Dağ Lecture 6, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 6 Outline 6.1Introduction.
C++ Pointers Review. Overview  What is a pointer  Why do I care?  What can be 'pointed to'?  Example.
Represents different voltage levels High: 5 Volts Low: 0 Volts At this raw level a digital computer is instructed to carry out instructions.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Variables and memory addresses
Department of Electronic & Electrical Engineering Types and Memory Addresses Pointers & and * operators.
Assembly Variables: Registers Unlike HLL like C or Java, assembly cannot use variables – Why not? Keep Hardware Simple Assembly Operands are registers.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Windows Programming Lecture 03. Pointers and Arrays.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
F453 Module 8: Low Level Languages 8.1: Use of Computer Architecture.
Bitwise Operations C includes operators that permit working with the bit-level representation of a value. You can: - shift the bits of a value to the left.
Pointers What is the data type of pointer variables?
EGR 2261 Unit 11 Pointers and Dynamic Variables
Computer Organization and Design Pointers, Arrays and Strings in C
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Winter 2009 Tutorial #6 Arrays Part 2, Structures, Debugger
User-Written Functions
Microprocessor Systems Design I
Data Structure and Algorithms
Pointers and Pointer-Based Strings
Prepared By: G.UshaRani B.Pranalini A.S.Lalitha
Passing Arguments to a Function
L7 – Assembler Directives
Memory and Addresses Memory is just a sequence of byte-sized storage devices. The bytes are assigned numeric addresses, starting with zero, just like the.
void Pointers Lesson xx
Variables ,Data Types and Constants
Bases and Representations, Memory, Pointers, Arrays, For-Loops
One-Dimensional Array Introduction Lesson xx
Introduction to Programming
CMSC202 Computer Science II for Majors Lecture 04 – Pointers
CS 240 – Lecture 9 Bit Shift Operations, Assignment Expressions, Modulo Operator, Converting Numeric Types to Strings.
Lecture 18 Arrays and Pointer Arithmetic
C second, objective-c, Go up!
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Assembly Language Review
Lecture 2 SCOPE – Local and Global variables
3.
Homework Starting K&R Chapter 5 Good tutorial on pointers
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Homework Reading Programming Assignments Finish K&R Chapter 1
Overloading functions
Pointers and Pointer-Based Strings
C Programming Pointers
Course Overview PART I: overview material PART II: inside a compiler
January 16 The books are here. Assignment 1 now due Thursday 18 Jan.
Assignment due Write a program the generates a random integer expression, presents the two operands and the result to the user, and asks the user to tell.
Review of Java Fundamentals
Hello World Program In Visual Studio and Debugging
Introduction to Pointers
Presentation transcript:

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

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

Short Type - data and address Short - occupy 2 bytes 12 1234 Here, a memory location of 1234 contains a value of 12. 1234 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, 1234 + 2 = 1236 2019/2/17

Integer Type - data and address Integer - occupy 4 bytes 12 1234 Here, a memory location of 1234 contains a value of 12. 1234 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, 1234 + 4 = 1238 2019/2/17

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

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

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

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: 110001110100010111111010000000000000000000000000000000001110101100001001100010110100010111111100100000111100000000000001100010010100010111111100100000110111110111111100 000001000111110100010010100010110100110111111100100010110101010111111000000000110001010010001101110110000010010101000010000000001000100101010101111110001110101111011111 It is difficult to understand 2019/2/17

the location of the variable array is given by the number 110110000010010101000010, which we now highlight in the original machine code: 11000111010001011111101000000000000000000000000000000000 11101011000010011000101101000101111111001000001111000000 00000001100010010100010111111100100000110111110111111100 00000100011111010001001010001011010011011111110010001011 01010101111110000000001100010100100011011101100000100101 01000010000000001000100101010101111110001110101111011111 2019/2/17

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

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

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

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

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

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

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

Example 1 2019/2/17

& 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

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

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

Same result, different expression Array and Pointer * char a[11]=“0123456789”; //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 -> 1 // (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

Example –look at the result 2019/2/17

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

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

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

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

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

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

Summary : Integer Integer : 4 bytes such as int a = 3; In hex 0x00000003 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

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 1010 1010 2019/2/17

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