CSC 221 Computer Organization and Assembly Language Lecture 27: 2-Dimensional Arrays + Structures.

Slides:



Advertisements
Similar presentations
CSC 221 Computer Organization and Assembly Language Lecture 21: Conditional and Block Structures: Assembly Programs.
Advertisements

Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
ICS312 Set 6 Operands. Basic Operand Types (1) Register Operands. An operand that refers to a register. MOV AX, BX ; moves contents of register BX to.
Irvine, Kip R. Assembly Language For Intel-Based Computers TYPE and SIZE Operators TYPE –returns the size, in bytes of a single element of a data label.
Assembly Language for Intel-Based Computers Chapter 8: Advanced Procedures Kip R. Irvine.
CS2422 Assembly Language and System Programming Structure and Macros Department of Computer Science National Tsing Hua University.
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, 5 th Edition Chapter 9: Strings and Arrays (c) Pearson Education, All rights reserved. You may.
Assembly Language for Intel-Based Computers Chapter 5: Procedures Kip R. Irvine.
CS2422 Assembly Language & System Programming October 3, 2006.
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Assembly Language for Intel-Based Computers
CS2422 Assembly Language & System Programming October 26, 2006.
Assembly Language for Intel-Based Computers, 4th Edition
Irvine, Kip R. Assembly Language For Intel-Based Computers.data string db "This is a string." COUNT = ($–string) ; calculate string length.code mov cx,COUNT.
1 Structure A template or pattern given to a logically related group of variables. field - structure member containing data Program access to a structure:
INVOKE Directive The INVOKE directive is a powerful replacement for Intel’s CALL instruction that lets you pass multiple arguments Syntax: INVOKE procedureName.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Defining and Using Procedures Creating Procedures.
Web siteWeb site ExamplesExamples Irvine, Kip R. Assembly Language for Intel-Based Computers, Stack Operations Runtime Stack PUSH Operation POP.
CS2422 Assembly Language and System Programming High-Level Language Interface Department of Computer Science National Tsing Hua University.
Addressing Modes Chapter 11 S. Dandamudi To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer,  S.
Sahar Mosleh California State University San MarcosPage 1 Applications of Shift and Rotate Instructions.
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
Assembly Language for x86 Processors 6th Edition Chapter 4: Data-Related Operators and Directives, Addressing Modes (c) Pearson Education, All rights.
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 8: Advanced Procedures (c) Pearson Education, All rights reserved. You may.
The x86 Architecture Lecture 15 Fri, Mar 4, 2005.
Today's topics Multi-dimensional arrays Multi-dimensional arrays String processing String processing Macros Macros.
CSC 221 Computer Organization and Assembly Language Lecture 12: Addressing Modes in Assembly.
COS2014: Assembly Language Programming Chapter 9: Strings and Arrays (c) Pearson Education, All rights reserved. You may modify and copy this slide.
Today’s topics Procedures Procedures Passing values to/from procedures Passing values to/from procedures Saving registers Saving registers Documenting.
Assembly Language for Intel-Based Computers Chapter 7: Integer Arithmetic (c) Pearson Education, All rights reserved. You may modify and copy.
Assembly Language for x86 Processors 7th Edition Chapter 13: High-Level Language Interface (c) Pearson Education, All rights reserved. You may modify.
Sahar Mosleh California State University San MarcosPage 1 Stack operations, Applications and defining procedures.
CSC 221 Computer Organization and Assembly Language
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.
Assembly Language for Intel-Based Computers, 5 th Edition Chapter 10: Structures and Macros (c) Pearson Education, All rights reserved. You.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 5: Procedures Lecture 19: Procedures Procedure’s parameters (c) Pearson Education, 2002.
Computer Organization & Assembly Language University of Sargodha, Lahore Campus Prepared by Ali Saeed.
Lecture 17 Structures and Macros Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
CSC 221 Computer Organization and Assembly Language Lecture 16: Procedures.
CSC 221 Computer Organization and Assembly Language Lecture 11: Addressing Modes in Assembly.
CSC 221 Computer Organization and Assembly Language Lecture 15: STACK Related Instructions.
CSC 221 Computer Organization and Assembly Language Lecture 20: Conditional and Block Structures.
Assembly Language for Intel-Based Computers, 4 th Edition Lecture 22: Conditional Loops (c) Pearson Education, All rights reserved. You may modify.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
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.
1 Using the Assembler Chapter – 4(A). 2 Exchanging Two Variables title Exchange Two Variables (Exchange.asm).model small.stack 100h.data value1 db 0Ah.
Lecture 15 Advanced Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
CSC 221 Computer Organization and Assembly Language
Format of Assembly language
Assembly Language for x86 Processors 7th Edition
Assembly Language for x86 Processors 6th Edition
Additional Assembly Programming Concepts
Microprocessor and Assembly Language
Symbolic Instruction and Addressing
BIC 10503: COMPUTER ARCHITECTURE
Data-Related Operators and Directives
Structs.
Assembly Language for Intel-Based Computers, 4th Edition
Morgan Kaufmann Publishers Computer Organization and Assembly Language
Computer Organization and Assembly Languages Yung-Yu Chuang 2005/12/01
Symbolic Instruction and Addressing
Chapter 3: Addressing Modes
Symbolic Instruction and Addressing
Assembly Language for Intel-Based Computers, 5th Edition
Miscellaneous Topics.
Chapter 6 –Symbolic Instruction and Addressing
Computer Organization and Assembly Language
Presentation transcript:

CSC 221 Computer Organization and Assembly Language Lecture 27: 2-Dimensional Arrays + Structures

Lecture 26: Review Assembly Implementation of: Stack Parameters –INVOKE Directive –PROC Directive –PROTO Directive –Passing by Value or by Reference –Example: Exchanging Two Integers

Lecture 26: Review Assembly Implementation of: Stack Frames –Explicit Access to Stack Parameters –Passing Arguments by Reference (cont.)

Lecture Outline Two Dimensional Arrays –Basic Concept –2-D Array Representation –Base-Index Operands Row-Sum Example –Base-Index Displacement –Bubble Sort Algorithm Structures

Two-Dimensional Arrays Basic Concept Base-Index Operands Base-Index Displacement

Basic Concepts From an assembly language programmer’s perspective, a two-dimensional array is a high-level abstraction of a one-dimensional array. One of two methods of arranging the rows and columns in memory: row-major order and column-major order. Logical Arrangement Row-major: (Most Common) Col.-major: Order

Base-Index Operand A base-index operand adds the values of two registers (called base and index), producing an effective address. [base + index] The square brackets are required. In 32-bit mode, any two 32-bit general-purpose registers may be used as base and index registers. In 16-bit mode, the base register must be BX or BP, and the index register must be SI or DI.

Base-Index Operand The following are examples of various combinations of base and index operands in 32-bit mode:.data array WORD 1000h,2000h,3000h.code mov ebx,OFFSET array mov esi,2 mov ax,[ebx+esi] ; AX = 2000h mov edi,OFFSET array mov ecx,4 mov ax,[edi+ecx] ; AX = 3000h mov ebp,OFFSET array mov esi,0 mov ax,[ebp+esi] ; AX = 1000h

Structure Application Base-index operands are great for accessing arrays of structures. (A structure groups together data under a single name.) A common application of base-index addressing has to do with addressing arrays of structures. The following defines a structure named COORD containing X and Y screen coordinates: COORD STRUCT X WORD ?; offset 00 Y WORD ?; offset 02 COORD ENDS.data setOfCoordinates COORD 10 DUP(<>) Then we can define an array of COORD objects:

Structure Application The following code loops through the array and displays each Y-coordinate: mov ebx,OFFSET setOfCoordinates mov esi,2; offset of Y value mov eax,0 L1:mov ax,[ebx+esi] invoke dwtoa, eax, addr DispDec invoke StdOut, addr DispDec add ebx,SIZEOF COORD loop L1

Two-Dimensional Array Example Imagine a table with three rows and five columns. The data can be arranged in any format on the page: table BYTE 10h, 20h, 30h, 40h, 50h BYTE 60h, 70h, 80h, 90h, 0A0h BYTE 0B0h, 0C0h, 0D0h, 0E0h, 0F0h NumCols = 5 table BYTE 10h,20h,30h,40h,50h,60h,70h, 80h,90h,0A0h, 0B0h,0C0h,0D0h, 0E0h,0F0h NumCols = 5 Alternative format:

Accessing Two-Dimensional Array Accessing a two-dimensional array in row-major order: Row offset is held in the base register Column offset is in the index register. Example: Following Table has 3 rows and 5 columns: tableB BYTE 10h, 20h, 30h, 40h, 50h BYTE 60h, 70h, 80h, 90h, 0A0h BYTE 0B0h, 0C0h, 0D0h, 0E0h, 0F0h Rowsize = 5

Two-Dimensional Array In row-major order: Rowsize is calculated by the assembler as the number of bytes in each table row. Access: Element at Row 1 and Column 2: tableB(1,2) EBX = table’s offset + Row Offset = EBX + (Rowsize * row_index) ESI = Column index row_index = 1 column_index = 2 mov ebx,OFFSET tableB ; table offset add ebx,RowSize * row_index ; row offset mov esi,column_index mov al,[ebx + esi] ; AL = 80h (cont.)

Two-Dimensional Array row_index = 1 column_index = 2 mov ebx,OFFSET tableB ; table offset add ebx,RowSize * row_index ; row offset mov esi,column_index mov al,[ebx + esi] ; AL = 80h (cont.) tableB OFFSET

Calculating Row-Sum.data tableB BYTE 10h, 20h, 30h, 40h, 50h BYTE 60h, 70h, 80h, 90h, 0A0h BYTE 0B0h, 0C0h, 0D0h, 0E0h, 0F0h RowSize = 5 Disp1 BYTE 20 DUP(?) msg2 BYTE "The sum is: ",0.code start: movedx,1; Row number movebx,OFFSET tableB movecx,RowSize callcalc_row_sum; EAX = sum invoke StdOut, addr msg2 ; "The sum is:” invoke dwtoa, eax, addr Disp1 invoke StdOut, addr Disp1 ……

Calculating Row-Sum ; calc_row_sum PROC uses ebx ecx edx esi ; ; Calculates the sum of a row in a byte matrix. ; Receives: EBX = table offset, EAX = row index, ; ECX = row size, in bytes. ; Returns: EAX holds the sum. ; mulecx; row index * row size addebx,eax; row offset moveax,0; accumulator movesi,0; column index L1:movzx edx,BYTE PTR[ebx + esi] ; get a byte addeax,edx ; add to accumulator incesi ; next byte in row loopL1 ret calc_row_sum ENDP

Base-Index-Displacement Operand A base-index-displacement operand adds base and index registers to a constant, producing an effective address. Displacement can be the name of a variable or a constant expression. Any two 32-bit general-purpose registers may be used. Common formats: [ base + index + displacement ] displacement [ base + index ]

Two-Dimensional Table Example The following code loads the table element stored in row 1, column 2: RowNumber = 1 ColumnNumber = 2 mov ebx,NumCols * RowNumber mov esi,ColumnNumber mov al,table[ebx + esi]

Sorting Integer Arrays Bubble Sort –A simple sorting algorithm that works well for small arrays

Bubble Sort Each pair of adjacent values is compared, and exchanged if the values are not ordered correctly:

Bubble Sort Pseudocode N = array size, cx1 = outer loop counter, cx2 = inner loop counter: cx1 = N - 1 while( cx1 > 0 ) { esi = addr(array) cx2 = cx1 while( cx2 > 0 ) { if( array[esi] < array[esi+4] ) exchange( array[esi], array[esi+4] ) add esi,4 dec cx2 } dec cx1 }

Bubble Sort Implementation BubbleSort PROC USES eax ecx esi, pArray:PTR DWORD,Count:DWORD mov ecx,Count dec ecx; decrement count by 1 L1:push ecx; save outer loop count mov esi,pArray; point to first value L2:mov eax,[esi]; get array value cmp [esi+4],eax; compare a pair of values jge L3; if [esi] <= [edi], skip xchg eax,[esi+4]; else exchange the pair mov [esi],eax L3:add esi,4; move both pointers forward loop L2; inner loop pop ecx; retrieve outer loop count loop L1; else repeat outer loop L4:ret BubbleSort ENDP

Structures - Overview Defining Structures Declaring Structure Variables Referencing Structure Variables

Structure A template or pattern given to a logically related group of variables. field - structure member containing data Program access to a structure: –entire structure as a complete unit –individual fields Useful way to pass multiple related arguments to a procedure –example: file directory information

Using a Structure Using a structure involves three sequential steps: 1. Define the structure. 2. Declare one or more variables of the structure type, called structure variables. 3. Write runtime instructions that access the structure.

Structure Definition Syntax name STRUCT field-declarations name ENDS Field-declarations are identical to variable declarations

COORD Structure The COORD structure used by the MS-Windows programming library identifies X and Y screen coordinates COORD STRUCT X WORD ? ; offset 00 Y WORD ? ; offset 02 COORD ENDS

Employee Structure Employee STRUCT IdNum BYTE " " LastName BYTE 30 DUP(0) Years WORD 0 SalaryHistory DWORD 0,0,0,0 Employee ENDS A structure is ideal for combining fields of different types:

Declaring Structure Variables Structure name is a user-defined type Insert replacement initializers between brackets: Empty brackets <> retain the structure's default field initializers Examples:.data point1 COORD point2 COORD <> worker Employee <>

Initializing Array Fields Use the DUP operator to initialize one or more elements of an array field:.data emp Employee

Array of Structures An array of structure objects can be defined using the DUP operator. Initializers can be used NumPoints = 3 AllPoints COORD NumPoints DUP( ) RD_Dept Employee 20 DUP(<>) accounting Employee 10 DUP( )

Referencing Structure Variables.data worker Employee <> mov eax,TYPE Employee ; 57 mov eax,SIZEOF Employee ; 57 mov eax,SIZEOF worker ; 57 mov eax,TYPE Employee.SalaryHistory ; 4 mov eax,LENGTHOF Employee.SalaryHistory ; 4 mov eax,SIZEOF Employee.SalaryHistory ; 16 Employee STRUCT; bytes IdNum BYTE " "; 9 LastName BYTE 30 DUP(0); 30 Years WORD 0; 2 SalaryHistory DWORD 0,0,0,0; 16 Employee ENDS; 57

... continued mov dx,worker.Years mov worker.SalaryHistory,20000 ; first salary mov worker.SalaryHistory+4,30000 ; second salary mov edx,OFFSET worker.LastName mov esi,OFFSET worker mov ax,(Employee PTR [esi]).Years mov ax,[esi].Years ; invalid operand (ambiguous)

Looping Through an Array of Points.data NumPoints = 3 AllPoints COORD NumPoints DUP( ).code mov edi,0; array index mov ecx,NumPoints; loop counter mov ax,1; starting X, Y values L1: mov (COORD PTR AllPoints[edi]).X,ax mov (COORD PTR AllPoints[edi]).Y,ax add edi,TYPE COORD inc ax Loop L1 Sets the X and Y coordinates of the AllPoints array to sequentially increasing values (1,1), (2,2),...

Summary Two Dimensional Arrays –Basic Concept –2-D Array Representation –Base-Index Operands Row-Sum Example –Base-Index Displacement –Bubble Sort Algorithm Structures

Reference Most of the Slides are taken from Presentation: Chapter 9 & 10 Assembly Language for Intel-Based Computers, 4 th Edition Kip R. Irvine (c) Pearson Education, All rights reserved. You may modify and copy this slide show for your personal use, or for use in the classroom, as long as this copyright statement, the author's name, and the title are not changed.