Lecture 8 Data structures

Slides:



Advertisements
Similar presentations
Lecture 13: 10/8/2002CS170 Fall CS170 Computer Organization and Architecture I Ayman Abdel-Hamid Department of Computer Science Old Dominion University.
Advertisements

Intermediate Code Generation
The University of Adelaide, School of Computer Science
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson slides3.ppt Modification date: March 16, Addressing Modes The methods used in machine instructions.
1 Compiler Construction Intermediate Code Generation.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
Pointers Applications
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
Runtime Environments Compiler Construction Chapter 7.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 3 – August 28, 2001.
4-1 Embedded Systems C Programming Language Review and Dissection II Lecture 4.
Data Structures - Part I CS 215 Lecture 7. Motivation  Real programs use abstractions like lists, trees, stacks, and queues.  The data associated with.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
Arrays in MIPS Assembly Computer Organization and Assembly Language: Module 6.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 10 Ahmed Ezzat.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Windows Programming Lecture 03. Pointers and Arrays.
CS 404 Introduction to Compiler Design
Assembly language.
Format of Assembly language
F453 Computing Questions and Answers
Compiler Construction (CS-636)
The Stack Chapter 5 Lecture notes for SPARC Architecture, Assembly Language Programming and C, Richard P. Paul by Anu G. Bourgeois.
Homework Reading Labs PAL, pp
Programming Languages and Paradigms
C Language VIVA Questions with Answers
Chapter7 Structure & C++
Optimization Code Optimization ©SoftMoore Consulting.
Computer Architecture and Organization Miles Murdocca and Vincent Heuring Chapter 4 – The Instruction Set Architecture.
Programming Paradigms
Java Review: Reference Types
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Programming Languages and Paradigms
Defining Types of data expression Dn [name] expression Dn [name]
Symbolic Instruction and Addressing
Chapter 7 LC-2 Assembly Language.
Lecture 5 Subroutines and stack
C Structures, Unions, Bit Manipulations and Enumerations
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Bases and Representations, Memory, Pointers, Arrays, For-Loops
Assembly Language Programming I: Introduction
7 Arrays.
Symbolic Instruction and Addressing
(Array and Addressing Modes)
Memory Allocation CS 217.
Lecture 18 Arrays and Pointer Arithmetic
ECEG-3202 Computer Architecture and Organization
Pointers C#, pointers can only be declared to hold the memory addresses of value types int i = 5; int *p; p = &i; *p = 10; // changes the value of i to.
COMP26120: Algorithms and Imperative programming
Lecture 3: Main Memory.
Computer Organization and Design Assembly & Compilation
Chapter 4 –Requirements for coding in Assembly Language
(Array and Addressing Modes)
Abstract Data Types, Elementary Data Structures and Arrays
Data Structures and Algorithms Introduction to Pointers
Assembler Directives end label end of program, label is entry point
Computer Architecture and System Programming Laboratory
Symbol Table 薛智文 (textbook ch#2.7 and 6.5) 薛智文 96 Spring.
Chapter 9: Pointers and String
Chapter 6 –Symbolic Instruction and Addressing
Process.
Lecture 13 Harvard architecture Coccone OS demonstrator
Lecture 11 How assembler works
Computer Organization and Assembly Language
Lecture 7 Separate compilation
Lecture 12 Input/Output (programmer view)
Introduction to 8086 Assembly Language
Week 5 Computers are like Old Testament gods; lots of rules and no mercy. Joseph Campbell.
(Array and Addressing Modes)
Extra C Material Based on material in: The C Programming Language, Second Edition by Brian W. Kernighan and Dennis M. Ritchie. Prentice Hall, Inc., 1988. 
Presentation transcript:

Lecture 8 Data structures Computing platforms Novosibirsk State University University of Hertfordshire D. Irtegov, A.Shafarenko 2018

Data structures in CdM-8 assembly Pointers No such type You can use any 8-bit numeric value as memory address Arrays No such structure in the language But some supporting constructs, like ds directive You can use addresses (pointers) and address arithmetic to work with arrays Strings Support for string literals in dc directive No other support You can operate strings like arrays of characters

More on data structures in CdM-8 You can use tplate section to describe a structure layout Arrays of structures Tplate section have _ symbol which designates size of the structure roughly equivalent to C sizeof operator You must implement C-style pointer arithmetic manually but it is hard because you do not have multiplication May be, it is good idea to pad structures to power of 2 But in CdM-8 you must save memory Linked lists and other linked structures (trees, graphs) No builtin support Note that C also has no builtin linked lists it justs offer facilities to implement them manually (structures and pointers

Arrays Sequence of elements of same size Can be allocated by ds (Define Space) directive Or by dc directive with many operands (“initialized array”) array: dc 1,2,3,4 array2: dc “Hello world” 1 2 3 Array start

Byte array (array of 8-bit ints or chars) Indexing with arbitrary index ldi r0, array ldi r1, index add r0,r1 # r1 contains address of array[index] ld r1,r1. # r1 contains value of array[index] Scanning all elements ldi r1, array.size+1 while dec r1 stays gt … inc r0 wend

How to determine array size? In most assemblers, you can assign arithmetic expressions to symbols Like: array: dc 0,1,2,3,4 .set array_size=.-array # . (dot characters) means current position In CdM-8, no equivalent of .set directive, no . pseudo-symbol And limitations on symbol arithmetic I plan a feature request

Using tplate section to define constants tplate array dc 0,2,5,3,4 ds 1 size: asect 0 br main array: dc 0,2,5,3,4 main: ldi r1, array.size Ugly, because you need to duplicate dc statement You can use macros to avoid this We will discuss macros later Why all this? Because tplate directive produces no code And its labels are calculated in compile time, not in runtime

So, the array scanning routine (body) main: ldi r0,array ld r0,r3 ldi r1, array.size while dec r1 stays gt ld r0,r2 # value of current element if cmp r2,r3 is gt move r2,r3 fi inc r0 wend halt

Two-dimensional arrays Two possible implementations: Array of arrays Indexing of [i1][i2] calculated as i1*row_size+i2 Not convenient on CdM-8 because you have no multiplication Impossible if rows have different size (why not?) Array of pointers (takes extra memory) row1: ds 5 row2: ds 6 row3: ds 4 array: dc row1, row2, row3 # Yes you can use labels as values in dc!

Arrays on stack Why not? Just allocate enough space on stack by using addsp instruction This way you can even allocate dynamic arrays (size defined at run time) Use ldsa instead of ldi to load array start pointer in r0..3 BTW, do you know that C99 allows variable size arrays? Or, you can push the array element by element, and thus initialize it

Copy array on stack and back (in reverse) ldi r0, array ldi r1, array.size while dec r1 stays gt ld r0,r2 push r2 inc r0 wend ldi r0, array ldi r1, array.size while dec r1 stays gt pop r2 st r0,r2 inc r0 wend

Structures Structure is a collection of fields Fields are defined by offset from the beginning of the structure It can be seen as an array with predefined indices field1 field2 field3 field4 Struct start

Tplate section can define structures tplate struct field1: ds 1 field2: ds 1 field3: ds 1 field4: ds 1 asect 0 struct: ds 4. # you can have tplate and label with same name! main: ldi r0, struct+struct.field3 # unfortunately, impossible in CdM-8! ldi r0, struct ldi r1, struct.field3 add r0, r1 # address of field3 is calculated at runtime

Linked lists But you can interpret some fields as addresses Below is valid CdM-8 data section asect 0x0D item1: dc item2, 5 item2: dc item3, 7 item3: dc item4, -3 item4: dc 0x00, 8 item2 5 Item1 item3 7 item4 -3 8