SlideShare a Scribd company logo
1 of 37
Download to read offline
Introduction to 8086
Assembly
Lecture 16
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
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
Global labels start address: arr1
element size: 1 byte
array size: 7 elements (7 bytes)
start address: arr2
element size: 2 bytes
array size: 6 elements (12 bytes)
start address: arr3
element size: 4 bytes
array size: 5 elements (20 bytes)
start address: arr4
element size: 4 bytes
array size: 64 elements (256 bytes)
start address: arr8
element size: 8 bytes
array size: 400 elements (3200 bytes)
Arrays on stack (as local variable)
start address: ebp-400
element size: 1 byte
array size: 400 elements
OR
start address: ebp-400
element size: 2 bytes
array size: 200 elements
OR
start address: ebp-400
element size: 4 bytes
array size: 100 elements
Parameters
return address
init val of EBP
400 bytes
EBP
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
Access array elements
array2.asm array3.asm
Exercise
● Write a function to print an array
of double word integers.
array4.asm
Exercise
● Write a function to print an array
of double word integers.
array4.asm
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]
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
Advanced indirect addressing
array5.asm
array3.asm
local variables/arrays
local variables/arrays
local variables/arrays
immediate
local variables/arrays
return address
pushed EBP
a (400 bytes)
EBP
k (4 bytes)
j (4 bytes)
array6.asm
local variables/arrays
array6.asm
local variables/arrays
array6.asm
load effective address
array6.asm array7.asm
load effective address
array6.asm array7.asm
address generation
unit (AGU)
final program
array7.asm
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
get address of local variables / arrays
● storing a pointer to a local variable
● pushing on stack for function call
get address of local variables / arrays
● storing a pointer to a local variable
● pushing on stack for function call
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
EBP
j
p
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)
fast computations
lea EAX, [ EAX + 4 * EAX ]
fast computations
lea EAX, [ EAX + 4 * EAX ] EAX *= 5
fast computations
lea EAX, [ EAX + 4 * EAX ] EAX *= 5
???? EAX *= 6
fast computations
lea EAX, [ EAX + 4 * EAX ] EAX *= 5
lea EAX, [ EAX + 5 * EAX ] EAX *= 6
fast computations
lea EAX, [ EAX + 4 * EAX ] EAX *= 5
lea EAX, [ EAX + 5 * EAX ] EAX *= 6
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
fast computations
lea EAX, [ EAX + 4 * EAX ] EAX *= 5
lea EAX, [ EAX + 5 * EAX ] EAX *= 6
lea EAX, [ EAX + 2 * EAX ]
sal EAX
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 ]
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
Arrays in inline assembly
void 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
Arrays in inline assembly
void 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

More Related Content

Similar to Introduction to 8086 Assembly language & arrays

lect- 3&4.ppt
lect- 3&4.pptlect- 3&4.ppt
lect- 3&4.pptmrizwan38
 
PHP data structures (and the impact of php 7 on them), phpDay Verona 2015, Italy
PHP data structures (and the impact of php 7 on them), phpDay Verona 2015, ItalyPHP data structures (and the impact of php 7 on them), phpDay Verona 2015, Italy
PHP data structures (and the impact of php 7 on them), phpDay Verona 2015, ItalyPatrick Allaert
 
20220112 sac v1
20220112 sac v120220112 sac v1
20220112 sac v1Sharon Liu
 
ESWow! - Intro to JavaScript ES6
ESWow! - Intro to JavaScript ES6ESWow! - Intro to JavaScript ES6
ESWow! - Intro to JavaScript ES6Rick Nagy
 
High Performance GPU computing with Ruby, Rubykaigi 2018
High Performance GPU computing with Ruby, Rubykaigi 2018High Performance GPU computing with Ruby, Rubykaigi 2018
High Performance GPU computing with Ruby, Rubykaigi 2018Prasun Anand
 
Csharp4 arrays and_tuples
Csharp4 arrays and_tuplesCsharp4 arrays and_tuples
Csharp4 arrays and_tuplesAbed Bukhari
 
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]RootedCON
 
ES6 and AngularAMD
ES6 and AngularAMDES6 and AngularAMD
ES6 and AngularAMDdhaval10690
 
GeoGebra JavaScript CheatSheet
GeoGebra JavaScript CheatSheetGeoGebra JavaScript CheatSheet
GeoGebra JavaScript CheatSheetJose Perez
 

Similar to Introduction to 8086 Assembly language & arrays (20)

lect- 3&4.ppt
lect- 3&4.pptlect- 3&4.ppt
lect- 3&4.ppt
 
Pointers_c
Pointers_cPointers_c
Pointers_c
 
PHP data structures (and the impact of php 7 on them), phpDay Verona 2015, Italy
PHP data structures (and the impact of php 7 on them), phpDay Verona 2015, ItalyPHP data structures (and the impact of php 7 on them), phpDay Verona 2015, Italy
PHP data structures (and the impact of php 7 on them), phpDay Verona 2015, Italy
 
The Stack and Buffer Overflows
The Stack and Buffer OverflowsThe Stack and Buffer Overflows
The Stack and Buffer Overflows
 
8558537werr.pptx
8558537werr.pptx8558537werr.pptx
8558537werr.pptx
 
Antlr V3
Antlr V3Antlr V3
Antlr V3
 
20220112 sac v1
20220112 sac v120220112 sac v1
20220112 sac v1
 
Unit3 C
Unit3 C Unit3 C
Unit3 C
 
Al2ed chapter6
Al2ed chapter6Al2ed chapter6
Al2ed chapter6
 
ESWow! - Intro to JavaScript ES6
ESWow! - Intro to JavaScript ES6ESWow! - Intro to JavaScript ES6
ESWow! - Intro to JavaScript ES6
 
High Performance GPU computing with Ruby, Rubykaigi 2018
High Performance GPU computing with Ruby, Rubykaigi 2018High Performance GPU computing with Ruby, Rubykaigi 2018
High Performance GPU computing with Ruby, Rubykaigi 2018
 
Csharp4 arrays and_tuples
Csharp4 arrays and_tuplesCsharp4 arrays and_tuples
Csharp4 arrays and_tuples
 
Short intro to ECMAScript
Short intro to ECMAScriptShort intro to ECMAScript
Short intro to ECMAScript
 
Spark_Documentation_Template1
Spark_Documentation_Template1Spark_Documentation_Template1
Spark_Documentation_Template1
 
Es6 to es5
Es6 to es5Es6 to es5
Es6 to es5
 
MA3696 Lecture 6
MA3696 Lecture 6MA3696 Lecture 6
MA3696 Lecture 6
 
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
Sergi Álvarez & Roi Martín - Radare2 Preview [RootedCON 2010]
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
ES6 and AngularAMD
ES6 and AngularAMDES6 and AngularAMD
ES6 and AngularAMD
 
GeoGebra JavaScript CheatSheet
GeoGebra JavaScript CheatSheetGeoGebra JavaScript CheatSheet
GeoGebra JavaScript CheatSheet
 

Recently uploaded

microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 

Recently uploaded (20)

microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 

Introduction to 8086 Assembly language & arrays

  • 1. Introduction to 8086 Assembly Lecture 16 Implementing Arrays
  • 2. 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
  • 3. 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
  • 4. Global labels start address: arr1 element size: 1 byte array size: 7 elements (7 bytes) start address: arr2 element size: 2 bytes array size: 6 elements (12 bytes) start address: arr3 element size: 4 bytes array size: 5 elements (20 bytes) start address: arr4 element size: 4 bytes array size: 64 elements (256 bytes) start address: arr8 element size: 8 bytes array size: 400 elements (3200 bytes)
  • 5. Arrays on stack (as local variable) start address: ebp-400 element size: 1 byte array size: 400 elements OR start address: ebp-400 element size: 2 bytes array size: 200 elements OR start address: ebp-400 element size: 4 bytes array size: 100 elements Parameters return address init val of EBP 400 bytes EBP
  • 6. 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
  • 8. Exercise ● Write a function to print an array of double word integers. array4.asm
  • 9. Exercise ● Write a function to print an array of double word integers. array4.asm
  • 10. 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]
  • 11. 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
  • 16. local variables/arrays return address pushed EBP a (400 bytes) EBP k (4 bytes) j (4 bytes) array6.asm
  • 20. load effective address array6.asm array7.asm address generation unit (AGU)
  • 22. 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
  • 23. get address of local variables / arrays ● storing a pointer to a local variable ● pushing on stack for function call
  • 24. get address of local variables / arrays ● storing a pointer to a local variable ● pushing on stack for function call
  • 25. 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 EBP j p
  • 26. 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)
  • 27. fast computations lea EAX, [ EAX + 4 * EAX ]
  • 28. fast computations lea EAX, [ EAX + 4 * EAX ] EAX *= 5
  • 29. fast computations lea EAX, [ EAX + 4 * EAX ] EAX *= 5 ???? EAX *= 6
  • 30. fast computations lea EAX, [ EAX + 4 * EAX ] EAX *= 5 lea EAX, [ EAX + 5 * EAX ] EAX *= 6
  • 31. fast computations lea EAX, [ EAX + 4 * EAX ] EAX *= 5 lea EAX, [ EAX + 5 * EAX ] EAX *= 6
  • 32. 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
  • 33. fast computations lea EAX, [ EAX + 4 * EAX ] EAX *= 5 lea EAX, [ EAX + 5 * EAX ] EAX *= 6 lea EAX, [ EAX + 2 * EAX ] sal EAX
  • 34. 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 ]
  • 35. 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
  • 36. Arrays in inline assembly void 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
  • 37. Arrays in inline assembly void 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