Professional Documents
Culture Documents
14 Assembly2
14 Assembly2
Assembly Language:
Part 2
1
Goals of this Lecture
2
Agenda
Flattened C code
Control flow with signed integers
Control flow with unsigned integers
Arrays
Structures
3
Flattened C Code
Problem
• Translating from C to assembly language is difficult
when the C code doesn’t proceed in consecutive lines
Solution
• Flatten the C code to eliminate all nesting
4
Flattened C Code
C Flattened C
if (expr) if (! expr) goto endif1;
{ statement1; statement1;
... ...
statementN; statementN;
} endif1:
5
Flattened C Code
C Flattened C
while (expr) loop1:
{ statement1; if (! expr) goto endloop1;
... statement1;
statementN; ...
} statementN;
goto loop1;
endloop1:
for (expr1; expr2; expr3} expr1;
{ statement1; loop1:
... if (! expr2) goto endloop1;
statementN; statement1;
} ...
statementN;
expr3;
goto loop1;
endloop1:
6
Agenda
Flattened C code
Control flow with signed integers
Control flow with unsigned integers
Arrays
Structures
7
if Example
C Flattened C
int i; int i;
... ...
if (i < 0) if (i >= 0) goto endif1;
i = -i; i = -i;
endif1:
8
if Example
Flattened C Assembly
int i; .section ".bss"
... i: .skip 4
if (i >= 0) goto endif1; ...
i = -i; .section ".text"
endif1: ...
adr x0, i
ldr w1, [x0]
cmp w1, 0
Assembler shorthand for
subs wzr, w1, 0 bge endif1
neg w1, w1
endif1:
Notes:
cmp instruction: compares operands, sets condition flags
bge instruction (conditional branch if greater than or equal):
Examines condition flags in PSTATE register
9
if…else Example
C Flattened C
int i; int i;
int j; int j;
int smaller; int smaller;
... ...
if (i < j) if (i >= j) goto else1;
smaller = i; smaller = i;
else goto endif1;
smaller = j; else1:
smaller = j;
endif1:
10
if…else Example
Flattened C Assembly
int i; ...
int j; adr x0, i
int smaller; ldr w1, [x0]
... adr x0, j
if (i >= j) goto else1; ldr w2, [x0]
smaller = i; cmp w1, w2
goto endif1; bge else1
else1: adr x0, smaller
smaller = j; str w1, [x0]
endif1: b endif1
else1:
adr x0, smaller
str w2, [x0]
endif1:
Note:
b instruction (unconditional branch)
11
while Example
C Flattened C
int n; int n;
int fact; int fact;
... ...
fact = 1; fact = 1;
while (n > 1) loop1:
{ fact *= n; if (n <= 1) goto endloop1;
n--; fact *= n;
} n--;
goto loop1;
endloop1:
12
while Example
Flattened C Assembly
int n; ...
int fact; adr x0, n
... ldr w1, [x0]
fact = 1; mov w2, 1
loop1: loop1:
if (n <= 1) goto endloop1; cmp w1, 1
fact *= n; ble endloop1
n--; mul w2, w2, w1
goto loop1; sub w1, w1, 1
endloop1: b loop1
endloop1:
# str w2 into fact
Note:
ble instruction (conditional branch if less than or equal)
13
for Example
C Flattened C
int power = 1; int power = 1;
int base; int base;
int exp; int exp;
int i; int i;
... ...
for (i = 0; i < exp; i++) i = 0;
power *= base; loop1:
if (i >= exp) goto endloop1;
power *= base;
i++;
goto loop1;
endloop1:
14
iClicker Question
Q: Which section(s) would power, base, exp, i go into?
int power = 1;
int base;
int exp;
int i;
A. All in .data
B. All in .bss
C. power in .data and rest in .rodata
D. power in .bss and rest in .data
E. power in .data and rest in .bss
for Example
Flattened C Assembly
int power = 1; .section ".data"
int base; power: .word 1
int exp; ...
int i; .section ".bss"
... base: .skip 4
i = 0; exp: .skip 4
loop1: i: .skip 4
if (i >= exp) goto endloop1; ...
power *= base;
i++;
goto loop1;
endloop1:
16
for Example
Flattened C Assembly
int power = 1; ...
int base; adr x0, power
int exp; ldr w1, [x0]
int i; adr x0, base
... ldr w2, [x0]
i = 0; adr x0, exp
loop1: ldr w3, [x0]
if (i >= exp) goto endloop1; mov w4, 0
power *= base; loop1:
i++; cmp w4, w3
goto loop1; bge endloop1
endloop1: mul w1, w1, w2
add w4, w4, 1
b loop1
endloop1:
# str w1 into power
17
Control Flow with Signed Integers
Unconditional branch
b label Branch to label
Compare
cmp Xm, Xn Compare Xm to Xn
cmp Wm, Wn Compare Wm to Wn
Flattened C
Control flow with signed integers
Control flow with unsigned integers
Arrays
Structures
19
Signed vs. Unsigned Integers
In C
• Integers are signed or unsigned
• Compiler generates assembly language instructions accordingly
In assembly language
• Integers are neither signed nor unsigned
• Distinction is in the instructions used to manipulate them
Compare
cmp Xm, Xn Compare Xm to Xn
cmp Wm, Wn Compare Wm to Wn
23
Alternative Control Flow: CBZ, CBNZ
24
Agenda
Flattened C
Control flow with signed integers
Control flow with unsigned integers
Arrays
Structures
25
Arrays: Brute Force
C Assembly
int a[100]; .section ".bss"
size_t i; a: .skip 400
int n; i: .skip 8
n: .skip 4
...
...
i = 2;
.section ".text"
... ...
n = a[i] mov x1, 2
... ...
adr x0, a
lsl x1, x1, 2
add x0, x0, x1
ldr w2, [x0]
adr x0, n
27
Arrays: Brute Force
Assembly
.section ".bss"
a: .skip 400 Registers Memory
i: .skip 8
x0 1000 0 1000
n: .skip 4
...
.section ".text"
x1 2 1 1004
...
mov x1, 2
w2 a 2 42 1008
...
adr x0, a
lsl x1, x1, 2
add x0, x0, x1 99 1396
ldr w2, [x0]
adr x0, n
i 1400
str w2, [x0]
n 1408
...
28
Arrays: Brute Force
Assembly
.section ".bss"
a: .skip 400 Registers Memory
i: .skip 8
x0 1000 0 1000
n: .skip 4
...
.section ".text"
x1 8 1 1004
...
mov x1, 2
w2 a 2 42 1008
...
adr x0, a
lsl x1, x1, 2
add x0, x0, x1 99 1396
ldr w2, [x0]
adr x0, n
i 1400
str w2, [x0]
n 1408
...
29
Arrays: Brute Force
Assembly
.section ".bss"
a: .skip 400 Registers Memory
i: .skip 8
x0 1008 0 1000
n: .skip 4
...
.section ".text"
x1 8 1 1004
...
mov x1, 2
w2 a 2 42 1008
...
adr x0, a
lsl x1, x1, 2
add x0, x0, x1 99 1396
ldr w2, [x0]
adr x0, n
i 1400
str w2, [x0]
n 1408
...
30
Arrays: Brute Force
Assembly
.section ".bss"
a: .skip 400 Registers Memory
i: .skip 8
x0 1008 0 1000
n: .skip 4
...
.section ".text"
x1 8 1 1004
...
mov x1, 2
w2 42 a 2 42 1008
...
adr x0, a
lsl x1, x1, 2
add x0, x0, x1 99 1396
ldr w2, [x0]
adr x0, n
i 1400
str w2, [x0]
n 1408
...
31
Arrays: Brute Force
Assembly
.section ".bss"
a: .skip 400 Registers Memory
i: .skip 8
x0 1408 0 1000
n: .skip 4
...
.section ".text"
x1 8 1 1004
...
mov x1, 2
w2 42 a 2 42 1008
...
adr x0, a
lsl x1, x1, 2
add x0, x0, x1 99 1396
ldr w2, [x0]
adr x0, n
i 1400
str w2, [x0]
n 1408
...
32
Arrays: Brute Force
Assembly
.section ".bss"
a: .skip 400 Registers Memory
i: .skip 8
x0 1408 0 1000
n: .skip 4
...
.section ".text"
x1 8 1 1004
...
mov x1, 2
w2 42 a 2 42 1008
...
adr x0, a
lsl x1, x1, 2
add x0, x0, x1 99 1396
ldr w2, [x0]
adr x0, n
i 1400
str w2, [x0]
...
n 42 1408
33
Arrays: Register Offset Addressing
C Brute-Force Register Offset
int a[100]; .section ".bss" .section ".bss"
size_t i; a: .skip 400 a: .skip 400
i: .skip 8 i: .skip 8
int n;
n: .skip 4 n: .skip 4
... ...
...
i = 2; .section ".text" .section ".text"
... ... ...
n = a[i] mov x1, 2 mov x1, 2
... ... ...
adr x0, a adr x0, a
lsl x1, x1, 2
add x0, x0, x1
ldr w2, [x0] ldr w2, [x0, x1, lsl 2]
adr x0, n adr x0, n
str w2, [x0] str w2, [x0]
... ...
Flattened C
Control flow with signed integers
Control flow with unsigned integers
Arrays
Structures
36
Structures
C Assembly
struct S .section ".bss"
{ int i; myStruct: .skip 8
int j; ...
.section ".text"
};
...
... adr x0, myStruct
struct S myStruct; ...
... mov w1, 18
myStruct.i = 18; str w1, [x0]
... ...
myStruct.j = 19; mov w1, 19
str ???
x0 RAM
18
19
37
iClicker Question
Q: Which addressing mode is most
appropriate for the last store? .section ".bss"
myStruct: .skip 8
...
x0 RAM
18
19
Structures: Offset Addressing
C Brute-Force Offset
struct S .section ".bss" .section ".bss"
{ int i; myStruct: .skip 8 myStruct: .skip 8
int j; ... ...
.section ".text" .section ".text"
};
... ...
... adr x0, myStruct adr x0, myStruct
struct S myStruct; ... ...
... mov w1, 18 mov w1, 18
myStruct.i = 18; str w1, [x0] str w1, [x0]
... ... ...
myStruct.j = 19; mov w1, 19 mov w1, 19
add x0, x0, 4 str w1, [x0, 4]
str w1, [x0]
x0 RAM
18
19
39
Structures: Padding
C Assembly
struct S .section ".bss"
{ char c; Three-byte myStruct: .skip 8
...
int i;
pad here .section ".text"
};
...
… adr x0, myStruct
struct S myStruct; ...
… mov w1, 'A'
myStruct.c = 'A'; strb w1, [x0]
… ...
myStruct.i = 18; mov w1, 18
str w1, [x0, 4]
4, not 1
Beware:
Compiler sometimes inserts padding after fields
40
Structures: Padding
AARCH64 rules
Data type Within a struct, field must
begin at address that is
evenly divisible by:
(unsigned) char 1
(unsigned) short 2
(unsigned) int 4
(unsigned) long 8
float 4
double 8
long double 16
any pointer 8
• Compiler may add padding after last field if struct is within an array
41
Summary
Structures
• Padding
42
Appendix
43
Setting Condition Flags
Question
• How does cmp (or arithmetic instructions with “s” suffix)
set condition flags?
44
Condition Flags
Condition flags
• N: negative flag: set to 1 iff result is negative
• 2’s complement sign bit (lmb) of the result
• Z: zero flag: set to 1 iff result is zero
• C: carry flag: set to 1 iff carry/borrow from msb (unsigned overflow)
• V: overflow flag: set to 1 iff signed overflow occurred
45
Condition Flags
46
Condition Flags
47
Using Condition Flags
Question
• How do conditional branch instructions use the condition flags?
Answer
• (See following slides)
48
Conditional Branches: Unsigned
After comparing unsigned data
Note:
• If you can understand why blo branches iff ~C
• … then the others follow
49
Conditional Branches: Unsigned
Why does blo branch iff C? Informal explanation:
50
Conditional Branches: Signed
After comparing signed data
Note:
• If you can understand why blt branches iff V^N
• … then the others follow
51
Conditional Branches: Signed
Why does blt branch iff V^N? Informal explanation:
(1) largeposnum – smallposnum (not less than)
• Certainly correct result
• ⇒ V=0, N=0, V^N==0 ⇒ don’t branch
53