assembly lecture 16 introduction to 8086 implementing arrays

37
Introduction to 8086 Assembly Lecture 16 Implementing Arrays

Upload: others

Post on 29-Apr-2022

18 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Assembly Lecture 16 Introduction to 8086 Implementing Arrays

Introduction to 8086 Assembly Lecture 16

Implementing Arrays

Page 2: Assembly Lecture 16 Introduction to 8086 Implementing Arrays

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

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

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

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

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

Access array elements

array2.asm array3.asm

Page 8: Assembly Lecture 16 Introduction to 8086 Implementing Arrays

Exercise

● Write a function to print an array of double word integers.

array4.asm

Page 9: Assembly Lecture 16 Introduction to 8086 Implementing Arrays

Exercise

● Write a function to print an array of double word integers.

array4.asm

Page 10: Assembly Lecture 16 Introduction to 8086 Implementing Arrays

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

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

Advanced indirect addressingarray5.asm array3.asm

Page 13: Assembly Lecture 16 Introduction to 8086 Implementing Arrays

local variables/arrays

Page 14: Assembly Lecture 16 Introduction to 8086 Implementing Arrays

local variables/arrays

Page 15: Assembly Lecture 16 Introduction to 8086 Implementing Arrays

local variables/arrays

immediate

Page 16: Assembly Lecture 16 Introduction to 8086 Implementing Arrays

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

local variables/arraysarray6.asm

Page 18: Assembly Lecture 16 Introduction to 8086 Implementing Arrays

local variables/arrays

array6.asm

Page 19: Assembly Lecture 16 Introduction to 8086 Implementing Arrays

load effective address

array6.asm array7.asm

Page 20: Assembly Lecture 16 Introduction to 8086 Implementing Arrays

load effective address

array6.asm array7.asm

address generation unit (AGU)

Page 21: Assembly Lecture 16 Introduction to 8086 Implementing Arrays

final programarray7.asm

Page 22: Assembly Lecture 16 Introduction to 8086 Implementing Arrays

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

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

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

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

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

fast computations

lea EAX, [ EAX + 4 * EAX ]

Page 28: Assembly Lecture 16 Introduction to 8086 Implementing Arrays

fast computations

lea EAX, [ EAX + 4 * EAX ] EAX *= 5

Page 29: Assembly Lecture 16 Introduction to 8086 Implementing Arrays

fast computations

lea EAX, [ EAX + 4 * EAX ] EAX *= 5

???? EAX *= 6

Page 30: Assembly Lecture 16 Introduction to 8086 Implementing Arrays

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

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

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

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

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

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

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

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