class08 - Representing and Manipulating Information...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Representing and Manipulating Information February 25, 2010 HW 5 is due 3/2/2010 Project One is due on 3/11/2010: programming must work in groups of two Read Chapter 3 CS 47 Natasha Khuri Makefiles In its simplest form, a Makefile contains rules in the following form: targetList: targetList: dependencyList commandList main1: main1.o reverse.o gcc main1.o reverse.o -o main1 main1.o: main1.c reverse.h gcc -c main1.c reverse.o: reverse.o: reverse.c reverse.h gcc -c reverse.c CS 47 Natasha Khuri Make Rules The order of make rules is important. The make utility creates a "tree" of interdependencies by initially examining the first rule: target file in the first rule is a root node of a dependency tree each file in its dependency list is added as a leaf of each root node. The make utility then visits each rule associated with each file in the dependency list and performs the similar actions. CS 47 Natasha Khuri Make Rules main1: main1.o reverse.o gcc main1.o reverse.o -o main1 main1.o: main1.c reverse.h gcc -c main1.c reverse.o: reverse.o: reverse.c reverse.h gcc -c reverse.c main1 main1.o reverse.o main1.c CS 47 Natasha Khuri reverse.h reverse.c reverse.h Running make Once the tree has been constructed, the make utility works up the tree from the bottom leaf nodes to the root node, looking to see if the last modification time of each child node is more recent than the last modification time of its immediate parent node: if it is: the rule of the parent is executed if a file is not present, the rule of the parent is executed CS 47 Natasha Khuri Running Make main1 7 main1.o 5 main1.c 1 reverse.h 2 reverse.o 6 reverse.c 3 reverse.h 4 CS 47 Natasha Khuri Word Size Every computer has a word size that indicates the minimal size of integer (int and pointer (char * int) int char *, int *, etc) data types Virtual addresses are encoded by such words. Maximum size of virtual memory is determined by the machine word size: 32-bit word size gives as 232 (4 GB) of virtual address space 64-bit word size gives as 264 bytes of virtual memory Machines support multiple data types Fractions or multiples of word size Always integral number of bytes CS 47 Natasha Khuri Data Types and Sizes C char short int int long int long long int float double CS 47 Natasha Khuri Intel-32 1 byte 2 bytes 4 bytes 4 bytes 8 bytes 4 bytes 8 bytes Java byte char short int int long int float double Any platform 1 byte 2 bytes 2 bytes 4 bytes 8 bytes 4 bytes 8 bytes C Data Sizes In C/C++ use sizeof() to find the size of the data sizeof() type (in bytes): sizeof(int) sizeof(int) sizeof(char *) sizeof(double *) sizeof(array) sizeof(array) sizeof(struct) sizeof(struct) sizeof(union) sizeof(union) The sizeof of any pointer will return machine's word size (same as the size of int data type) CS 47 Natasha Khuri Memory Organization A machine-level program views memory as a very large array of bytes, called virtual memory: conceptually very large array of bytes actually implemented with hierarchy of different memory types: SDRAM, DRAM, disk every byte of memory is identified by a unique number, known as its address the set of all possible addresses is known as the virtual address space. CS 47 Natasha Khuri Memory Organization 32-bit 64-bit Words Words Addr = 0000 ?? Addr = 0000 ?? Addr = 0004 ?? Bytes Addr. 0000 0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011 0012 0013 0014 0015 Addresses specify byte locations address of first byte in word addresses of successive words differ by 4 (32-bit) or 8 (64-bit) Addr = 0008 ?? Addr = 0008 ?? Addr = 0012 ?? CS 47 Natasha Khuri Byte Ordering Example Big Endian Least significant byte has highest address Little Endian Least significant byte has lowest address Example Variable x has 4-byte representation 0x01234567 Address given by &x is 0x100 Big Endian 0x100 0x101 0x102 0x103 01 Little Endian 23 45 67 0x100 0x101 0x102 0x103 67 CS 47 Natasha Khuri 45 23 01 Data Representations Code to print byte representation of data Casting pointer to unsigned char * creates a byte array typedef unsigned char *pointer; void show_bytes(pointer start, int len) { int i; for (i = 0; i < len; i++) printf("0x%p\t0x%.2x\n", start+i, start[i]); printf("\n"); } Printf directives: %p: Print pointer %x: Print Hexadecimal CS 47 Natasha Khuri Execution Example int a = 15213; printf("int a = 15213;\n"); show_bytes((pointer) &a, sizeof(int)); Result (Linux): int a = 15213; 0x11ffffcb8 0x11ffffcb9 0x11ffffcba 0x11ffffcbb CS 47 Natasha Khuri Decimal: 15213 Binary: 0011 1011 0110 1101 3 B 6 D 0x6d 0x3b 0x00 0x00 Hex: Today Bitwise shift operators Sign extension Overflow CS 47 Natasha Khuri C/Java Bitwise Operations A special set of operations, the bit operations can be used on the integer types in C and Java: long, int, int, short, char The bit operations are: ~expression expression1 expression1 expression1 expression1 expression1 CS 47 Natasha Khuri >> expression2 << expression2 & expression2 ^ expression2 | expression2 bitwise NOT shift right shift left bitwise AND bitwise XOR bitwise OR P r e c e d e n c e Left Shift Operations x << y Shift bit-vector x left y positions: throw away extra bits on left, fill with 0's on right. x Bit BasePattern 10 00000001 1 00000010 x << 1 00000010 2 00000100 x << 2 00000100 4 00001000 x << 3 00001000 8 00100000 x << 5 00100000 32 Each left shift by one is equivalent to multiplying by two. Undefined behavior when shifting by <0 or word size CS 47 Natasha Khuri Right Shifts x >> y Shift bit-vector x right y positions: throw away extra bits on right. what happens on the left, depends on the integer type (signed signed or unsigned unsigned). x Bit BasePattern 10 00100000 32 x >> 1 00010000 16 00001000 x >> 2 00001000 8 00000100 x >> 3 00000100 4 00000001 x >> 5 00000001 1 Undefined behavior when shifting by <0 or word size CS 47 Natasha Khuri Logical and Arithmetic Shifts Generally, machines support two forms of right shift: logical and arithmetic. a logical shift by k fills the left end with k zeroes: given x = 101000102, x >> 2, 001010002: each logical right shift by one is equivalent to (integer) division by two. a logical shift is implemented for unsigned integer types. In Java, this shift is >>> an arithmetic right shift fills the left end with k repetitions of the most significant bit: given x = 101000102, x >> 2, 11101000 2 for signed types, arithmetic shift is default CS 47 Natasha Khuri Exercise x (8 bits) Hex x << 3 Hex x >> 2 x >> 2 (logical) (arithmetic) Binary Hex Binary Hex Binary Binary 0xC3 0x75 0x87 0x66 CS 47 Natasha Khuri From Exam 1 Perform UNSIGNED binary addition: 0x503C + 0x8 = 0x502C - 0x30 = 0x50DA - 0x502C = Perform SIGNED binary addition: 0101 1001 1100 1001 + + + + 0100 0101 0100 1010 = = = = CS 47 Natasha Khuri Detecting Overflow Signed overflow in addition: overflow can only occur if the two numbers are both positive or both negative. addition of two positive numbers overflows if: a carry into the sign bit of the result occurs, but carry out of the MSB does not occur. addition of two negative numbers overflows if: carry into the MSB does not occur carry out of the MSB does occur CS 47 Natasha Khuri Detecting Overflow Unsigned overflow in addition: overflow can only occur if there is a carry out of MSB overflow can occur in subtraction when the bottom number is bigger than the top number. Most computers have either special 1-bit storage areas called flags or special circuits to detect overflow. May applications ignore overflow. CS 47 Natasha Khuri ...
View Full Document

Ask a homework question - tutors are online