Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of...
Transcript of Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of...
![Page 1: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/1.jpg)
1
Assembly Language:Part 2
Princeton UniversityComputer Science 217: Introduction to Programming Systems
![Page 2: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/2.jpg)
Goals of this Lecture
Help you learn:• Intermediate aspects of x86-64 assembly language…• Control flow with signed integers• Control flow with unsigned integers• Arrays• Structures
2
![Page 3: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/3.jpg)
Agenda
Flattened C code
Control flow with signed integers
Control flow with unsigned integers
Arrays
Structures
3
![Page 4: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/4.jpg)
Flattened C Code
Problem• Translating from C to assembly language is difficult
when the C code contains nested statements
Solution• Flatten the C code to eliminate all nesting
4
![Page 5: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/5.jpg)
Flattened C Code
if (expr){ statement1;
…statementN;
}
if (! expr) goto endif1;statement1;…statementN;
endif1:
if (expr){ statementT1;
…statementTN;
}else{ statementF1;
…statementFN;
}
if (! expr) goto else1;statementT1;…statementTN;goto endif1;
else1:statementF1;…statementFN;
endif1:
Flattened CC
5
![Page 6: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/6.jpg)
Flattened C Code
6
while (expr){ statement1;
…statementN;
}
loop1:if (! expr) goto endloop1;
statement1;…statementN;goto loop1;
endloop1:
Flattened CC
expr1;loop1:
if (! expr2) goto endloop1;statement1;…statementN;expr3;goto loop1;
endloop1:
for (expr1; expr2; expr3}{ statement1;
…statementN;
}
See Bryant & O’Hallaronbook for faster patterns
![Page 7: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/7.jpg)
Agenda
Flattened C code
Control flow with signed integers
Control flow with unsigned integers
Arrays
Structures
7
![Page 8: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/8.jpg)
if Example
8
int i;…if (i < 0)
i = -i;
int i;…
if (i >= 0) goto endif1;i = -i;
endif1:
C Flattened C
![Page 9: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/9.jpg)
if Example
9
.section ".bss"i: .skip 4…
.section ".text"…
cmpl $0, ijge endif1negl i
endif1:
Note:cmp instruction (counterintuitive operand order)
Sets CC bits in EFLAGS registerjge instruction (conditional jump)
Examines CC bits in EFLAGS register
int i;…
if (i >= 0) goto endif1;i = -i;
endif1:
Flattened C Assem Lang
![Page 10: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/10.jpg)
if…else Example
10
int i;int j;int smaller;…if (i < j)
smaller = i;else
smaller = j;
int i;int j;int smaller;…
if (i >= j) goto else1;smaller = i;goto endif1;
else1:smaller = j;
endif1:
C Flattened C
![Page 11: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/11.jpg)
if…else Example
11
.section ".bss"i: .skip 4j: .skip 4smaller: .skip 4…
.section ".text"…
movl i, %eaxcmpl j, %eaxjge else1movl i, %eaxmovl %eax, smallerjmp endif1
else1:movl j, %eaxmovl %eax, smaller
endif1:
Flattened C
Note:jmp instruction
(unconditional jump)
Assem Langint i;int j;int smaller;…
if (i >= j) goto else1;smaller = i;goto endif1;
else1:smaller = j;
endif1:
![Page 12: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/12.jpg)
while Example
12
int fact;int n;…fact = 1;while (n > 1){ fact *= n;n--;
}
int fact;int n;…
fact = 1;loop1:
if (n <= 1) goto endloop1;fact *= n;n--;goto loop1;
endloop1:
C Flattened C
![Page 13: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/13.jpg)
while Example
13
.section ".bss"fact: .skip 4n: .skip 4…
.section ".text"…
movl $1, factloop1:
cmpl $1, njle endloop1movl fact, %eaximull nmovl %eax, factdecl njmp loop1
endloop1:
int fact;int n;…
fact = 1;loop1:
if (n <= 1) goto endloop1;fact *= n;n--;goto loop1;
endloop1:
Flattened C
Note:jle instruction (conditional jump)imul instruction
Assem Lang
![Page 14: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/14.jpg)
for Example
14
int power = 1;int base;int exp;int i;…for (i = 0; i < exp; i++)power *= base;
int power = 1;int base;int exp;int i;…
i = 0;loop1:
if (i >= exp) goto endloop1;power *= base;i++;goto loop1;
endloop1:
C Flattened C
![Page 15: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/15.jpg)
for Example
15
.section ".data"power: .long 1
.section ".bss"base: .skip 4exp: .skip 4i: .skip 4…
.section ".text”
…movl $0, i
loop1:movl i, %eaxcmpl exp, %eaxjge endloop1movl power, %eaximull basemovl %eax, powerincl ijmp loop1
endloop1:
Flattened Cint power = 1;int base;int exp;int i;…
i = 0;loop1:
if (i >= exp) goto endloop1;power *= base;i++;goto loop1;
endloop1:
Assem Lang
![Page 16: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/16.jpg)
Control Flow with Signed Integers
Comparing signed integers
• Sets condition-code bits in the EFLAGS register• Beware: operands are in counterintuitive order• Beware: many other instructions set condition-code bits
• Conditional jump should immediately follow cmp
16
cmp{q,l,w,b} srcIRM, destRM Compare dest with src
![Page 17: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/17.jpg)
Control Flow with Signed IntegersUnconditional jump
Conditional jumps after comparing signed integers
• Examine CC bits in EFLAGS register
17
je label Jump to label if equaljne label Jump to label if not equaljl label Jump to label if lessjle label Jump to label if less or equaljg label Jump to label if greaterjge label Jump to label if greater or equal
jmp label Jump to label
![Page 18: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/18.jpg)
Agenda
Flattened C
Control flow with signed integers
Control flow with unsigned integers
Arrays
Structures
18
![Page 19: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/19.jpg)
Signed vs. Unsigned Integers
In C• Integers are signed or unsigned• Compiler generates assem lang instructions accordingly
In assembly language• Integers are neither signed nor unsigned• Distinction is in the instructions used to manipulate them
Distinction matters for• Multiplication and division• Control flow
19
![Page 20: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/20.jpg)
Handling Unsigned Integers
Multiplication and division• Signed integers: imul, idiv• Unsigned integers: mul, div
Control flow• Signed integers: cmp + {je, jne, jl, jle, jg, jge}
Unsigned integers: “unsigned cmp” + {je, jne, jl, jle, jg, jge} ? No!!!• Unsigned integers: cmp + {je, jne, jb, jbe, ja, jae}
20
![Page 21: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/21.jpg)
while Example
21
unsigned int fact;unsigned int n;…fact = 1;while (n > 1){ fact *= n;n--;
}
unsigned int fact;unsigned int n;…
fact = 1;loop1:
if (n <= 1) goto endloop1;fact *= n;n--;goto loop1;
endloop1:
C Flattened C
![Page 22: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/22.jpg)
while Example
22
.section ".bss"fact: .skip 4n: .skip 4…
.section ".text"…
movl $1, factloop1:
cmpl $1, njbe endloop1movl fact, %eaxmull nmovl %eax, factdecl njmp loop1
endloop1:
unsigned int fact;unsigned int n;…
fact = 1;loop1:
if (n <= 1) goto endloop1;fact *= n;n--;goto loop1;
endloop1:
Flattened C
Note:jbe instruction (instead of jle)mull instruction (instead of imull)
Assem Lang
![Page 23: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/23.jpg)
for Example
23
unsigned int power = 1;unsigned int base;unsigned int exp;unsigned int i;…for (i = 0; i < exp; i++)power *= base;
unsigned int power = 1;unsigned int base;unsigned int exp;unsigned int i;…
i = 0;loop1:
if (i >= exp) goto endloop1;power *= base;i++;goto loop1;
endloop1:
C Flattened C
![Page 24: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/24.jpg)
for Example
24
.section ".data"power: .long 1
.section ".bss"base: .skip 4exp: .skip 4i: .skip 4…
.section ".text”
…movl $0, i
loop1:movl i, %eaxcmpl exp, %eaxjae endloop1movl power, %eaxmull basemovl %eax, powerincl ijmp loop1
endloop1:
Flattened Cunsigned int power = 1;unsigned int base;unsigned int exp;unsigned int i;…
i = 0;loop1:
if (i >= exp) goto endloop1;power *= base;i++;goto loop1;
endloop1:
Assem Lang
Note:jae instruction (instead of jge)mull instruction (instead of imull)
![Page 25: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/25.jpg)
Control Flow with Unsigned IntegersComparing unsigned integers
(Same as comparing signed integers)
Conditional jumps after comparing unsigned integers
• Examine CC bits in EFLAGS register
25
je label Jump to label if equaljne label Jump to label if not equaljb label Jump to label if belowjbe label Jump to label if below or equalja label Jump to label if abovejae label Jump to label if above or equal
cmp{q,l,w,b} srcIRM, destRM Compare dest with src
![Page 26: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/26.jpg)
Agenda
Flattened C
Control flow with signed integers
Control flow with unsigned integers
Arrays
Structures
26
![Page 27: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/27.jpg)
Arrays: Indirect Addressing
27
.section ".bss"a: .skip 400i: .skip 4n: .skip 4
….section ".text"…movl $3, i…movslq i, %raxsalq $2, %raxaddq $a, %raxmovl (%rax), %r10dmovl %r10d, n…
C Assem Langint a[100];int i;int n;…i = 3;…n = a[i]…
One step at a time…
![Page 28: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/28.jpg)
Arrays: Indirect Addressing
28
.section ".bss"a: .skip 400i: .skip 4n: .skip 4
….section ".text"…movl $3, i…movslq i, %raxsalq $2, %raxaddq $a, %raxmovl (%rax), %r10dmovl %r10d, n…
Assem Lang
123
3
1000
1004
1008
…
1396
1400
1404
0
1
2
3
99
i
n
a
RAX
R10
Registers Memory
1012…
![Page 29: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/29.jpg)
Arrays: Indirect Addressing
29
.section ".bss"a: .skip 400i: .skip 4n: .skip 4
….section ".text"…movl $3, i…movslq i, %raxsalq $2, %raxaddq $a, %raxmovl (%rax), %r10dmovl %r10d, n…
Assem Lang
123
3
1000
1004
1008
…
1396
1400
1404
0
1
2
3
99
i
n
a
3RAX
R10
Registers Memory
1012…
![Page 30: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/30.jpg)
Arrays: Indirect Addressing
30
.section ".bss"a: .skip 400i: .skip 4n: .skip 4
….section ".text"…movl $3, i…movslq i, %raxsalq $2, %raxaddq $a, %raxmovl (%rax), %r10dmovl %r10d, n…
Assem Lang
123
3
1000
1004
1008
…
1396
1400
1404
0
1
2
3
99
i
n
a
12RAX
R10
Registers Memory
1012…
![Page 31: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/31.jpg)
Arrays: Indirect Addressing
31
.section ".bss"a: .skip 400i: .skip 4n: .skip 4
….section ".text"…movl $3, i…movslq i, %raxsalq $2, %raxaddq $a, %raxmovl (%rax), %r10dmovl %r10d, n…
Assem Lang
123
3
1000
1004
1008
…
1396
1400
1404
0
1
2
3
99
i
n
a
1012RAX
R10
Registers Memory
1012…
![Page 32: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/32.jpg)
Arrays: Indirect Addressing
32
.section ".bss"a: .skip 400i: .skip 4n: .skip 4
….section ".text"…movl $3, i…movslq i, %raxsalq $2, %raxaddq $a, %raxmovl (%rax), %r10dmovl %r10d, n…
Assem Lang
123
3
1000
1004
1008
…
1396
1400
1404
0
1
2
3
99
i
n
a
1012
123
RAX
R10
Registers Memory
1012…
Note:Indirect addressing
![Page 33: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/33.jpg)
Arrays: Indirect Addressing
33
.section ".bss"a: .skip 400i: .skip 4n: .skip 4
….section ".text"…movl $3, i…movslq i, %raxsalq $2, %raxaddq $a, %raxmovl (%rax), %r10dmovl %r10d, n…
Assem Lang
123
3
123
1000
1004
1008
…
1396
1400
1404
0
1
2
3
99
i
n
a
1012
123
RAX
R10
Registers Memory
1012…
![Page 34: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/34.jpg)
Arrays: Base+Disp Addressing
34
.section ".bss"a: .skip 400i: .skip 4n: .skip 4
….section ".text"…movl $3, i…movl i, %eaxsall $2, %eaxmovl a(%eax), %r10dmovl %r10d, n…
C Assem Langint a[100];int i;int n;…i = 3;…n = a[i]…
One step at a time…
![Page 35: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/35.jpg)
Arrays: Base+Disp Addressing
35
.section ".bss"a: .skip 400i: .skip 4n: .skip 4
….section ".text"…movl $3, i…movl i, %eaxsall $2, %eaxmovl a(%eax), %r10dmovl %r10d, n…
Assem Lang
123
3
1000
1004
1008
…
1396
1400
1404
0
1
2
3
99
i
n
a
RAX
R10
Registers Memory
1012…
![Page 36: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/36.jpg)
Arrays: Base+Disp Addressing
36
.section ".bss"a: .skip 400i: .skip 4n: .skip 4
….section ".text"…movl $3, i…movl i, %eaxsall $2, %eaxmovl a(%eax), %r10dmovl %r10d, n…
Assem Lang
123
3
1000
1004
1008
…
1396
1400
1404
0
1
2
3
99
i
n
a
3RAX
R10
Registers Memory
1012…
![Page 37: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/37.jpg)
Arrays: Base+Disp Addressing
37
.section ".bss"a: .skip 400i: .skip 4n: .skip 4
….section ".text"…movl $3, i…movl i, %eaxsall $2, %eaxmovl a(%eax), %r10dmovl %r10d, n…
Assem Lang
123
3
1000
1004
1008
…
1396
1400
1404
0
1
2
3
99
i
n
a
12RAX
R10
Registers Memory
1012…
![Page 38: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/38.jpg)
Arrays: Base+Disp Addressing
38
.section ".bss"a: .skip 400i: .skip 4n: .skip 4
….section ".text"…movl $3, i…movl i, %eaxsall $2, %eaxmovl a(%eax), %r10dmovl %r10d, n…
Assem Lang
123
3
1000
1004
1008
…
1396
1400
1404
0
1
2
3
99
i
n
a
12
123
RAX
R10
Registers Memory
1012…
Note:Base+displacement addressing
![Page 39: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/39.jpg)
Arrays: Base+Disp Addressing
39
.section ".bss"a: .skip 400i: .skip 4n: .skip 4
….section ".text"…movl $3, i…movl i, %eaxsall $2, %eaxmovl a(%eax), %r10dmovl %r10d, n…
Assem Lang
123
3
123
1000
1004
1008
…
1396
1400
1404
0
1
2
3
99
i
n
a
12
123
RAX
R10
Registers Memory
1012…
![Page 40: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/40.jpg)
Arrays: Scaled Indexed Addressing
40
.section ".bss"a: .skip 400i: .skip 4n: .skip 4
….section ".text"…movl $3, i…movl i, %eaxmovl a(,%eax,4), %r10dmovl %r10d, n…
C Assem Langint a[100];int i;int n;…i = 3;…n = a[i]…
One step at a time…
![Page 41: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/41.jpg)
Arrays: Scaled Indexed Addressing
41
.section ".bss"a: .skip 400i: .skip 4n: .skip 4
….section ".text"…movl $3, i…movl i, %eaxmovl a(,%eax,4), %r10dmovl %r10d, n…
Assem Lang
123
3
1000
1004
1008
…
1396
1400
1404
0
1
2
3
99
i
n
a
RAX
R10
Registers Memory
1012…
![Page 42: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/42.jpg)
Arrays: Scaled Indexed Addressing
42
.section ".bss"a: .skip 400i: .skip 4n: .skip 4
….section ".text"…movl $3, i…movl i, %eaxmovl a(,%eax,4), %r10dmovl %r10d, n…
Assem Lang
123
3
1000
1004
1008
…
1396
1400
1404
0
1
2
3
99
i
n
a
3RAX
R10
Registers Memory
1012…
![Page 43: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/43.jpg)
Arrays: Scaled Indexed Addressing
43
.section ".bss"a: .skip 400i: .skip 4n: .skip 4
….section ".text"…movl $3, i…movl i, %eaxmovl a(,%eax,4), %r10dmovl %r10d, n…
Assem Lang
123
3
1000
1004
1008
…
1396
1400
1404
0
1
2
3
99
i
n
a
3
123
RAX
R10
Registers Memory
1012…
Note:Scaled indexed addressing
![Page 44: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/44.jpg)
Arrays: Scaled Indexed Addressing
44
.section ".bss"a: .skip 400i: .skip 4n: .skip 4
….section ".text"…movl $3, i…movl i, %eaxmovl a(,%eax,4), %r10dmovl %r10d, n…
Assem Lang
123
3
123
1000
1004
1008
…
1396
1400
1404
0
1
2
3
99
i
n
a
12
123
RAX
R10
Registers Memory
1012…
![Page 45: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/45.jpg)
Generalization: Memory OperandsFull form of memory operands:
displacement(base,index,scale)• displacement is an integer or a label (default = 0)• base is a 4-byte or 8-byte register• index is a 4-byte or 8-byte register• scale is 1, 2, 4, or 8 (default = 1)
Meaning• Compute the sum
(displacement) + (contents of base) + ((contents of index) * (scale))• Consider the sum to be an address• Load from (or store to) that address
Note:• All other forms are subsets of the full form…
45
![Page 46: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/46.jpg)
Generalization: Memory OperandsValid subsets:
• Direct addressing• displacement
• Indirect addressing• (base)
• Base+displacement addressing• displacement(base)
• Indexed addressing• (base, index)• displacement(base,index)
• Scaled indexed addressing• (,index, scale)• displacement(,index,scale)• (base,index,scale)• displacement(base,index,scale)
46
![Page 47: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/47.jpg)
Operand ExamplesImmediate operands
• $5 ⇒ use the number 5 (i.e. the number that is available immediately within the instruction)
• $i ⇒ use the address denoted by i (i.e. the address that is available immediately within the instruction)
Register operands• %rax ⇒ read from (or write to) register RAX
Memory operands: direct addressing• 5 ⇒ load from (or store to) memory at address 5 (silly; seg fault)• i ⇒ load from (or store to) memory at the address denoted by i
Memory operands: indirect addressing• (%rax) ⇒ consider the contents of RAX to be an address; load
from (or store to) that address 47
![Page 48: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/48.jpg)
Operand ExamplesMemory operands: base+displacement addressing
• 5(%rax) ⇒ compute the sum (5) + (contents of RAX); consider the sum to be an address; load from (or store to) that address
• i(%rax) ⇒ compute the sum (address denoted by i) + (contents of RAX); consider the sum to be an address; load from (or store to) that address
Memory operands: indexed addressing• 5(%rax,%r10) ⇒ compute the sum (5) + (contents of RAX) +
(contents of R10); consider the sum to be an address; load from (or store to) that address
• i(%rax,%r10) ⇒ compute the sum (address denoted by i) + (contents of RAX) + (contents of R10); consider the sum to be an address; load from (or store to) that address
48
![Page 49: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/49.jpg)
Operand Examples
Memory operands: scaled indexed addressing• 5(%rax,%r10,4) ⇒ compute the sum (5) + (contents of RAX) +
((contents of R10) * 4); consider the sum to be an address; load from (or store to) that address
• i(%rax,%r10,4) ⇒ compute the sum (address denoted by i) + (contents of RAX) + ((contents of R10) * 4); consider the sum to be an address; load from (or store to) that address
49
![Page 50: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/50.jpg)
Aside: The lea Instructionlea: load effective address
• Unique instruction: suppresses memory load/store
Example• movq 5(%rax), %r10
• Compute the sum (5) + (contents of RAX); consider the sum to be an address; load 8 bytes from that address into R10
• leaq 5(%rax), %r10• Compute the sum (5) + (contents of RAX); move that sum to R10
Useful for• Computing an address, e.g. as a function argument
• See precept code that calls scanf()• Some quick-and-dirty arithmetic
50
![Page 51: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/51.jpg)
iClicker QuestionQ: What is the effect of the following code?
A. Move the quad at 4*(contents of RAX) to RAX
B. Move the quad at 5*(contents of RAX) to RAX
C. Multiply RAX by 4
D. Multiply RAX by 5
E. Double RAX and add 4
leaq (%rax,%rax,4),%rax
![Page 52: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/52.jpg)
Agenda
Flattened C
Control flow with signed integers
Control flow with unsigned integers
Arrays
Structures
52
![Page 53: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/53.jpg)
Structures: Indirect Addressing
53
struct S{ int i;int j;
};…struct S myStruct;…myStruct.i = 18;…myStruct.j = 19;
C.section ".bss"
myStruct: .skip 8….section ".text"…movq $myStruct, %raxmovl $18, (%rax)…movq $myStruct, %raxaddq $4, %raxmovl $19, (%rax)
Assem Lang
Note:Indirect addressing 18
19
rax RAM
![Page 54: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/54.jpg)
Structures: Base+Disp Addressing
54
struct S{ int i;int j;
};…struct S myStruct;…myStruct.i = 18;…myStruct.j = 19;
C.section ".bss"
myStruct: .skip 8….section ".text"…movq $myStruct, %raxmovl $18, 0(%rax)…movl $19, 4(%rax)
Assem Lang
18
19
rax RAM
![Page 55: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/55.jpg)
Structures: Padding
55
struct S{ char c;int i;
};…struct S myStruct;…myStruct.c = 'A';…myStruct.i = 18;
C.section ".bss"
myStruct: .skip 8….section ".text"…movq $myStruct, %raxmovb $'A', 0(%rax)…movl $18, 4(%rax)
Assem Lang
Beware:Compiler sometimes inserts padding after fields
Three-bytepad here
![Page 56: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/56.jpg)
Structures: Paddingx86-64/Linux rules
• Compiler may add padding after last field if struct is within an array56
Data type Within a struct, must begin at address that is evenly divisible by:
(unsigned) char 1(unsigned) short 2(unsigned) int 4(unsigned) long 8float 4double 8long double 16any pointer 8
![Page 57: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/57.jpg)
Summary
Intermediate aspects of x86-64 assembly language…
Flattened C code
Control transfer with signed integers
Control transfer with unsigned integers
Arrays• Full form of instruction operands
Structures• Padding
57
![Page 58: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/58.jpg)
Appendix
Setting and using CC bits in EFLAGS register
58
![Page 59: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/59.jpg)
Setting Condition Code Bits
Question• How does cmp{q,l,w,b} set condition code bits in EFLAGS
register?
Answer• (See following slides)
59
![Page 60: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/60.jpg)
Condition Code Bits
Condition code bits• ZF: zero flag: set to 1 iff result is zero• SF: sign flag: set to 1 iff result is negative• CF: carry flag: set to 1 iff unsigned overflow occurred• OF: overflow flag: set to 1 iff signed overflow occurred
60
![Page 61: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/61.jpg)
Condition Code Bits
Example: addq src, dest• Compute sum (dest+src)• Assign sum to dest• ZF: set to 1 iff sum == 0• SF: set to 1 iff sum < 0• CF: set to 1 iff unsigned overflow
• Set to 1 iff sum<src• OF: set if signed overflow
• Set to 1 iff(src>0 && dest>0 && sum<0) ||(src<0 && dest<0 && sum>=0)
61
![Page 62: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/62.jpg)
Condition Code BitsExample: subq src, dest
• Compute sum (dest+(-src))• Assign sum to dest• ZF: set to 1 iff sum == 0• SF: set to 1 iff sum < 0• CF: set to 1 iff unsigned overflow
• Set to 1 iff dest<src• OF: set to 1 iff signed overflow
• Set to 1 iff(dest>0 && src<0 && sum<0) ||(dest<0 && src>0 && sum>=0)
Example: cmpq src, dest• Same as subq• But does not affect dest
62
![Page 63: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/63.jpg)
Using Condition Code Bits
Question• How do conditional jump instructions use condition code bits in
EFLAGS register?
Answer• (See following slides)
63
![Page 64: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/64.jpg)
Conditional Jumps: UnsignedAfter comparing unsigned data
Note:• If you can understand why jb jumps iff CF• … then the others follow
64
Jump Instruction
Use of CC Bits
je label ZFjne label ~ZFjb label CFjae label ~CFjbe label CF | ZFja label ~(CF | ZF)
![Page 65: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/65.jpg)
Conditional Jumps: UnsignedWhy does jb jump iff CF? Informal explanation:
(1) largenum – smallnum (not below)• Correct result• ⇒ CF=0 ⇒ don’t jump
(2) smallnum – largenum (below)• Incorrect result• ⇒ CF=1 ⇒ jump
65
![Page 66: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/66.jpg)
Conditional Jumps: SignedAfter comparing signed data
Note:• If you can understand why jl jumps iff OF^SF• … then the others follow
66
Jump Instruction
Use of CC Bits
je label ZFjne label ~ZFjl label OF ^ SFjge label ~(OF ^ SF)jle label (OF ^ SF) | ZFjg label ~((OF ^ SF) | ZF)
![Page 67: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/67.jpg)
Conditional Jumps: SignedWhy does jl jump iff OF^SF? Informal explanation:
(1) largeposnum – smallposnum (not less than)• Certainly correct result• ⇒ OF=0, SF=0, OF^SF==0 ⇒ don’t jump
(2) smallposnum – largeposnum (less than)• Certainly correct result• ⇒ OF=0, SF=1, OF^SF==1 ⇒ jump
(3) largenegnum – smallnegnum (less than)• Certainly correct result• ⇒ OF=0, SF=1 ⇒ (OF^SF)==1 ⇒ jump
(4) smallnegnum – largenegnum (not less than)• Certainly correct result• ⇒ OF=0, SF=0 ⇒ (OF^SF)==0 ⇒ don't jump 67
![Page 68: Assembly Language: Part 2 · Goals of this Lecture. Help you learn: • Intermediate aspects of x86-64 assembly language… • Control flow with signed integers • Control flow](https://reader035.fdocuments.in/reader035/viewer/2022070821/5f1f609660f62d3f721d5032/html5/thumbnails/68.jpg)
Conditional Jumps: Signed(5) posnum – negnum (not less than)
• Suppose correct result• ⇒ OF=0, SF=0 ⇒ (OF^SF)==0 ⇒ don't jump
(6) posnum – negnum (not less than)• Suppose incorrect result• ⇒ OF=1, SF=1 ⇒ (OF^SF)==0 ⇒ don't jump
(7) negnum – posnum (less than)• Suppose correct result• ⇒ OF=0, SF=1 ⇒ (OF^SF)==1 ⇒ jump
(8) negnum – posnum (less than)• Suppose incorrect result• ⇒ OF=1, SF=0 ⇒ (OF^SF)==1 ⇒ jump
68