Pointers and Arrays in C and Assembly Language

Slides:



Advertisements
Similar presentations
compilers and interpreters
Advertisements

Instruction Set Design
Goal: Write Programs in Assembly
The University of Adelaide, School of Computer Science
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 5: Data Transfer Instructions / Control Flow Instructions Partially adapted from Computer.
IT253: Computer Organization Lecture 6: Assembly Language and MIPS: Programming Tonga Institute of Higher Education.
COMP3221 lec-12-mem-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 12: Memory Access - II
L5 – Addressing Modes 1 Comp 411 – Spring /29/08 Operands and Addressing Modes Where is the data? Addresses as data Names and Values Indirection.
CS 101 Problem Solving and Structured Programming in C Sami Rollins Spring 2003.
9/29: Lecture Topics Memory –Addressing (naming) –Address space sizing Data transfer instructions –load/store on arrays on arrays with variable indices.
1 Chapter-01 Introduction to Computers and C++ Programming.
Georgia Institute of Technology Student Computer Simulation Barbara Ericson Georgia Tech Sept 2005.
Cosc 2150: Computer Organization
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
ECEG-3202 Computer Architecture and Organization Chapter 6 Instruction Sets: Addressing Modes and Formats.
9/29: Lecture Topics Conditional branch instructions
Computer Organization Instructions Language of The Computer (MIPS) 2.
Pointers and Arrays in C and Assembly Language. Arrays Provide useful abstraction Match up to machine memory organization Array index computation –time.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 14, 15 Addressing Mode.
Arrays In high-level languages, we have several technigues available for constucting data stuctures: −One-dimensional arrays −Multi-dimensional arrays.
SPACE COMPLEXITY & TIME COMPLEXITY 1. ALGORITHMS COMPLEXITY 2.
Why don’t programmers have to program in machine code?
Displacement (Indexed) Stack
Overview of Instruction Set Architectures
Types for Programs and Proofs
CSCI206 - Computer Organization & Programming
CS2100 Computer Organisation
Introduction to programming
Computational Thinking, Problem-solving and Programming: General Principals IB Computer Science.
Compiler Construction (CS-636)
The University of Adelaide, School of Computer Science
1. Introduction A microprocessor executes instructions given by the user Instructions should be in a language known to the microprocessor Microprocessor.
Morgan Kaufmann Publishers
A451 Theory – 7 Programming 7A, B - Algorithms.
Optimization Code Optimization ©SoftMoore Consulting.
William Stallings Computer Organization and Architecture 8th Edition
The University of Adelaide, School of Computer Science
Compiler Construction
Teaching Computing to GCSE
An Overview to Compiler Design
Clear1 and Clear2 clear1(int array[], int size) { int i; for (i = 0; i < size; i += 1) array[i] = 0; } clear2(int *array, int size) {
Teaching Computing to GCSE
ECE243 Interpreter Lab.
CSCI206 - Computer Organization & Programming
Computer Programming Machine and Assembly.
High Level Programming Languages
Review for Test1.
CSc 453 Interpreters & Interpretation
The University of Adelaide, School of Computer Science
Operands and Addressing Modes
September 5 Fang-Yi’s Office Hours Friday 10am  12pm (and another TBD) Programming Language Issues (JAVA vs. C, Pointers vs. Arrays) Representations Instructions.
ECEG-3202 Computer Architecture and Organization
Computer Organization and Design Assembly & Compilation
Other ISAs Next, we’ll first we look at a longer example program, starting with some C code and translating it into our assembly language. Then we discuss.
Lecture 1. Program Surgery
2 code samples int [] array; int i, j, temp; for(i = 0; i < array.length/2; i++) { j = array.length-1-i; temp = array[i]; array[i] = array[j]; array[j]
Other ISAs Next, we’ll first we look at a longer example program, starting with some C code and translating it into our assembly language. Then we discuss.
3.
COMS 361 Computer Organization
University of Gujrat Department of Computer Science
Yan Shi CS/SE 2630 Lecture Notes
Computer Organization and Design Addressing Modes
William Stallings Computer Organization and Architecture 8 th Edition Chapter 11 Instruction Sets: Addressing Modes and Formats.
1.3.7 High- and low-level languages and their translators
Operands and Addressing Modes
9/27: Lecture Topics Memory Data transfer instructions
You are the manager of a team of ARM programmers
Compiler Construction
Algoritmos y Programacion
Conditional Branching (beq)
Presentation transcript:

Pointers and Arrays in C and Assembly Language

Arrays Provide useful abstraction Match up to machine memory organization Array index computation time consuming is there a faster way

Arrays and Pointers in C Arrays can also be accessed with pointers in C Uses low-level model of memory at the high- level language level Can result in faster code but modern compilers can often do this work, so we are better off, in general, using the high-level abstractions

C code examples - Set array values to 0 // array version clear1(int array[], int size) { int i; for(i = 0; i < size; i = i+1) array[i] = 0; } // pointer version clear2(int *array, int size) { int *p; for(p = &array[0]; p < &array[size]; p = p+1) *p = 0; }

Array version assembly code clear1: add X9, XZR, XZR // i = 0 fori: lsl X10, X9, #8 // X10 = 8*i add X10, X0, X10 // X10 = addr of array[i] stur XZR, [X10, #0] // array[i] = 0 add X9, X9, 1 // i = i + 1 subs XZR, X9, X1 // i < size ? b.lt fori // if so, continue endfor: Six instructions in loop.

Pointer version assembly code clear2: add X9, X0, XZR // p = addr of array[0] forp: stur XZR, [X9, #0] // memory[p] = 0 add X9, X9, 8 // p = p + 4 lsl X10, X1, 3 // X10 = 8 * size add X10, X10, X0 // X10 = addr array[size] subs X9, X10 // p < &array[size] ? b.lt forp // if so, continue endfor:

Pointer version assembly code clear2: add X9, X0, XZR // p = addr of array[0] lsl X10, X1, 3 // X10 = 8 * size add X10, X10, X0 // X10 = addr array[size] forp: stur XZR, [X9, #0] // memory[p] = 0 add X9, X9, 8 // p = p + 4 subs X9, X10 // p < &array[size] ? b.lt forp // if so, continue endfor:

What to use in code 1970's 1990’s and beyond compilers simpler optimize in C code 1990’s and beyond optimizing compilers can convert code for arrays to be similar to pointer code Programming in high level language should use the appropriate abstraction let the compiler do the work fewer errors in program using appropriate abstractions