pobieranie; pdf; ebook; download; do ÂściÂągnięcia
 
Cytat
Felicitas multos habet amicos - szczęście ma wielu przyjaciół.
Indeks Eddings_Dav D20021169 arteuza
 
  Witamy


[ Pobierz całość w formacie PDF ]

should be a multiple of four (112 in this case) to keep ESP on a double word
boundary. One could arrange the variables inside this 109 bytes in several
ways. Figure 5.2 shows two possible ways. The unused part of the first
ordering is there to keep the double words on double word boundaries to
speed up memory accesses.
5.1.2 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. Consider the following
two array definitions:
array1 db 5, 4, 3, 2, 1 ; array of bytes
array2 dw 5, 4, 3, 2, 1 ; array of words
Here are some examples using this arrays:
5.1. INTRODUCTION 97
EBP - 1 char
unused
EBP - 8 dword 1
EBP - 12 dword 2 word
array
word
array EBP - 100
EBP - 104 dword 1
EBP - 108 dword 2
EBP - 109 char
EBP - 112 unused
Figure 5.2: Arrangements of the stack
1 mov al, [array1] ; al = array1[0]
2 mov al, [array1 + 1] ; al = array1[1]
3 mov [array1 + 3], al ; array1[3] = al
4 mov ax, [array2] ; ax = array2[0]
5 mov ax, [array2 + 2] ; ax = array2[1] (NOT array2[2]!)
6 mov [array2 + 6], ax ; array2[3] = ax
7 mov ax, [array2 + 1] ; ax = ??
In line 5, element 1 of the word array is referenced, not element 2. Why?
Words are two byte units, so to move to the next element of a word array,
one must move two bytes ahead, not one. Line 7 will read one byte from the
first element and one from the second. In C, the compiler looks at the type
of a pointer in determining how many bytes to move in an expression that
uses pointer arithmetic so that the programmer does not have to. However,
in assembly, it is up to the programmer to take the size of array elements in
account when moving from element to element.
Figure 5.3 shows a code snippet that adds all the elements ofarray1
in the previous example code. In line 7, AX is added to DX. Why not
AL? First, the two operands of theADDinstruction must be the same size.
Secondly, it would be easy to add up bytes and get a sum that was too big
to fit into a byte. By using DX, sums up to 65,535 are allowed. However, it
is important to realize that AH is being added also. This is why AH is set
to zero1 in line 3.
Figures 5.4 and 5.5 show two alternative ways to calculate the sum. The
lines in italics replace lines 6 and 7 of Figure 5.3.
1
Setting AH to zero is implicitly assuming that AL is an unsigned number. If it is
signed, the appropriate action would be to insert aCBWinstruction between lines 6 and 7
98 CHAPTER 5. ARRAYS
1 mov ebx, array1 ; ebx = address of array1
2 mov dx, 0 ; dx will hold sum
3 mov ah, 0 ; ?
4 mov ecx, 5
5 lp:
6 mov al, [ebx] ; al = *ebx
7 add dx, ax ; dx += ax (not al!)
8 inc ebx ; bx++
9 loop lp
Figure 5.3: Summing elements of an array (Version 1)
1 mov ebx, array1 ; ebx = address of array1
2 mov dx, 0 ; dx will hold sum
3 mov ecx, 5
4 lp:
5 add dl, [ebx] ; dl += *ebx
6 jnc next ; if no carry goto next
7 inc dh ; inc dh
8 next:
9 inc ebx ; bx++
10 loop lp
Figure 5.4: Summing elements of an array (Version 2)
5.1.3 More advanced indirect addressing
Not surprisingly, 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.)
5.1. INTRODUCTION 99
1 mov ebx, array1 ; ebx = address of array1
2 mov dx, 0 ; dx will hold sum
3 mov ecx, 5
4 lp:
5 add dl, [ebx] ; dl += *ebx
6 adc dh, 0 ; dh += carry flag + 0
7 inc ebx ; bx++
8 loop lp
Figure 5.5: Summing elements of an array (Version 3)
constant is a 32-bit constant. The constant can be a label (or a label
expression).
5.1.4 Example
Here is an example that uses an array and passes it to a function. It
uses thearray1c.cprogram (listed below) as a driver, not thedriver.c
program.
array1.asm
1 %define ARRAY_SIZE 100
2 %define NEW_LINE 10
3
4 segment .data
5 FirstMsg db "First 10 elements of array", 0
6 Prompt db "Enter index of element to display: ", 0
7 SecondMsg db "Element %d is %d", NEW_LINE, 0
8 ThirdMsg db "Elements 20 through 29 of array", 0
9 InputFormat db "%d", 0
10
11 segment .bss
12 array resd ARRAY_SIZE
13
14 segment .text
15 extern _puts, _printf, _scanf, _dump_line
16 global _asm_main
17 _asm_main:
18 enter 4,0 ; local dword variable at EBP - 4
19 push ebx
20 push esi
21
100 CHAPTER 5. ARRAYS [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • natalcia94.xlx.pl
  • comp
    IndeksHollywood Hills Holly and Alexa_Book 3 Aimee Friedman12th Generation Book of ShadowsE book Odplyw Netpress DigitalBook_of_Five_RingsMackenzie Myrna Harlequin Romans 1064 SśÂ‚odycz śźyciaCities in Flight James Blish1082. McMahon Barbara Escape Around the World 1 Podróśź w chmurachGordon Dickson Dragon 02 The Dragon Knight (v1.4)Heather Killough Walden [Syndicate] Redeemer (pdf)Surrender 1 Surrender Melody Anne
  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • own-team.pev.pl
  • Cytat

    Długi język ma krótkie nogi. Krzysztof Mętrak
    Historia kroczy dziwnymi grogami. Grecy uczyli się od Trojan, uciekinierzy z Troi założyli Rzym, a Rzymianie podbili Grecję, po to jednak, by przejąć jej kulturę. Erik Durschmied
    A cruce salus - z krzyża (pochodzi) zbawienie.
    A ten zwycięzcą, kto drugim da / Najwięcej światła od siebie! Adam Asnyk, Dzisiejszym idealistom
    Ja błędy popełniam nieustannie, ale uważam, że to jest nieuniknione i nie ma co się wobec tego napinać i kontrolować, bo przestanę być normalnym człowiekiem i ze spontanicznej osoby zmienię się w poprawną nauczycielkę. Jeżeli mam uczyć dalej, to pod warunkiem, że będę sobą, ze swoimi wszystkimi głupotami i mądrościami, wadami i zaletami. s. 87 Zofia Kucówna - Zdarzenia potoczne

    Valid HTML 4.01 Transitional

    Free website template provided by freeweblooks.com