![Page 1: Assembly Lecture 16 Introduction to 8086 Implementing Arrays](https://reader034.vdocuments.mx/reader034/viewer/2022043005/626b238aaaba1e34481ff253/html5/thumbnails/1.jpg)
Introduction to 8086 Assembly Lecture 16
Implementing Arrays
![Page 2: Assembly Lecture 16 Introduction to 8086 Implementing Arrays](https://reader034.vdocuments.mx/reader034/viewer/2022043005/626b238aaaba1e34481ff253/html5/thumbnails/2.jpg)
Arrays
● A list of elements all of same size (and type)● Accessing array element
○ starting address in memory○ element size○ index○ index of first element (0 or 1?)○ no. of elements (array size)?
2100
2096
2092
2088
Memory
![Page 3: Assembly Lecture 16 Introduction to 8086 Implementing Arrays](https://reader034.vdocuments.mx/reader034/viewer/2022043005/626b238aaaba1e34481ff253/html5/thumbnails/3.jpg)
Defining arrays
● Define arrays○ In data segment (e.g. global arrays)
■ absolute address (global label)○ In stack (e.g. local arrays)
■ relative address (relative to esp or ebp)2100
2096
2092
2088
Memory
![Page 4: Assembly Lecture 16 Introduction to 8086 Implementing Arrays](https://reader034.vdocuments.mx/reader034/viewer/2022043005/626b238aaaba1e34481ff253/html5/thumbnails/4.jpg)
Global labels start address: arr1element size: 1 bytearray size: 7 elements (7 bytes)
start address: arr2element size: 2 bytesarray size: 6 elements (12 bytes)
start address: arr3element size: 4 bytesarray size: 5 elements (20 bytes)
start address: arr4element size: 4 bytesarray size: 64 elements (256 bytes)
start address: arr8element size: 8 bytesarray size: 400 elements (3200 bytes)
![Page 5: Assembly Lecture 16 Introduction to 8086 Implementing Arrays](https://reader034.vdocuments.mx/reader034/viewer/2022043005/626b238aaaba1e34481ff253/html5/thumbnails/5.jpg)
Arrays on stack (as local variable) start address: ebp-400element size: 1 bytearray size: 400 elements
OR
start address: ebp-400element size: 2 bytesarray size: 200 elements
OR
start address: ebp-400element size: 4 bytesarray size: 100 elements
Parameters
return address
init val of EBP
400 bytes
EBP
![Page 6: Assembly Lecture 16 Introduction to 8086 Implementing Arrays](https://reader034.vdocuments.mx/reader034/viewer/2022043005/626b238aaaba1e34481ff253/html5/thumbnails/6.jpg)
Access array elements
● Use indirect addressing mov al, [arr1+3]
mov ax, [arr2+2]
mov eax, [arr3+8]
mov eax, [arr3+3]
mov ecx, 12
mov dword [arr7+ecx], -200
![Page 7: Assembly Lecture 16 Introduction to 8086 Implementing Arrays](https://reader034.vdocuments.mx/reader034/viewer/2022043005/626b238aaaba1e34481ff253/html5/thumbnails/7.jpg)
Access array elements
array2.asm array3.asm
![Page 8: Assembly Lecture 16 Introduction to 8086 Implementing Arrays](https://reader034.vdocuments.mx/reader034/viewer/2022043005/626b238aaaba1e34481ff253/html5/thumbnails/8.jpg)
Exercise
● Write a function to print an array of double word integers.
array4.asm
![Page 9: Assembly Lecture 16 Introduction to 8086 Implementing Arrays](https://reader034.vdocuments.mx/reader034/viewer/2022043005/626b238aaaba1e34481ff253/html5/thumbnails/9.jpg)
Exercise
● Write a function to print an array of double word integers.
array4.asm
![Page 10: Assembly Lecture 16 Introduction to 8086 Implementing Arrays](https://reader034.vdocuments.mx/reader034/viewer/2022043005/626b238aaaba1e34481ff253/html5/thumbnails/10.jpg)
Advanced indirect addressing
mov eax, [ecx]
mov eax, [ecx + constant]
mov eax, [4 * ecx + constant]
mov eax, [ ebx + 4 * ecx + constant]
[mymovxyz eax, ebx, 4, ecx, immed]
![Page 11: Assembly Lecture 16 Introduction to 8086 Implementing Arrays](https://reader034.vdocuments.mx/reader034/viewer/2022043005/626b238aaaba1e34481ff253/html5/thumbnails/11.jpg)
Advanced indirect addressing
[ base-reg + scale * index-reg + constant ]
scale: 1,2,4,8
base-reg: EAX, EBX, ECX, EDX, EBP, ESP, ESI, EDI
index-reg: EAX, EBX, ECX, EDX, EBP, ESI, EDI (not ESP)
constant: label or immediate
![Page 12: Assembly Lecture 16 Introduction to 8086 Implementing Arrays](https://reader034.vdocuments.mx/reader034/viewer/2022043005/626b238aaaba1e34481ff253/html5/thumbnails/12.jpg)
Advanced indirect addressingarray5.asm array3.asm
![Page 13: Assembly Lecture 16 Introduction to 8086 Implementing Arrays](https://reader034.vdocuments.mx/reader034/viewer/2022043005/626b238aaaba1e34481ff253/html5/thumbnails/13.jpg)
local variables/arrays
![Page 14: Assembly Lecture 16 Introduction to 8086 Implementing Arrays](https://reader034.vdocuments.mx/reader034/viewer/2022043005/626b238aaaba1e34481ff253/html5/thumbnails/14.jpg)
local variables/arrays
![Page 15: Assembly Lecture 16 Introduction to 8086 Implementing Arrays](https://reader034.vdocuments.mx/reader034/viewer/2022043005/626b238aaaba1e34481ff253/html5/thumbnails/15.jpg)
local variables/arrays
immediate
![Page 16: Assembly Lecture 16 Introduction to 8086 Implementing Arrays](https://reader034.vdocuments.mx/reader034/viewer/2022043005/626b238aaaba1e34481ff253/html5/thumbnails/16.jpg)
local variables/arrays
return address
pushed EBP
a (400 bytes)
EBPk (4 bytes)
j (4 bytes)
array6.asm
![Page 17: Assembly Lecture 16 Introduction to 8086 Implementing Arrays](https://reader034.vdocuments.mx/reader034/viewer/2022043005/626b238aaaba1e34481ff253/html5/thumbnails/17.jpg)
local variables/arraysarray6.asm
![Page 18: Assembly Lecture 16 Introduction to 8086 Implementing Arrays](https://reader034.vdocuments.mx/reader034/viewer/2022043005/626b238aaaba1e34481ff253/html5/thumbnails/18.jpg)
local variables/arrays
array6.asm
![Page 19: Assembly Lecture 16 Introduction to 8086 Implementing Arrays](https://reader034.vdocuments.mx/reader034/viewer/2022043005/626b238aaaba1e34481ff253/html5/thumbnails/19.jpg)
load effective address
array6.asm array7.asm
![Page 20: Assembly Lecture 16 Introduction to 8086 Implementing Arrays](https://reader034.vdocuments.mx/reader034/viewer/2022043005/626b238aaaba1e34481ff253/html5/thumbnails/20.jpg)
load effective address
array6.asm array7.asm
address generation unit (AGU)
![Page 21: Assembly Lecture 16 Introduction to 8086 Implementing Arrays](https://reader034.vdocuments.mx/reader034/viewer/2022043005/626b238aaaba1e34481ff253/html5/thumbnails/21.jpg)
final programarray7.asm
![Page 22: Assembly Lecture 16 Introduction to 8086 Implementing Arrays](https://reader034.vdocuments.mx/reader034/viewer/2022043005/626b238aaaba1e34481ff253/html5/thumbnails/22.jpg)
load effective address
mov reg, [ base-reg + scale * index-reg + constant ]
reg = *(base-reg + scale * index-reg + constant)
lea reg, [ base-reg + scale * index-reg + constant ]
reg = base-reg + scale * index-reg + constant
![Page 23: Assembly Lecture 16 Introduction to 8086 Implementing Arrays](https://reader034.vdocuments.mx/reader034/viewer/2022043005/626b238aaaba1e34481ff253/html5/thumbnails/23.jpg)
get address of local variables / arrays
● storing a pointer to a local variable ● pushing on stack for function call
![Page 24: Assembly Lecture 16 Introduction to 8086 Implementing Arrays](https://reader034.vdocuments.mx/reader034/viewer/2022043005/626b238aaaba1e34481ff253/html5/thumbnails/24.jpg)
get address of local variables / arrays
● storing a pointer to a local variable ● pushing on stack for function call
![Page 25: Assembly Lecture 16 Introduction to 8086 Implementing Arrays](https://reader034.vdocuments.mx/reader034/viewer/2022043005/626b238aaaba1e34481ff253/html5/thumbnails/25.jpg)
get address of local variables / arrays
● storing a pointer to a local variable ● pushing on stack for function call
return address
init val of EBP
iEBP
j
p
![Page 26: Assembly Lecture 16 Introduction to 8086 Implementing Arrays](https://reader034.vdocuments.mx/reader034/viewer/2022043005/626b238aaaba1e34481ff253/html5/thumbnails/26.jpg)
get address of local variables / arrays
● storing a pointer to a local variable ● pushing on stack for function call
return address
init val of EBP
i
j
p
EBP
assuming 32-bit addressing (pointers are 32 bits long)
![Page 27: Assembly Lecture 16 Introduction to 8086 Implementing Arrays](https://reader034.vdocuments.mx/reader034/viewer/2022043005/626b238aaaba1e34481ff253/html5/thumbnails/27.jpg)
fast computations
lea EAX, [ EAX + 4 * EAX ]
![Page 28: Assembly Lecture 16 Introduction to 8086 Implementing Arrays](https://reader034.vdocuments.mx/reader034/viewer/2022043005/626b238aaaba1e34481ff253/html5/thumbnails/28.jpg)
fast computations
lea EAX, [ EAX + 4 * EAX ] EAX *= 5
![Page 29: Assembly Lecture 16 Introduction to 8086 Implementing Arrays](https://reader034.vdocuments.mx/reader034/viewer/2022043005/626b238aaaba1e34481ff253/html5/thumbnails/29.jpg)
fast computations
lea EAX, [ EAX + 4 * EAX ] EAX *= 5
???? EAX *= 6
![Page 30: Assembly Lecture 16 Introduction to 8086 Implementing Arrays](https://reader034.vdocuments.mx/reader034/viewer/2022043005/626b238aaaba1e34481ff253/html5/thumbnails/30.jpg)
fast computations
lea EAX, [ EAX + 4 * EAX ] EAX *= 5
lea EAX, [ EAX + 5 * EAX ] EAX *= 6
![Page 31: Assembly Lecture 16 Introduction to 8086 Implementing Arrays](https://reader034.vdocuments.mx/reader034/viewer/2022043005/626b238aaaba1e34481ff253/html5/thumbnails/31.jpg)
fast computations
lea EAX, [ EAX + 4 * EAX ] EAX *= 5
lea EAX, [ EAX + 5 * EAX ] EAX *= 6
![Page 32: Assembly Lecture 16 Introduction to 8086 Implementing Arrays](https://reader034.vdocuments.mx/reader034/viewer/2022043005/626b238aaaba1e34481ff253/html5/thumbnails/32.jpg)
fast computations
lea EAX, [ EAX + 4 * EAX ] EAX *= 5
lea EAX, [ EAX + 5 * EAX ] EAX *= 6
[ base-reg + scale * index-reg + constant ]
scale: 1,2,4,8
![Page 33: Assembly Lecture 16 Introduction to 8086 Implementing Arrays](https://reader034.vdocuments.mx/reader034/viewer/2022043005/626b238aaaba1e34481ff253/html5/thumbnails/33.jpg)
fast computations
lea EAX, [ EAX + 4 * EAX ] EAX *= 5
lea EAX, [ EAX + 5 * EAX ] EAX *= 6
lea EAX, [ EAX + 2 * EAX ]sal EAX
![Page 34: Assembly Lecture 16 Introduction to 8086 Implementing Arrays](https://reader034.vdocuments.mx/reader034/viewer/2022043005/626b238aaaba1e34481ff253/html5/thumbnails/34.jpg)
fast computations
lea EAX, [ EAX + 4 * EAX ] EAX *= 5
lea EAX, [ EAX + 5 * EAX ] EAX *= 6
lea EAX, [ EAX + 8 * EAX ]lea EAX, [ EAX + 4 * EAX ]
![Page 35: Assembly Lecture 16 Introduction to 8086 Implementing Arrays](https://reader034.vdocuments.mx/reader034/viewer/2022043005/626b238aaaba1e34481ff253/html5/thumbnails/35.jpg)
fast computations
lea EAX, [ EAX + 4 * EAX ] EAX *= 5
lea EAX, [ EAX + 5 * EAX ] EAX *= 6
lea EAX, [ EAX + 8 * EAX ]lea EAX, [ EAX + 4 * EAX ] EAX *= 45
![Page 36: Assembly Lecture 16 Introduction to 8086 Implementing Arrays](https://reader034.vdocuments.mx/reader034/viewer/2022043005/626b238aaaba1e34481ff253/html5/thumbnails/36.jpg)
Arrays in inline assemblyvoid printArray(const int a[], int n) { for (int i = 0; i < n; i++) printf("%d, ", a[i]); putchar('\n');}
int main() { int array[10] = {1,2,3,4,5,6,7,8,9,10}; printArray(array,10);
for (int i = 0; i < 10; i++) { asm volatile ("mov eax, [ebx+4*esi];" "lea eax, [eax+8*eax];" "mov [ebx+4*esi], eax" : : "b" (array), "S" (i) : "memory", "eax"); }
printArray(array,10);}
array9.c
![Page 37: Assembly Lecture 16 Introduction to 8086 Implementing Arrays](https://reader034.vdocuments.mx/reader034/viewer/2022043005/626b238aaaba1e34481ff253/html5/thumbnails/37.jpg)
Arrays in inline assemblyvoid printArray(const int a[], int n) { for (int i = 0; i < n; i++) printf("%d, ", a[i]); putchar('\n');}
int main() { int array[10] = {1,2,3,4,5,6,7,8,9,10}; printArray(array,10);
for (int i = 0; i < 10; i++) { asm volatile ("mov eax, [ebx+4*esi];" "lea eax, [eax+8*eax];" "mov [ebx+4*esi], eax" : : "b" (array), "S" (i) : "memory", "eax"); }
printArray(array,10);}
array9.c