Скачать презентацию Lection 5 Arrays l an array is the Скачать презентацию Lection 5 Arrays l an array is the

2_06_Lec_engl_00.ppt

  • Количество слайдов: 27

Lection 5. Arrays l an array is the structured type of information, consisting of Lection 5. Arrays l an array is the structured type of information, consisting of some number of elements of one type.

Arrays l l l In order to understand possibilities features of treatment of arrays Arrays l l l In order to understand possibilities features of treatment of arrays in the programs on an assembler, it is needed to answer on the following questions: How to describe an array in the program? How to initialize an array, that how to set the initial values of his elements? How to organize access to the array cells? How to organize arrays with a dimension more by one? How to organize implementation of model operations with arrays?

Description and initialization of array in the program l l Special facilities of description Description and initialization of array in the program l l Special facilities of description of arrays in the programs of assembler, certainly, no. If it is necessary to use an array in the program of him it is needed to design by one of the following methods: 1. By enumeration of array cells in the field of operands of one of directives of data definition. At enumeration elements are divided by commas. For example: ; array of 5 elements. Size of each elements are 4 Bytes: mas dd 1, 2, 3, 4, 5

Description and initialization of array in the program l l 1. By enumeration of Description and initialization of array in the program l l 1. By enumeration of array cells in the field of operands of one of directives of data definition. At enumeration elements are divided by commas. 2. Using the operator of reiteration of dup. For example: ; array of 5 zero elements. Size of each elements are 2 Bytes: mas l dw 5 dup (0) Such method of determination is used for reservation of memory with a purpose placing and initializations of array cells.

Description and initialization of array in the program l l 1. By enumeration of Description and initialization of array in the program l l 1. By enumeration of array cells in the field of operands of one of directives of data definition. At enumeration elements are divided by commas. 2. Using the operator of reiteration of dup. 3. Using the directives of label and rept. The pair of these directives can facilitate description of large arrays in memory and promote evident of such description. The directive of rept behaves to macro means of assembly language and the indicated number causes the reiteration one time the lines concluded between a directive and line of endm. For example, we will define an array byte in area of the memory marked by the identifier mas_b. In this case the directive of label determines the symbolic name of mas_b, like to that how it is done by the directives of reservation and initialization of memory.

Description and initialization of array in the program l Dignity of directive of label Description and initialization of array in the program l Dignity of directive of label that she does not reserve memory, and only determines descriptions of object. In this case an object is a memory cell. Using a few directives of label, written down one by one, it is possible to appropriate different names and different type to the same storage area, that it is done in the following fragment: . . . n=0. . . mas_b mas_w rept 4 dw endm label byte label word 0 f 1 f 0 h

Description and initialization of array in the program l As a result a sequence Description and initialization of array in the program l As a result a sequence from four words f 1 f 0 will be created in memory. This sequence it is possible to interpret as array a byte or words depending on that, what name of region we will use in the program - mas_b or mas_w.

Description and initialization of array in the program l l l 1. By enumeration Description and initialization of array in the program l l l 1. By enumeration of array cells in the field of operands of one of directives of data definition. At enumeration elements are divided by commas. 2. Using the operator of reiteration of dup. 3. Using the directives of label and rept. 4. Use of cycle for initialization by the values of storage which it is possible afterwards to interpret as array area. We will look on the example of listing, how it is done.

MODEL small STACK 256. data mes db 0 ah, 0 dh, 'Масив', '$' mas MODEL small STACK 256. data mes db 0 ah, 0 dh, 'Масив', '$' mas db 10 dup (? ) i db 0. code main: mov ax, @data mov ds, ax xor ax, ax mov cx, 10 mov si, 0 go: mov bh, i mov mas[si], bh inc i inc si loop go mov cx, 10 mov si, 0 mov ah, 09 h lea dx, mes int 21 h show: mov ah, 02 h mov dl, mas[si] add dl, 30 h int 21 h inc si loop show exit: mov ax, 4 c 00 h int 21 h end main

Access to the array cells l During work it is necessary expressly to imagine Access to the array cells l During work it is necessary expressly to imagine with arrays, that all array cells are disposed in memory of computer consistently. l On itself such location speaks nothing about setting and order of the use of these elements. And only a programmer determines by the algorithm of treatment made to them, how it is needed to interpret this sequence byte, making an array.

Access to the array cells l So, the same storage area can be interpreted Access to the array cells l So, the same storage area can be interpreted as 1 dimension array, and simultaneously the same information can be interpreted as 2 dimension array. l All depends only on the algorithm of these data processing in the concrete program. On itself information carry no information about the "semantic", or logical, type. Remember about this of principle moment.

Access to the array cells The same considering can be spread on the indexes Access to the array cells The same considering can be spread on the indexes of array cells. An assembler is unaware of their existence and to him absolutely however, their numeral semantic values are which. l In order to localize a certain array cell, to his name it is needed to add an index. Because we design an array, must look after about the design of index. l

Access to the array cells l In an assembly language the indexes of arrays Access to the array cells l In an assembly language the indexes of arrays are ordinary addresses, but with them work as the special appearance. In other words, when at programing on an assembler we speak about an index, rather imply here under no number of element in an array, and some address.

Access to the array cells l Lets once again we will appeal to array Access to the array cells l Lets once again we will appeal to array declaration. For example, the sequence of information is statically certain in the program: mas dw 0, 1, 2, 3, 4, 5

Access to the array cells l Let this sequence of numbers be interpreted as Access to the array cells l Let this sequence of numbers be interpreted as 1 x array. It is determined size of every element by the directive of dw, that she is equal 2 bytes. To get access to the third element, it is needed to the address of array to add 6. Numeration of array cells in an assembler begins from a zero. l That in our case the question, actually, is about a 4 th array cell - 3, but a programmer knows only about it; to the microprocessor in this case however - he needs an address only.

Access to the array cells l In general case for the receipt of address Access to the array cells l In general case for the receipt of address of element in an array it is necessary to lay down the leading (base) address of array with work of index (number of element minus unit) of this element on the size of array cell: base + (index*size of element)

Access to the array cells l Architecture of microprocessor gives comfortable enough program-hardware facilities Access to the array cells l Architecture of microprocessor gives comfortable enough program-hardware facilities for work with arrays. Base and index registers allowing to realize a few modes of addressing of information behave to them. Using these modes of addressing, it is possible to organize effective work with arrays in memory. We will remember these modes: l the indexed addressing with displacement is the mode of addressing, at which an effective address is formed from two components: permanent (base) - pointing of direct addresses of array as the name of identifier designating beginning of array; l variable (index) - pointing of the name of index register. l

Access to the array cells l For example : mas dw. . . mov Access to the array cells l For example : mas dw. . . mov 0, 1, 2, 3, 4, 5 si, 4 ; move 3 -th element of array mas into register ax: mov ax, mas[si]

Access to the array cells l l The base-displacement indexed addressing with displacement is Access to the array cells l l The base-displacement indexed addressing with displacement is the mode of addressing, at which an effective address is formed maximum from three components: l permanent (optional component), which the direct address of array as the name of identifier designating beginning of array can come forward as, or direct value; l variable (base) - pointing of the name of base register; l variable (index) - pointing of the name of index register. It is comfortably to use this type of addressing for treatment of 2 x arrays.

Access to the array cells l l A microprocessor allows to scale an index. Access to the array cells l l A microprocessor allows to scale an index. It means that if to specify after the name of index register the sign of increase of "*" with a subsequent number 2, 4 or 8, content of index register will be increased on 2, 4 or 8, that be scaled. Application of scaling facilitates work with arrays, which have the size of elements, equal 2, 4 or 8 bytes, because a microprocessor produces correction of index for the receipt of address of a next array cell. We need only to load the value of the required index in an index register (considering from 0).

MODEL STACK. data ; text mes 1 mes 2 mes 3 mas small 256 MODEL STACK. data ; text mes 1 mes 2 mes 3 mas small 256 db ‘not equal 0!$', 0 ah, 0 dh db ' equal 0!$', 0 ah, 0 dh db 0 ah, 0 dh, ‘Element $' dw 2, 7, 0, 0, 1, 9, 3, 6, 0, 8 ; array . code. 486 main: mov ax, @data mov ds, ax ; ds connect with. Data xor ax, ax ; ax=0 prepare: mov cx, 10 ; cx=10 mov esi, 0 ; index into esi compare: mov dx, mas[esi*2] ; first element of arrayin the dx cmp dx, 0 ; compare dx with 0 je equal ; jump, if is equal not_equal: ; not equal mov ah, 09 h ; output message lea dx, mes 3 int 21 h mov ah, 02 h ; output № of araay’s element mov dx, si add dl, 30 h int 21 h mov ah, 09 h lea dx, mes 1 int 21 h inc esi ; next element

dec cx jcxz exit ; cx=0? Yes — go to exit jmp compare ; dec cx jcxz exit ; cx=0? Yes — go to exit jmp compare ; no — repeat loop equal: ; is equal 0 mov ah, 09 h ; output mes 3 lea dx, mes 3 int 21 h mov ah, 02 h mov dx, si add dl, 30 h int 21 h mov ah, 09 h ; output mes 2 lea int inc dx, mes 2 21 h esi ; next element dec cx ; finish array? jcxz exit jmp compare exit: mov ax, 4 c 00 h ; exit int 21 h end main ; finish of the program

A few words about the agreements l If for description of address one register A few words about the agreements l If for description of address one register is used only, the question is about the base-displacement addressing and this register is examined as base: ; move byte from the ; adress in the register ebx: mov al, [ebx]

A few words about the agreements l If for the task of address the A few words about the agreements l If for the task of address the direct addressing (as an identifier) in combination with one register is used in a command, the question is about the indexed addressing. A register is considered index, and it is possible to use scaling for the receipt of address of necessary array cell: add eax, mas[ebx*4] ; eax + double word from ; adress mas + (ebx)*4

A few words about the agreements l l If for description of address two A few words about the agreements l l If for description of address two registers are used, the question is about the basely-indexed addressing. A left register is examined as base, and right - as index. In general case on no principle it, but if we use scaling with one of registers, he always is index. But it is better to adhere to the certain agreements. Remember that application of registers of ebp/bp and esp/sp implies by default, that the segment constituent of address is in the register of ss.

A few words about the agreements l l We will notice that the basely-indexed A few words about the agreements l l We will notice that the basely-indexed addressing is not forbidden to combine with the direct addressing or pointing of direct value. An address then will be formed as sum of all components. For example: mov ax, mas[ebx][ecx*2] ; adress of operand is equal [mas+(ebx)+(ecx)*2] . . . sub dx, [ebx+8][ecx*4] ; adress of operand is equal [(ebx)+8+(ecx)*4] l But mean that scaling is effective only then, when the dimension of array cells is equal a 2, 4 or 8 byte. If the dimension of elements is other, organizing the address to the array cells is needed in general a way, as described before.

MODEL STACK. data N=5 small 256 mas db. code main: mov xor ax, ax MODEL STACK. data N=5 small 256 mas db. code main: mov xor ax, ax mov go: mov 5 dup (3 dup (0)) ax, @data ds, ax ; обнулення ax si, 0 ; 0 у si cx, N ; N у cx dl, mas[si] ; first byte to the dl inc dl mov ; go to next element loop go mov si, 0 ; repeat loop ; number elements of array ; increment dl add si, 3 mas[si], dl ; return dl tj the array ; output to the display mov show: cx, N ; output to the display firsts bytes of mas mov dl, mas[si] add dl, 30 h mov ah, 02 h int 21 h loop show exit: mov ax, 4 c 00 h int 21 h end main