Wednesday, March 29, Implementation of sets in an efficient manner illustrates some bit-manipulation ideas.
|
|
- Sabina Shields
- 5 years ago
- Views:
Transcription
1 Wednesday, March 29, 2017 Topics for today Sets: representation and manipulation using bits Dynamic memory allocation Addressing mode summary Sets Implementation of sets in an efficient manner illustrates some bit-manipulation ideas. Few languages have sets as built-in types. SETL is based on sets and Pascal has sets but with limitations. In a language having the set type we would expect to be able to have things like var A, B, C : set of integer; A = [ 2, -5, 7, 29]; B = [ 1, 5, 29, 17, 30]; C = A union B; C = A intersect B; if ismember(c,32)... A = A + 14; /* insert 14 to set A */ B = B - 5; /* remove 5 from set B */ In Standard Pascal, the base type could have no more than 64(?) possible members. One way of implementing sets is with linked lists. Items in the lists are typically stored in ascending (or descending) order so that union and intersection operator can be implemented efficiently. However, if we know the range of the base type and the members are enumerable, we can use bit vectors (arrays of bits) In a bit vector A, bit I is 1 if the Ith possible member is in the set and bit I is 0 if the possible member is not in the set. This method of representing a set does not support multisets where a member may appear more than once. Operations such as union and intersection are now extremely fast. In what follows we will assume that our sets are sets of integers. Q: How many bytes to do we need if there are N possible set members? A: ceiling(n/8) Comp 162 Notes Page 1 of 16 March 29, 2017
2 Q: How do we know which byte contains the bit that represents the membership of integer k. A: Numbering bytes from 0 upwards, assuming 8 bits per byte then the bit is in byte k/8. Q: Which bit in this byte represents membership of k? A: Assuming bits are numbered 0 through 7 then the bit is number k%8. Example Assume underlying base type is We need 5 bytes (ceiling(38/8) = 5). We can represent the set [ ] as The leftmost bit represents membership of 0, the bit next to it represents the membership of 1 and so on. The locations of the 5 bits set to 1 correspond to the 5 members of set [ ]. Bit vectors in Pep/9 We look at assembly code fragments to * test bit i (membership test) * set bit i (adding to set) * clear bit i (removing from set) Our code uses two arrays of 16-bit bit masks (bit patterns); these enable us to isolate individual bits within a byte Each entry in the first table (bits1) is all zeros except for a single one. Each entry in the second table (bits2) is all ones except for a single zero. We use full words because the Pep/9 AND and OR instructions operate on words. Here are the tables. Comp 162 Notes Page 2 of 16 March 29, 2017
3 bits1:.word 0x0080 ; word 0x0040 ; word 0x0020 ; word 0x0010 ; etc.word 0x0008.word 0x0004.word 0x0002.word 0x0001 bits2:.word 0xff7f ; word 0xffbf ; word 0xffdf ; word 0xffef ; etc.word 0xfff7.word 0xfffb.word 0xfffd.word 0xfffe The following code is common to all three operations (test bit i, set bit i and clear bit i) ; assume S is the array of bytes ; get appropriate byte ldwx i,d ; bit number ; i/8 is byte number ldba S,x ; get byte containing bit i ldwx i,d ; bit number again andx 7,i ; bit number within the byte i.e. i%8 aslx ; turn into a byte offset to index bit mask array Having got the byte of interest into register A, here are the code sequences for the three operations Test bit i anda bits1,x breq... ; branch if bit is zero Set bit i ora bits1,x Clear bit i anda bits2,x Both the Set and Clear operations need to be followed by code that updates S as follows Comp 162 Notes Page 3 of 16 March 29, 2017
4 ldwx i,d stba S,x ; bit number ; the byte number ; put the modified byte back into S Applications of bit maps Indexes Bit maps or bit vectors are commonly used in indexes of various kinds. For example A bit map might be used by an operating system as a record of the free space on a disc. Bit I indicates if disc block I is free or in use. Suppose space is allocated by the operating system in 4Kbyte sections. The ratio between disc and map is to 1 so on a 1Tbytes disc, about 256 Mbytes is occupied by the map In a document retrieval system bit I might indicate if document I contains the keyword of interest. Queries such as Find bank and (finance or account) and not river Can be implemented as set operations. Consider the following code ldwx 2000,I top: ldwa River,x nota stwa -2,s ldwa Finance,x ora Account,x anda -2,s anda Bank,x stwa result,x subx 2,i brge top ; or whatever the size is In a lottery, a lottery ticket can be implemented as a bit map. The winning numbers can also be implemented in this way then matched quickly with the tickets. Comp 162 Notes Page 4 of 16 March 29, 2017
5 Prime numbers We can use a bitmap to represent the set of prime numbers. If we assume that no even number is prime then our bitlist represents the prime-ness of the odd numbers 3, 5, 7,,32767 Such a bit list would occupy 2048 bytes, a relatively small amount of memory. The first few bits are shown below together with the numbers they represent Our code below, lets the user enter a sequence of numbers terminated by zero. The program reports, for each number, whether it is prime. The bitmap primemap is omitted for space reasons. It differs from the earlier examples in that the relevant parts of the bit masks are 16 bits long rather than 8 bits. ; prime number tester top: deci N,d breq done ; zero terminates program brv badin ; warn if number out of range brlt top ; negative numbers ignored ldwa N,d cpwa 1,i breq top ; 1 is ignored cpwa 2,i breq prime ; 2 is prime anda 1,i breq notprime ; even number is not prime ldwa N,d suba 3,i asra stwa K,d ; K=(N-3)/2 = bit position to test ldwx K,d ; K/16 is number of word containing bit of interest aslx ; byte offset of this word ldwa primemap,x ; get the word of interest ldwx K,d ; bit number again andx 15,i ; bit within word aslx ; byte offset for bitmask table anda bitmask,x ; zero out all other bits breq notprime ; if bit of interest is zero, N is not prime prime: stro isprime,d br top notprime: stro noprime,d br top badin: stro badinput,d br top done: stop Comp 162 Notes Page 5 of 16 March 29, 2017
6 N:.block 2 K:.block 2 badinput:.ascii "overflow on input\n\x00" noprime:.ascii "is not prime\n\x00" isprime:.ascii "is prime\n\x00"; bitmask:.word 0x8000.word 0x4000.word 0x2000.word 0x1000.word 0x0800.word 0x0400.word 0x0200.word 0x0100.word 0x0080.word 0x0040.word 0x0020.word 0x0010.word 0x0008.word 0x0004.word 0x0002.word 0x0001 Dynamic memory allocation (section 6.5) In C there are three general classes of variable: Globals, Locals and Heap. In many high-level languages there is a mechanism to request/allocate space from a memory pool dynamically as the program runs. The pool is often referred to as the heap. The space can be returned to the pool in an order different from its allocation. This is what makes the heap different from and separate from the stack. In C space no longer needed has to be returned explicitly to the heap, in Java a garbage collector finds space that can no longer be accessed and can thus be reused In the following.the function malloc (found in stdlib.h) returns the address of the space requested or NULL is the request cannot be satisfied. int *P,*Q; char *str; P = malloc (sizeof(int)); // sizeof returns the size in bytes Q = malloc (4 * sizeof(int)); // array of 4 int str = malloc (15); We may not know how big our table needs to be until run-time. int *Table; int N; scanf( %d,&n); // size of table Table = malloc (N*sizeof(int)); Comp 162 Notes Page 6 of 16 March 29, 2017
7 In C, we can free up the space after we have finished with it enabling it to be reused free(q); free(table); In Pep/9 we can mimic the allocation of space (using Warford s malloc subroutine). However, there is no easy way to duplicate the action of free to return space. Space allocation (the malloc subroutine) We need a mechanism to get the address of a block of bytes of a certain size. Warford's solution is to use as the heap the space immediately following the program in memory.... stop hpptr:.addrss heap heap:.block 1.end Visually Program hpptr: heap: Heap space allocated stack Comp 162 Notes Page 7 of 16 March 29, 2017
8 We use the malloc subroutine (without a parameter) to implement the C malloc mechanism: malloc: ldwx hpptr,d adda hpptr,d stwa hpptr,d ret ; Get current heap pointer; this is the address result ; Adding the space requested and the old hpptr ; gives us the new value of the hpptr Pre-conditions: Register A contains size of request in bytes Variable hpptr ( heap pointer ) points to the next free space. Post-conditions: Register X contains a pointer to the bytes allocated. Variable hpptr is advanced over the allocated space. Note that there is no check to see if we have tried to allocate space in use on the stack. In theory this is possible to add such a check because we can copy the SP into Register A and compare it with hpptr. In practice it is difficult because addresses are unsigned numbers. If we did determine that there was not enough free space above the stack we could return 0 in Register X like the C malloc returning NULL. Example trace The following trace of a sequence of high-level language declarations and statements and their Pep/9 equivalents. This example also introduces the 8 th and last addressing mode. Comp 162 Notes Page 8 of 16 March 29, 2017
9 High-level Language Pep/9 e.g. hpptr Register A Register X P Q int *P,*Q; P:.block 2 Q:.block P = malloc(8); ldwa 8,i call malloc stwx P,d Q = malloc(2); ldwa 2,i call malloc stwx Q,d *Q = 99; ldwa 99,i stwa Q,n 99 P[i] = 45; ldwa 45,i ldwx i,d aslx addx P,d stwa 0,x 45 i 2*i P + 2*i The last addressing mode in Pep/9 is indirect addressing. The designation means that the operand is Memory[Memory[operand ]],n Suppose variable Q is at address 300 and it contains the value 430. Suppose further that at location 430 is the word containing 47 Then Q,i is 300 Q,d is 430 Q,n is 47 Local pointers What if pointer variables are local? It turns out we just use an addressing mode we have seen before when the stack contains pointers. Consider the following example. Comp 162 Notes Page 9 of 16 March 29, 2017
10 int main() { int *A, *B, *C; A = malloc(4*sizeof(int)); B = malloc(sizeof(int)); C = malloc(sizeof(int)); } A[2] = 40; *C = 30; *B = 2 + *C; Assume that the stack is C B A main: subsp 6,i ; space for locals ; assignment to A ldwa 8,i ; size of request call malloc stwx 4,s ; address of array to A ; assignment to B ldwa 2,i call malloc stwx 2,s ; address of new int to B ; assignment to C ldwa 2,i call malloc stwx 0,s ; address of new int to C At this point we have Comp 162 Notes Page 10 of 16 March 29, 2017
11 C B A The assignments use modes we saw earlier ; assignment to A[2] ldwx 2,i ; index for array access aslx ; make it a byte offset ldwa 40,i ; the value to assign stwa 4,sfx ; put in element 2 of array pointed to from 4,s ; assignment to *C ldwa 30,i stwa 0,sf ; put 30 in element pointed to by 0,s ; assignment to *B ldwa 0,sf adda 2,i stwa 2,sf ; redundant statement because of previous instruction ; update location pointed to by B Summary of addressing modes We have now seen all eight of the Pep/9 addressing modes. The following page is an attempt to illustrate all of them on a single page by grouping them into three groups. Comp 162 Notes Page 11 of 16 March 29, 2017
12 Group 1 (i,d,n) These are basic modes Immediate, Direct and Indirect. Consider the following example: br main A:.addrss B B:.word 99 (3)A main: deco A,i deco A,d deco A,n (5)B stop.end 5 99 Output is : 3 value of A 5 contents of address A 99 contents of address that A points to Group 2 (s,sf,sx,sfx) These are the modes that involve the stack 0 s for simple items on the stack 2 sf for simple items pointed to from the stack 4 sx for arrays on the stack sfx for arrays pointed to from the stack 6 8 Following code prints from memory shown 10 deco 4,s ; 1 12 deco 0,sf ; 2 14 ldwx 4,i ; byte offset of 3 deco 6,sfx ; ldwx 6,i ; index of 4 in array in stack deco 10,sx ; Assume this is array Group 3 (x) Indexing: Base + register X The ith element of array of words M is at address M + 2 * i. Each of the following code fragments prints this element ldwx i,d aslx deco M,x ; i ; 2 * i ; output memory[m + 2*i] ldwx i,d addx i,d addx M,i deco 0,x ; i ; 2 * i ; M + 2 * i ; output memory[m + 2*i] Comp 162 Notes Page 12 of 16 March 29, 2017
13 Addressing mode costs At the microprogramming level, some addressing modes take longer to execute than others. Memory reads in particular take longer than additions. In the following table, modes are ranked in their probable order from slowest to fastest assuming a memory read takes longer than an addition. Mode Operand Memory Reads Additions sfx mem[mem[sp+op]+x] 2 2 sf mem[mem[sp+op]] 2 1 n mem[mem[op]] 2 0 sx mem[sp+op+x] 1 2 s mem[sp+op] 1 1 x mem[x+op] 1 1 d mem[op] 1 0 i op 0 0 Reading The last Pep/9 topic we cover is the implementation and manipulation of structures, particularly how they are used with heap storage. This is on pages Comp 162 Notes Page 13 of 16 March 29, 2017
14 Review Questions 1. In arrays A, B and C (each 100 bytes) are bitmaps that show the documents containing terms A, B and C respectively. Thus, for example bit I in B indicates if document I contains the term B. Write code that puts into array D (also 100 bytes) a bit map showing the documents that contain term A or term B but not term C. 2. Subroutine bitcount takes a word parameter (N) and returns the number of bits in N that are 1. Assuming the availability of this subroutine, write code that reports the number of documents in bitmap D of question We can detect when the heap is out of space by comparing heapptr with the SP register. Why is this difficult? 4. In Pep/9, addressing mode n is allowed with stro. Array days contains the names of the days of the week (padded out to 10 characters each) as follows days:.ascii "Monday\x00\x00\x00\x00".ascii "Tuesday\x00\x00\x00".ascii "Wednesday\x00".ascii "Thursday\x00\x00".ascii "Friday\x00\x00\x00\x00".ascii "Saturday\x00\x00".ascii "Sunday\x00\x00\x00\x00" Write code that inputs N and outputs the Nth day of the week using mode n. 5. If mode sfx were not available (but we could use the other 7 modes, how could we code the equivalent of ldwa 6,sfx 6. In Pep/8, mode sfx was named sxf. Why is the Pep/9 name more appropriate? Comp 162 Notes Page 14 of 16 March 29, 2017
15 Review Answers 1. ldwx 98,i ; offset of last word loop: ldwa C,x nota stwa -2,s ; not C lda A,x ora B,x ; A or B anda -2,s ; (A or B) and not C stwa D,x subx 2,i brge loop 2. ldwa 0,i stwa total,d ldwx 98,i loop: subsp 4,i ; for parameter and result of bitcount ldwa D,x stwa 0,s ; parameter is word from D call bitcount addsp 2,I ; get rid of parameter ldwa 0,s ; number of 1s adda total,d stwa total,d ; is added to total addsp 2,I ; and removed subx 2,i brge loop ; branch if more 3. The comparison needs to treat heapptr and SP as unsigned numbers. 4. The following uses indirect addressing deci N,d ldwa N,d asla stwa -2,s ; 2N asla ; 4N asla ; 8N adda -2,s ; 10N adda days,i ; days+10*n = address of start of string to output stwa P,d ; is stored in pointer P stro P,n ; output day name stop N:.block 2 P:.block 2 Comp 162 Notes Page 15 of 16 March 29, 2017
16 5. The following is one way addx 6,s ldwa 0,x 6. It better reflects the order of operations in calculating the operand address. For example, ldwa 4,sfx s: Mem[SP+ 4] f: Mem[Mem[SP + 4]] x: Mem[Mem[SP + 4] + X] Comp 162 Notes Page 16 of 16 March 29, 2017
Monday, March 13, 2017
Monday, March 13, 2017 Topics for today Arrays and Indexed Addressing Global arrays Local arrays Buffer exploit attacks Arrays and indexed addressing (section 6.4) So far we have looked at scalars (int,
More informationWednesday, March 14, 2018
Wednesday, March 14, 2018 Topics for today Arrays and Indexed Addressing Arrays as parameters of functions Multi-dimensional arrays Option A: Space-minimal solution Option B: Iliffe vectors Array bound
More informationWednesday, September 27, 2017
Wednesday, September 27, 2017 Topics for today Chapter 6: Mapping High-level to assembly-level The Pep/9 run-time stack (6.1) Stack-relative addressing (,s) SP manipulation Stack as scratch space Global
More informationMonday, March 27, 2017
Monday, March 27, 2017 Topics for today Indexed branching Implementation of switch statement Reusable subroutines Indexed branching It turns out that arrays are useful in translating other language constructs,
More informationWednesday, March 12, 2014
Wednesday, March 12, 2014 Topics for today Solutions to HW #3 Arrays and Indexed Addressing Global arrays Local arrays Buffer exploit attacks Solutions to Homework #3 1. deci N,d < (a) N not defined lda
More informationWednesday, February 28, 2018
Wednesday, February 28, 2018 Topics for today C functions and Pep/9 subroutines Introduction Location of subprograms in a program Translating functions (a) Void functions (b) Void functions with parameters
More informationWednesday, February 15, 2017
Wednesday, February 15, 2017 Topics for today Before and after assembly: Macros, Linkers Overview of Chapter 6 Branching Unconditional Status bits and branching If statements While statements The V and
More informationMonday, October 24, 2016
Monday, October 24, 2016 Topics for today Arrays and Indexed Addressing Arrays as parameters of functions Multi-dimensional arrays Option A: Space-minimal solution Option B: Iliffe vectors Array bound
More informationWednesday, April
Wednesday, April 9. 2014 Topics for today Addressing mode summary Structures Structures and dynamic memory Grammars and Languages (Chapter 7) String generation Parsing Regular languages Summary of addressing
More informationMonday, March 6, We have seen how to translate void functions. What about functions that return a value such as
Monday, March 6, 2017 Topics for today C functions and Pep/9 subroutines Translating functions (c) Non-void functions (d) Recursive functions Reverse Engineering: Pep/9 to C C Functions and Pep/9 Subroutines
More informationMonday, October 17, 2016
Monday, October 17, 2016 Topics for today C functions and Pep/8 subroutines Passing parameters by reference Globals Locals Reverse Engineering II Representation of Booleans C Functions and Pep/8 Subroutines
More informationWednesday, February 7, 2018
Wednesday, February 7, 2018 Topics for today The Pep/9 memory Four example programs The loader The assembly language level (Chapter 5) Symbolic Instructions Assembler directives Immediate mode and equate
More informationMonday, March 9, 2015
Monday, March 9, 2015 Topics for today C functions and Pep/8 subroutines Passing parameters by reference Globals Locals More reverse engineering: Pep/8 to C Representation of Booleans C Functions and Pep/8
More informationWednesday, October 17, 2012
Wednesday, October 17, 2012 Topics for today Arrays and Indexed Addressing Arrays as parameters of functions Multi-dimensional arrays Indexed branching Implementation of switch statement Arrays as parameters
More informationWednesday, September 20, 2017
Wednesday, September 20, 2017 Topics for today More high-level to Pep/9 translations Compilers and Assemblers How assemblers work Symbol tables ILC Pass 1 algorithm, Error checking Pass 2 Immediate mode
More informationCSC 221: Computer Organization, Spring 2009
1 of 7 4/17/2009 10:52 AM Overview Schedule Resources Assignments Home CSC 221: Computer Organization, Spring 2009 Practice Exam 2 Solutions The exam will be open-book, so that you don't have to memorize
More informationMonday, November 7, Structures and dynamic memory
Monday, November 7, 2016 Topics for today Structures Structures and dynamic memory Grammars and Languages (Chapter 7) String generation Parsing Regular languages Structures We have seen one composite data
More informationMonday, October 26, 2015
Monday, October 26, 2015 Topics for today Indexed branching Implementation of switch statement Reusable subroutines Indexed branching It turns out that arrays are useful in translating other language constructs,
More informationMonday, February 16, 2015
Monday, February 16, 2015 Topics for today How assemblers work Symbol tables ILC Pass 1 algorithm, Error checking Pass 2 Immediate mode and equate Assembler variants: Disassembler, Cross assembler Macros
More informationWednesday, April 16, 2014
Wednesday, pril 16, 2014 Topics for today Homework #5 solutions Code generation nalysis lgorithm 4: infix to tree Synthesis lgorithm 5: tree to code Optimization HW #5 solutions 1. lda 0,i ; for sum of
More informationMonday, September 28, 2015
Monda, September 28, 2015 Topics for toda Chapter 6: Mapping High-level to assembl-level The Pep/8 run-time stack (6.1) Stack-relative addressing (,s) SP manipulation Stack as scratch space Global variables
More informationWednesday, September 21, 2016
Wednesday, September 21, 2016 Topics for today More high-level to translations Compilers and Assemblers How assemblers work Symbol tables ILC Pass 1 algorithm, Error checking Pass 2 Immediate mode and
More informationWednesday, April 19, 2017
Wednesday, April 19, 2017 Topics for today Process management (Chapter 8) Loader Traps Interrupts, Time-sharing Storage management (Chapter 9) Main memory (1) Uniprogramming (2) Fixed-partition multiprogramming
More informationn NOPn Unary no operation trap U aaa NOP Nonunary no operation trap i
Instruction set Instruction Mnemonic Instruction Addressing Status Specifier Mode Bits 0000 0000 STOP Stop execution U 0000 0001 RET Return from CALL U 0000 0010 RETTR Return from trap U 0000 0011 MOVSPA
More informationAPPLICATION LEVEL HIGH-ORDER LANGUAGE LEVEL ASSEMBLY LEVEL OPERATING SYSTEM LEVEL INSTRUCTION SET ARCHITECTURE LEVEL MICROCODE LEVEL LOGIC GATE LEVEL
Operating System APPLICATION LEVEL HIGH-ORDER LANGUAGE LEVEL ASSEMBLY LEVEL OPERATING SYSTEM LEVEL 4 INSTRUCTION SET ARCHITECTURE LEVEL MICROCODE LEVEL LOGIC GATE LEVEL Three types of operating systems
More informationChapter 6. Compiling to the Assembly Level
Chapter 6 Compiling to the Assembly Level Direct addressing Oprnd = Mem[OprndSpec] Asmb5 letter: d The operand specifier is the address in memory of the operand. Immediate addressing Oprnd = OprndSpec
More informationWednesday, October 4, Optimizing compilers source modification Optimizing compilers code generation Your program - miscellaneous
Wednesday, October 4, 2017 Topics for today Code improvement Optimizing compilers source modification Optimizing compilers code generation Your program - miscellaneous Optimization Michael Jackson Donald
More informationWednesday, February 19, 2014
Wednesda, Februar 19, 2014 Topics for toda Solutions to HW #2 Topics for Eam #1 Chapter 6: Mapping High-level to assembl-level The Pep/8 run-time stack Stack-relative addressing (,s) SP manipulation Stack
More informationChapter 8. Process Management
Chapter 8 Process Management Operating System APPLICATION LEVEL HIGH-ORDER LANGUAGE LEVEL ASSEMBLY LEVEL OPERATING SYSTEM LEVEL 4 INSTRUCTION SET ARCHITECTURE LEVEL MICROCODE LEVEL LOGIC GATE LEVEL Three
More informationAnnouncements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1
Announcements assign due tonight No late submissions Labs start this week Very helpful for assign1 Goals for Today Pointer operators Allocating memory in the heap malloc and free Arrays and pointer arithmetic
More informationA rubric for programming assignments
Fall 2012 Comp 162 Peter Smith A rubric for programming assignments Generally, half the points for a program assignment are for the Correctness of the program with respect to the specification. The other
More informationWednesday, April 22, 2015
Wednesday, April 22, 2015 Topics for today Topics for Exam 3 Process management (Chapter 8) Loader Traps Interrupts, Time-sharing Storage management (Chapter 9) Main memory (1) Uniprogramming (2) Fixed-partition
More informationM4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE
M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE NOTE: 1. There are TWO PARTS in this Module/Paper. PART ONE contains FOUR questions and PART TWO contains FIVE questions. 2. PART ONE is to be
More informationWednesday, September 13, Chapter 4
Wednesday, September 13, 2017 Topics for today Introduction to Computer Systems Static overview Operation Cycle Introduction to Pep/9 Features of the system Operational cycle Program trace Categories of
More informationMonday, February 11, 2013
Monday, February 11, 2013 Topics for today The Pep/8 memory Four example programs The loader The assembly language level (Chapter 5) Symbolic Instructions Assembler directives Immediate mode and equate
More informationWednesday, November 15, 2017
Wednesday, November 15, 2017 Topics for today Code generation Synthesis Algorithm 5: tree to code Optimizations Code generation Algorithm 5: generating assembly code Visiting all the nodes in a linked
More informationCS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Allocating Space
CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Allocating Space Dynamic Memory Allocation All variables, arrays, structures and unions that we worked with so far are statically allocated,
More informationCS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018
CS 31: Intro to Systems Pointers and Memory Kevin Webb Swarthmore College October 2, 2018 Overview How to reference the location of a variable in memory Where variables are placed in memory How to make
More informationreply db y prompt db Enter your favourite colour:, 0 colour db 80 dup(?) i db 20 k db? num dw 4000 large dd 50000
Declaring Variables in Assembly Language As in Java, variables must be declared before they can be used Unlike Java, we do not specify a variable type in the declaration in assembly language Instead we
More informationChapter. Assembly Language
Chapter 5 Assembly Language Mappings The mapping from Asmb5 to ISA3 is one-toone The mapping from HOL6 to Asmb5 is oneto-many Symbols Defined by an identifier followed by a colon at the start of a statement
More informationadvanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef
today advanced data types (1) typedef. mon 23 sep 2002 homework #1 due today homework #2 out today quiz #1 next class 30-45 minutes long one page of notes topics: C advanced data types dynamic memory allocation
More informationMicroprocessor and Assembly Language Week-5. System Programming, BCS 6th, IBMS (2017)
Microprocessor and Assembly Language Week-5 System Programming, BCS 6th, IBMS (2017) High Speed Memory Registers CPU store data temporarily in these location CPU process, store and transfer data from one
More informationWednesday, February 4, Chapter 4
Wednesday, February 4, 2015 Topics for today Introduction to Computer Systems Static overview Operation Cycle Introduction to Pep/8 Features of the system Operational cycle Program trace Categories of
More informationComputer Architecture and System Software Lecture 07: Assembly Language Programming
Computer Architecture and System Software Lecture 07: Assembly Language Programming Instructor: Rob Bergen Applied Computer Science University of Winnipeg Announcements New assembly examples uploaded to
More informationC Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008
C Programming Language: C ADTs, 2d Dynamic Allocation Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008 Overview Row major format 1 and 2-d dynamic allocation struct
More informationIntermediate Programming, Spring 2017*
600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general
More informationCS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0)
CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Dynamic Memory Allocation All variables, arrays, structures and unions that we worked with so far are statically allocated, meaning
More informationQUIZ. Name all the 4 parts of the fetch-execute cycle.
QUIZ Name all the 4 parts of the fetch-execute cycle. 1 Solution Name all the 4 parts of the fetch-execute cycle. 2 QUIZ Name two fundamental differences between magnetic drives and optical drives: 3 Solution
More informationQUIZ. Name all the 4 parts of the fetch-execute cycle.
QUIZ Name all the 4 parts of the fetch-execute cycle. 1 Solution Name all the 4 parts of the fetch-execute cycle. 2 QUIZ Name two fundamental differences between magnetic drives and optical drives: 3 QUIZ
More informationUnderstanding Pointers
Division of Mathematics and Computer Science Maryville College Pointers and Addresses Memory is organized into a big array. Every data item occupies one or more cells. A pointer stores an address. A pointer
More informationSubprograms, Subroutines, and Functions
Subprograms, Subroutines, and Functions Subprograms are also called subroutines, functions, procedures and methods. A function is just a subprogram that returns a value; say Y = SIN(X). In general, the
More informationSubject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100
Code: DC-05 Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100 NOTE: There are 11 Questions in all. Question 1 is compulsory and carries 16 marks. Answer to Q. 1. must be written in the space
More informationCS201 - Introduction to Programming Glossary By
CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with
More informationVARDHAMAN COLLEGE OF ENGINEERING (AUTONOMOUS) Shamshabad, Hyderabad
Introduction to MS-DOS Debugger DEBUG In this laboratory, we will use DEBUG program and learn how to: 1. Examine and modify the contents of the 8086 s internal registers, and dedicated parts of the memory
More informationActually, C provides another type of variable which allows us to do just that. These are called dynamic variables.
When a program is run, memory space is immediately reserved for the variables defined in the program. This memory space is kept by the variables until the program terminates. These variables are called
More informationDynamic memory allocation (malloc)
1 Plan for today Quick review of previous lecture Array of pointers Command line arguments Dynamic memory allocation (malloc) Structures (Ch 6) Input and Output (Ch 7) 1 Pointers K&R Ch 5 Basics: Declaration
More informationArrays, Pointers and Memory Management
Arrays, Pointers and Memory Management EECS 2031 Summer 2014 Przemyslaw Pawluk May 20, 2014 Answer to the question from last week strct->field Returns the value of field in the structure pointed to by
More informationOutline. Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary
Pointers 1 2 Outline Computer Memory Structure Addressing Concept Introduction to Pointer Pointer Manipulation Summary 3 Computer Memory Revisited Computers store data in memory slots Each slot has an
More informationLow-level software. Components Circuits Gates Transistors
QUIZ Pipelining A computer pipeline has 4 processors, as shown above. Each processor takes 15 ms to execute, and each instruction must go sequentially through all 4 processors. A program has 10 instructions.
More informationMonday, April 9, 2018
Monday, April 9, 208 Topics for today Grammars and Languages (Chapter 7) Finite State Machines Semantic actions Code generation Overview Finite State Machines (see 7.2) If a language is regular (Type 3)
More informationStructs and Alignment CSE 351 Spring
Structs and Alignment CSE 351 Spring 2018 http://xkcd.com/1168/ Administrivia Homework 3 due Wednesday Lab 3 released, due next week Lab 2 and midterm will be graded this week [in that order] 2 Roadmap
More informationShort Notes of CS201
#includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system
More informationMachine-Level Programming V: Unions and Memory layout
Machine-Level Programming V: Unions and Memory layout Slides adapted from Bryant and O Hallaron Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition 1 FAQ Call conventions
More informationChapter 2 (Dynamic variable (i.e. pointer), Static variable)
Chapter 2 (Dynamic variable (i.e. pointer), Static variable) August_04 A2. Identify and explain the error in the program below. [4] #include int *pptr; void fun1() { int num; num=25; pptr= #
More informationC Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:
C Programming Code: MBD101 Duration: 10 Hours Prerequisites: You are a computer science Professional/ graduate student You can execute Linux/UNIX commands You know how to use a text-editing tool You should
More informationSYSC 2006 C Winter 2012
SYSC 2006 C Winter 2012 Pointers and Arrays Copyright D. Bailey, Systems and Computer Engineering, Carleton University updated Sept. 21, 2011, Oct.18, 2011,Oct. 28, 2011, Feb. 25, 2011 Memory Organization
More informationECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee
ECE 250 / CS 250 Computer Architecture C to Binary: Memory & Data Representations Benjamin Lee Slides based on those from Alvin Lebeck, Daniel Sorin, Andrew Hilton, Amir Roth, Gershon Kedem Administrivia
More informationComputer System and programming in C
1 Basic Data Types Integral Types Integers are stored in various sizes. They can be signed or unsigned. Example Suppose an integer is represented by a byte (8 bits). Leftmost bit is sign bit. If the sign
More informationC Language Part 3. Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee
C Language Part 3 Pointers (revisited) int i = 4, j = 6, *p = &i, *q = &j, *r; if (p == &i)...; if (p == (& i))...;... = **&p;... = *(*(& p));... = 9 * *p / *q + 8;... = (((9*(*p)))/(*q)) + 8; *(r = &i)
More informationCS401 - Computer Architecture and Assembly Language Programming Glossary By
CS401 - Computer Architecture and Assembly Language Programming Glossary By absolute address : A virtual (not physical) address within the process address space that is computed as an absolute number.
More informationA brief introduction to C programming for Java programmers
A brief introduction to C programming for Java programmers Sven Gestegård Robertz September 2017 There are many similarities between Java and C. The syntax in Java is basically
More informationAt this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:
Pointers and Arrays Part II We will continue with our discussion on the relationship between pointers and arrays, and in particular, discuss how arrays with dynamical length can be created at run-time
More informationCS113: Lecture 9. Topics: Dynamic Allocation. Dynamic Data Structures
CS113: Lecture 9 Topics: Dynamic Allocation Dynamic Data Structures 1 What s wrong with this? char *big_array( char fill ) { char a[1000]; int i; for( i = 0; i < 1000; i++ ) a[i] = fill; return a; void
More informationDynamic Data Structures. CSCI 112: Programming in C
Dynamic Data Structures CSCI 112: Programming in C 1 It s all about flexibility In the programs we ve made so far, the compiler knows at compile time exactly how much memory to allocate for each variable
More informationCS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/
CS61C Machine Structures Lecture 5 C Structs & Memory Mangement 1/27/2006 John Wawrzynek (www.cs.berkeley.edu/~johnw) www-inst.eecs.berkeley.edu/~cs61c/ CS 61C L05 C Structs (1) C String Standard Functions
More informationFOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body;
CLASSROOM SESSION Loops in C Loops are used to repeat the execution of statement or blocks There are two types of loops 1.Entry Controlled For and While 2. Exit Controlled Do while FOR Loop FOR Loop has
More informationProgramming Language B
Programming Language B Takako Nemoto (JAIST) 7 January Takako Nemoto (JAIST) 7 January 1 / 13 Usage of pointers #include int sato = 178; int sanaka = 175; int masaki = 179; int *isako, *hiroko;
More informationCS61, Fall 2012 Section 2 Notes
CS61, Fall 2012 Section 2 Notes (Week of 9/24-9/28) 0. Get source code for section [optional] 1: Variable Duration 2: Memory Errors Common Errors with memory and pointers Valgrind + GDB Common Memory Errors
More informationIntel 8086: Instruction Set
IUST-EE (Chapter 6) Intel 8086: Instruction Set 1 Outline Instruction Set Data Transfer Instructions Arithmetic Instructions Bit Manipulation Instructions String Instructions Unconditional Transfer Instruction
More informationRoadmap. Java: Assembly language: OS: Machine code: Computer system:
Roadmap C: car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Assembly language: Machine code: Computer system: get_mpg: pushq movq... popq ret %rbp %rsp, %rbp
More informationCSE 333 Lecture 2 Memory
CSE 333 Lecture 2 Memory John Zahorjan Department of Computer Science & Engineering University of Washington Today s goals - some terminology - review of memory resources - reserving memory - type checking
More informationWednesday, November 8, 2017
Wednesday, November 8, 207 Topics for today Grammars and Languages (hapter 7) Finite State Machines Semantic actions ode generation - Overview Finite State Machines (see 7.2) If a language is regular (Type
More informationExtra-credit QUIZ Pipelining -due next time-
QUIZ Pipelining A computer pipeline has 4 processors, as shown above. Each processor takes 15 ms to execute, and each instruction must go sequentially through all 4 processors. A program has 10 instructions.
More informationDynamic Allocation in C
Dynamic Allocation in C C Pointers and Arrays 1 The previous examples involved only targets that were declared as local variables. For serious development, we must also be able to create variables dynamically,
More informationDynamic Allocation in C
Dynamic Allocation in C 1 The previous examples involved only targets that were declared as local variables. For serious development, we must also be able to create variables dynamically, as the program
More informationECE 2035 A Programming HW/SW Systems Spring problems, 5 pages Exam Three 13 April Your Name (please print clearly)
Instructions: This is a closed book, closed note exam. Calculators are not permitted. If you have a question, raise your hand; do not leave your seat. Please work the exam in pencil and do not separate
More informationEXPERIMENT WRITE UP. LEARNING OBJECTIVES: 1. Get hands on experience with Assembly Language Programming 2. Write and debug programs in TASM/MASM
EXPERIMENT WRITE UP AIM: Assembly language program for 16 bit BCD addition LEARNING OBJECTIVES: 1. Get hands on experience with Assembly Language Programming 2. Write and debug programs in TASM/MASM TOOLS/SOFTWARE
More informationCS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 21: Generating Pentium Code 10 March 08
CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 21: Generating Pentium Code 10 March 08 CS 412/413 Spring 2008 Introduction to Compilers 1 Simple Code Generation Three-address code makes it
More informationQ. State and Explain steps involved in program development. [w-08, w-10, s-12, w-11]
Q. State and Explain steps involved in program development. [w-08, w-10, s-12, w-11] Answer: 1. Defining Problem 2. Algorithm 3. Flowchart 4. Initialization of checklist 5. Choosing instructions 6. Converting
More informationDeclaring Pointers. Declaration of pointers <type> *variable <type> *variable = initial-value Examples:
1 Programming in C Pointer Variable A variable that stores a memory address Allows C programs to simulate call-by-reference Allows a programmer to create and manipulate dynamic data structures Must be
More informationLectures 13 & 14. memory management
Lectures 13 & 14 Linked lists and memory management Courtesy of Prof. Garcia (UCB) CS61C L05 Introduction to C (pt 3) (1) Review Pointers and arrays are virtually same C knows how to increment pointers
More informationHeap Arrays. Steven R. Bagley
Heap Arrays Steven R. Bagley Recap Data is stored in variables Can be accessed by the variable name Or in an array, accessed by name and index a[42] = 35; Variables and arrays have a type int, char, double,
More informationLab 3. Pointers Programming Lab (Using C) XU Silei
Lab 3. Pointers Programming Lab (Using C) XU Silei slxu@cse.cuhk.edu.hk Outline What is Pointer Memory Address & Pointers How to use Pointers Pointers Assignments Call-by-Value & Call-by-Address Functions
More informationIMPORTANT QUESTIONS IN C FOR THE INTERVIEW
IMPORTANT QUESTIONS IN C FOR THE INTERVIEW 1. What is a header file? Header file is a simple text file which contains prototypes of all in-built functions, predefined variables and symbolic constants.
More informationFinal CSE 131B Winter 2003
Login name Signature Name Student ID Final CSE 131B Winter 2003 Page 1 Page 2 Page 3 Page 4 Page 5 Page 6 Page 7 Page 8 _ (20 points) _ (25 points) _ (21 points) _ (40 points) _ (30 points) _ (25 points)
More informationThese are reserved words of the C language. For example int, float, if, else, for, while etc.
Tokens in C Keywords These are reserved words of the C language. For example int, float, if, else, for, while etc. Identifiers An Identifier is a sequence of letters and digits, but must start with a letter.
More informationCode segment Stack segment
Registers Most of the registers contain data/instruction offsets within 64 KB memory segment. There are four different 64 KB segments for instructions, stack, data and extra data. To specify where in 1
More informationCS C Primer. Tyler Szepesi. January 16, 2013
January 16, 2013 Topics 1 Why C? 2 Data Types 3 Memory 4 Files 5 Endianness 6 Resources Why C? C is exteremely flexible and gives control to the programmer Allows users to break rigid rules, which are
More informationAssembly Language. Operand Size. The internal registers. Computers operate on chunks of data composed of a particular number of bits.
1 2 Chapter 6 Assembly Language Operand Size 8 bits 16 bits Computers operate on chunks of data composed of a particular number of bits. The 68K has a 32-bit architecture and a 16-bit organization. Internal
More informationCSE 12 Spring 2016 Week One, Lecture Two
CSE 12 Spring 2016 Week One, Lecture Two Homework One and Two: hw2: Discuss in section today - Introduction to C - Review of basic programming principles - Building from fgetc and fputc - Input and output
More information