Arrays. Outline 1.(Introduction) Arrays An array is a contiguous block of list of data in memory. Each element of the list must be the same type and use.

Slides:



Advertisements
Similar presentations
Goal: Write Programs in Assembly
Advertisements

Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
C Programming and Assembly Language Janakiraman V – NITK Surathkal 2 nd August 2014.
Department of Computer Science and Software Engineering
Assembly 02. Outline mov Command Registers Memory EFLAGS Arithmetic 1.
1 Lecture 4: Data Transfer, Addressing, and Arithmetic Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
PC hardware and x86 3/3/08 Frans Kaashoek MIT
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Accessing parameters from the stack and calling functions.
Practical Session 3. The Stack The stack is an area in memory that its purpose is to provide a space for temporary storage of addresses and data items.
ICS312 Set 3 Pentium Registers. Intel 8086 Family of Microprocessors All of the Intel chips from the 8086 to the latest pentium, have similar architectures.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Stack Operations Runtime Stack PUSH Operation POP.
Addressing Modes Chapter 11 S. Dandamudi To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer,  S.
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
6.828: PC hardware and x86 Frans Kaashoek
CDP ECE Spring 2000 ECE 291 Spring 2000 Lecture 7: More on Addressing Modes, Structures, and Stack Constantine D. Polychronopoulos Professor, ECE.
Dr. José M. Reyes Álamo 1.  The 80x86 memory addressing modes provide flexible access to memory, allowing you to easily access ◦ Variables ◦ Arrays ◦
3.7 String Instructions Specifying the Operands’ Size and Address and the String Direction STRING = a data collection in memory. String ELEMENTS can be:
CSC 221 Computer Organization and Assembly Language
INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 3 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 4 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
1 ICS 51 Introductory Computer Organization Fall 2009.
Assembly Language for x86 Processors 7th Edition Chapter 13: High-Level Language Interface (c) Pearson Education, All rights reserved. You may modify.
ICS312 Lecture13 String Instructions.
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
Addressing Modes Chapter 6 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Assembly Language. Symbol Table Variables.DATA var DW 0 sum DD 0 array TIMES 10 DW 0 message DB ’ Welcome ’,0 char1 DB ? Symbol Table Name Offset var.
מבנה מחשב תרגול 4 מבנה התוכנית. 2 Introduction When we wrote: ‘int n = 10’; the compiler allocated the variable’s memory address and labeled it ‘n’. In.
String Processing Chapter 10 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Chapter 2 Parts of a Computer System. 2.1 PC Hardware: Memory.
Assembly 03. Outline inc, dec movsx jmp, jnz Assembly Code Sections Labels String Variables equ $ Token 1.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Computer And Programming Array and Pointer. Array provides a means to allocating and accessing memory. Pointers, on the other hand, provides a way to.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Variables and memory addresses
String Instructions String instructions were designed to operate on large data structures. The SI and DI registers are used as pointers to the data structures.
Assembly 09. Outline Strings in x86 esi, edi, ecx, eax stosb, stosw, stosd cld, std rep loop 1.
Computer Organization & Assembly Language University of Sargodha, Lahore Campus Prepared by Ali Saeed.
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
CSC 221 Computer Organization and Assembly Language Lecture 15: STACK Related Instructions.
NASM ASSEMBLER & COMPILE WITH GCC 어셈러브 refered to ‘PC Assembly Language’ by Paul A. Carter
Addressing Modes Dr. Hadi Hassan.  Two Basic Questions  Where are the operands?  How memory addresses are computed?  Intel IA-32 supports 3 fundamental.
Assembly Language Addressing Modes. Introduction CISC processors usually supports more addressing modes than RISC processors. –RISC processors use the.
Chapter 8 String Operations. 8.1 Using String Instructions.
Stack Operations Dr. Hadi AL Saadi.
Computer Architecture and Assembly Language
Assembly language.
Assembly Lab 3.
Assembly 07 String Processing.
BYTE AND STRING MANIPULATON
Chapter 9.
Chapter six of V4: The String Instructions
Aaron Miller David Cohen Spring 2011
Chapter 4 Data Movement Instructions
Assembly IA-32.
Microcomputer Programming
Arrays in C.
BIC 10503: COMPUTER ARCHITECTURE
Assembly Language for Intel-Based Computers, 5th Edition
T opic: S TRING I NSTRUCTION P RESENTED B Y: N OOR FATIMA M AHA AKRAM ASIF.
Computer Architecture CST 250
Computer Architecture and System Programming Laboratory
Computer Architecture and Assembly Language
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Presentation transcript:

Arrays

Outline 1.(Introduction) Arrays An array is a contiguous block of list of data in memory. Each element of the list must be the same type and use exactly the same number of bytes of memory for storage. Because of these properties, arrays allow efficient access of the data by its position (or index) in the array. The address of any element can be computed by knowing three facts: 1)The address of the first element of the array. 2)The number of bytes in each element 3)The index of the element 1.Array/String instructions

1.Introduction Defining arrays Accessing elements of arrays More advanced indirect addressing Example Multidimensional arrays

Defining arrays To define an initialized array in the data segment, use the normal db, dw, etc. directives. a useful directive named TIMES To define an uninitialized array in the bss segment, use the resb, resw, etc.

Defining arrays as local variables on the stack one computes the total bytes required by all local variables, including arrays, and subtracts this from ESP (either directly or using the ENTER instruction). For example, if a function needed a character variable, two double word integers and a 50 element word array, one would need × × 2 = 109 bytes.

Accessing elements of arrays There is no [ ] operator in assembly language as in C. To access an element of an array, its address must be computed. E.g., array1 db 5, 4, 3, 2, 1 ; array of bytes array2 dw 5, 4, 3, 2, 1 ; array of words

Adds all the elements of array1

More advanced indirect addressing indirect addressing is often used with arrays. The most general form of an indirect memory reference is: [ base reg + factor *index reg + constant ] where: –base reg is one of the registers EAX, EBX, ECX, EDX, EBP, ESP, ESI or EDI. –factor is either 1, 2, 4 or 8. (If 1, factor is omitted.) –index reg is one of the registers EAX, EBX, ECX, EDX, EBP, ESI, EDI. (Note that ESP is not in list.) –constant is a 32-bit constant. The constant can be a label (or a label expression).

Example An example that uses an array and passes it to a function. It uses the array1c.c program (listed below) as a driver, not the driver.c program. array1c.c array1.asm

Multidimensional arrays they are represented in memory as just that, a plain one dimensional array. Two Dimensional Arrays, e.g. int a[3][2] Each row of the two dimensional array is stored contiguously in memory. The last element of a row is followed by the first element of the next row. This is known as the rowwise representation of the array and is how a C/C++ compiler would represent the array.

An example: how gcc compiles x = a[i][j] The compiler essentially converts the code to: x = (&a[0][0] + 2i + j ); and in fact, the programmer could write this way with the same result.

Dimensions above two For dimensions above two, the same basic idea is applied. int b [4][3][2]; b[i][j][k] is 6i + 2j + k. a[L][M][N] the position of element a[i][j][k] will be M × N × i + N × j + k.

For higher dimensions, For an n dimensional array of dimensions D1 to Dn, the position of element denoted by the indices i1 to in is given by the formula: D2 × D3 · · · × Dn × i1 + D3 × D4 · · · × Dn × i2 + · · · + Dn × in−1 + in

Passing Multidimensional Arrays as Parameters in C The rowwise representation of multidimensional arrays has a direct effect in C programming. void f ( int a [ ][ ] ); / no dimension information / void f ( int a [ ][2] ); void f ( int * a [ ] ); void f ( int a [ ][4][3][2] );

2.Array/String instructions Reading and writing memory The REP instruction prefix Comparision string instructions The REPx instruction prefixes Example

String instructions The 80x86 family of processors provide several instructions that are designed to work with arrays. These instructions are called string instructions. They use the index registers (ESI and EDI) to perform an operation and then to automatically increment or decrement one or both of the index registers. The direction flag in the FLAGS register determines where the index registers are incremented or decremented.

Two instructions that modify the direction flag CLD clears the direction flag. In this state, the index registers are incremented. STD sets the direction flag. In this state, the index registers are decremented. A very common mistake in 80x86 programming is to forget to explicitly put the direction flag in the correct state.

Reading and writing memory ESI is used for reading and EDI for writing. The register that holds the data is fixed (either AL, AX or EAX). The storing instructions use ES to determine the segment to write to, not DS.

Load and store example segment.data array1 dd 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 segment.bss array2 resd 10 segment.text cld;don’t forget this! mov esi, array1 mov edi, array2 mov ecx, 10 lp: lodsd stosd loop lp

The REP instruction prefix The 80x86 family provides a special instruction prefix called REP which tells the CPU to repeat the next string instruction a specified number of times. The ECX register is used to count the iterations

Comparision string instructions

Search example

The REPx instruction prefixs REPE and REPZ are just synonyms for the same prefix (as are REPNE and REPNZ). If the repeated comparison string instruction stops because of the result of the comparison, the index register or registers are still incremented and ECX decremented; however, the FLAGS register still holds the state that terminated the repetition. Thus, it is possible to use the Z flag to determine if the repeated comparisons stopped because of a comparison or ECX becoming zero.

e.g. Comparing memory blocks

Examples: void asm_copy( void * dest, const void * src, unsigned sz); void * asm_find( const void * src, char target, unsigned sz); unsigned asm_strlen( const char * ); void asm_strcpy( char * dest, const char * src);

Summary 1.(Introduction) Arrays An array is a contiguous block of list of data in memory. Each element of the list must be the same type and use exactly the same number of bytes of memory for storage. Because of these properties, arrays allow efficient access of the data by its position (or index) in the array. The address of any element can be computed by knowing three facts: 1)The address of the first element of the array. 2)The number of bytes in each element 3)The index of the element 1.Array/String instructions