70 likes | 200 Views
Two Dimensional Arrays. Two operand types, base-index and base-index-displacement, work well when dealing with two-dimensional arrays. Base-Index Operands. Adds the values of two registers (base and index), producing an offset address.
E N D
Two Dimensional Arrays Two operand types, base-index and base-index-displacement, work well when dealing with two-dimensional arrays.
Base-Index Operands • Adds the values of two registers (base and index), producing an offset address. • Any two general purpose registers may be used in 32-bit mode(in 16-bit mode, only bx with di or si, and bp with di or si) .data array WORD 1000h, 2000h, 3000h .code mov ebx, OFFSET array mov esi, 2 mov ax, [bx + si] ;ax = 2000h
Table Example • For two dimensional tables, a base register usually contains a row offset, and an index register contains a column offset. • Defining a table that has three rows and five columns: tableB BYTE 10h, 20h, 30h, 40h, 50h BYTE 60h, 70h, 80h, 90h, 0A0h BYTE 0B0h, 0C0h,0D0h, 0E0h, 0F0h NumCols = 5
Memory • In memory this table is a continuous stram of bytes as if it were a one-dimensional array. • The physical storage of the array is in row-major order, where the last byte in the first row is followed by the first byte in the second row, and so on.
How to move through the tableAssuming that the rows begin with 0, Columns begin with 0. RowNumber = 1 Column Number =2 Mov ebx, OFFSET tableB Add ebx, NumCols * RowNumber Mov esi, ColumnNumber Mov al, [ebx + esi] ;AL = 80h
Looking at Memory Locations • Move into BX, OFFSET of tableB • Each row is 5 bytes long (NumCols) • Data is on 1st row (row nums begins with 0) • Multiply Rownumber * NumCols • Add result to bx. • Move into SI, ColumnNumber (data is in 2nd column) • Move into al, value