Objectives. External declarations Pointers Dynamic data structures using pointers Function pointers

Similar documents
Objectives. Introduction to process management of an operating system Explain step-by-step how processes are created in TOS

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

PRINCIPLES OF OPERATING SYSTEMS

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University

Objectives. Explain non-preemptive scheduling Explain step-by-step how a context switch works in TOS

Final CSE 131B Spring 2004

QUIZ. What are 3 differences between C and C++ const variables?

A Fast Review of C Essentials Part I

CIS 2107 Computer Systems and Low-Level Programming Fall 2011 Midterm

CS C Primer. Tyler Szepesi. January 16, 2013

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

High Performance Computing and Programming, Lecture 3

Lectures 13 & 14. memory management

Dynamic memory allocation (malloc)

Functions. Arash Rafiey. September 26, 2017

So far, system calls have had easy syntax. Integer, character string, and structure arguments.

Lecture 3: C Programm

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Quick review pointer basics (KR ch )

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

Dynamic Data Structures. CSCI 112: Programming in C

CIS 2107 Computer Systems and Low-Level Programming Fall 2011 Midterm Solutions

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

C++ Programming for Non-C Programmers. Supplement

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

QUIZ. Source:

QUIZ. What is wrong with this code that uses default arguments?

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

CS349/SE382 A1 C Programming Tutorial

CS61, Fall 2012 Section 2 Notes

G52CPP C++ Programming Lecture 12

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

Lecture 8 Dynamic Memory Allocation

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

CSC 1600 Memory Layout for Unix Processes"

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Programming in C - Part 2

CS241 Computer Organization Spring Data Alignment

Programming in C. Pointers and Arrays

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

Dynamic Memory Allocation and Command-line Arguments

Reference slides! Garcia, Fall 2011 UCB! CS61C L04 Introduction to C (pt 2) (1)!

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

We do not teach programming

EL2310 Scientific Programming

Kurt Schmidt. October 30, 2018

Please refer to the turn-in procedure document on the website for instructions on the turn-in procedure.

Machine-Level Programming V: Unions and Memory layout

Binghamton University. CS-120 Summer Introduction to C. Text: Introduction to Computer Systems : Chapters 11, 12, 14, 13

Pointers (1A) Young Won Lim 11/1/17

TELE402. Internetworking. TELE402 Lecture 1 Protocol Layering 1

Structured Programming. Functions and Structured Programming. Functions. Variables

Class Information ANNOUCEMENTS

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

just a ((somewhat) safer) dialect.

C for Java Programmers 1. Last Week. Overview of the differences between C and Java. The C language (keywords, types, functies, etc.

Problem Set 1: Unix Commands 1

A Fast Review of C Essentials Part II

CSE2421 Systems1 Introduction to Low-Level Programming and Computer Organization

C Programming Basics II

Hakim Weatherspoon CS 3410 Computer Science Cornell University

Programs in memory. The layout of memory is roughly:

Lectures 5-6: Introduction to C

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

General Syntax. Operators. Variables. Arithmetic. Comparison. Assignment. Boolean. Types. Syntax int i; float j = 1.35; int k = (int) j;

Objectives. Learn how to read and write from memory using C-pointers Implement the very first TOS functions

Unit 7. Functions. Need of User Defined Functions

CSC 2400: Computer Systems. Using the Stack for Function Calls

Arrays and Pointers (part 1)

Programming in C++: Assignment Week 2

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Vector and Free Store (Vectors and Arrays)

Final CSE 131B Winter 2003

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

See P&H 2.8 and 2.12, and A.5-6. Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University

Reference slides! C Strings! A string in C is just an array of characters.!!!char string[] = "abc";! How do you tell how long a string is?!

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Personal SE. Arrays Pointers Strings

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;

BLM2031 Structured Programming. Zeyneb KURT

A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH 'C' LANGUAGE

ECE 435 Network Engineering Lecture 2

CIS 2107 Computer Systems and Low-Level Programming Spring 2012 Final

Number Review. Lecture #3 More C intro, C Strings, Arrays, & Malloc Variables. Clarification about counting down

ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

The output will be: marks all or nothing. 1 #include <stdio.h> 2 main() { 3 int i; int j; 4 int *p; int *q; 6 p = &i; 7 q = &j; 8 i = 1;

Exercise Session 2 Simon Gerber

COMP-520 GoLite Tutorial

Applications of Pointers (1A) Young Won Lim 12/26/17

G52CPP C++ Programming Lecture 3. Dr Jason Atkin

Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory

Transcription:

TOS Arno Puder 1

Objectives Basic knowledge of C and C++ should have been taught in an introductory class on programming In the following we focus on some finer points of C: External declarations Pointers Dynamic data structures using pointers Function pointers 2

Extern Declarations my_incl.h extern int my_var; prog1.c #include my_incl.h void f() my_var = 1; prog2.c #include my_incl.h int my_var; void g() my_var = 0; 3

External Declarations my_var is an external identifier It is introduced to the compiler by extern int my_var; The compiler does not yet allocate memory for my_var, but programs can start using it (e.g. in prog1.c) Variable must be declared exactly once in one of the translation units (e.g. prog2.c). With the declaration the compiler allocates memory The linker resolves all external references (e.g. my_var in prog1.c refers to the declaration in prog2.c) Note that there is a difference in C and C++: in C it is permissible to have a global symbol declared more than once. The linker will not complain about this. 4

Memory allocation int x; int y; void main() x = 1; y = 2; 0 for each variable declaration, the compiler allocates memory Variable x is stored at address 1030 Variable y is stored at address 2050 The address operator & determines the address. E.g. &x is 1030 1030 2050 1 2 1 MB Machine instructions for main() x y 5

Pointers int x; int *p; void main() x = 1; p = &x; 0 1030 3100 1 1030 p is a pointer to an integer p as a global variable is also allocated memory by the compiler (e.g. &p is 3100) p = &x; means that p is assigned the address of x Content of memory location 1030 is 1 Content of memory location 3100 is 1030 1 MB x p 6

/* unix */ void main() char ch = 'A'; char *p = &ch; char **q = &p; printf("%c\n", ch); printf("%p\n", p); printf("%p\n", q); printf("%c\n", *p); printf("%p\n", *q); printf("%c\n", **q); Pointers Output A 530 1200 A 530 A 0 530 1200 2500 1 MB A 530 1200 ch p q 7

Casting During an assignment x=y; the type of x has to be compatible with the type of y If this is not the case, the C-compiler will issue an error. E.g. char* c; c = 1000; will result in a compile time error, because the type of c is char* and the type of 1000 is int. When you know what you do, you can short-circuit type checking. This is called a cast. E.g. char* c; c = (char*) 1000; This tells the C-compiler to assume that the type of 1000 is char* 8

Using Pointers void kernel_main() char* screen_base = (char *) 0xB8000; *screen_base = A ; We can use pointers to write to memory-mapped I/O devices. The program above will write an A to the top left corner of the screen. 0xB8000 is the address of the top-left corner of the video screen. More on this later. Note that this program will very likely not work under Unix or Windows because of memory protection (I.e., you cannot just write to address 0xB8000). 9

Dynamic Data Structures Dynamic data are used when the number of data items are not known a priori Program usually uses malloc()/free() in C and new/delete in C++ to create sufficient number of data items at runtime As will be discussed later, in OS hacking we don t have those functions!!! We have to solve the problem by doing a pseudo dynamic memory management Solution #1: do an array where each element has a used flag. If a new element is needed, look for one where used==false Solution #2: use a single-linked list (see next slides) 10

Part 1: Data Structures typedef struct _Node int x; struct _Node* next_free; Node; Node l[30]; Node* first_free_node; Compiler allocates array with 30 data items (this is how we get around using malloc()) I.e., there can be a maximum of 30 data items at any given time Each data item contains a pointer to the next available data item 11

Part 2: Initialization void init () int i; /* Create single linked list of the first _29_ elements */ for (i = 0; i < 29; i++) l[i].next_free = &l[i+1]; /* 0 terminates the list at the last element */ l[29].next_free = (Node *) 0; /* Initialize the pointer to the first free node */ first_free_node = &l[0]; l[0] l[1] l[2] l[29] first_free_node Before the data structure can be used, it needs to be initialized once Function init() creates a single linked list of the first 29 elements of the list. Global variable first_free_node is initialized to point to the first array element Only available (I.e., free) elements are on the linked list. 12

Part 3: Allocating a Data Item Node* alloc_data_item () Node* tmp; tmp = first_free_node; first_free_node = tmp->next_free; return tmp; Function alloc_data_item() returns a free data item from the list of available data items The next free data item is determined via global variable first_free_node I.e., the next free element is taken from the head of the single linked list If no more data items are available, the function returns 0 The caller is responsible to check that the value returned is not 0 13

Part 4: Deleting a Data Item void delete_data_item (Node* elem) elem->next_free = first_free_node; first_free_node = elem; A data item is deleted by returning it to the free list This is done by adding the data item to be deleted to the beginning of the single linked list 14

Example l[0] l[1] l[2] first_free_node Initial setup: all elements are on free list. 15

Example item1 l[0] l[1] l[2] first_free_node Step 1: request a data item. 16

Example item1 item2 l[0] l[1] l[2] first_free_node Step 2: request another data item. 17

Example item2 l[0] l[1] l[2] first_free_node Step 3: release item1. 18

Example l[0] l[1] l[2] first_free_node Step 4: release item2. 19

Pointers to Functions void process_a (int x) printf ( Process a got %d\n, x); void process_b (int x) printf ( Process b got %d\n, x); Produces output: Process a got 10 Process b got 20 void call (void (*func) (int), int arg) (*func) (arg); void main() call (process_a, 10); call (process_b, 20); 0 process_a process_a process_b Program code. Data segment 20 1 MB

Pointer to Functions void call (void (*func)(int), int arg) - func and arg are formal parameters. - void (*func)(int) is the first argument - int arg is the second argument The first argument to call expects a pointer to a function with return type void and one input parameter of type integer. The pointer to a function is actually the address of the first machine instruction that belong to the implementation of the function. call (process_a, 10) passes two actual parameters: - pointer (i.e. address) of function process_a (note: process_a is not called at this time) - 10 (*func )(arg) actually calls the function pointed to by func. The function to be called is passed the actual parameter arg. 21

Pointers revisited void say_hello() printf( Hello!\n ); Output: Hello! void main() char *p = (char *) say_hello; say_hello(); *p = 0xc3; say_hello(); 0xc3 is the Intel x86 machine instruction for RET (Return from Subroutine) Overwriting the first instruction of say_hello() with RET will cause the function to exit immediately This program will cause a security violation under Unix, but it would work under TOS. 22

Assignment 1 Detailed explanation on course web site Get TOS source code Implement the functions located in tos/kernel/stdlib.c: k_strlen() k_memcpy() k_memcmp() Run test cases by calling make target make host-tests Not too much coding -- get familiar with TOS source code, review pointers Hint: the above functions should behave identical to the standard C- library functions strlen(), memcpy(), and memcmp(). Use the Unix man-pages to understand their behavior. 23