Lecture 5
Lecture 5
Lecture 5
Yeongpil Cho
Hanynag University
Topics
• Structure of ARM Assembly Code
• Assembler Directives
2
ARM Instruction Set Architecture
3
History
4
From C to Assembly
.text
…
LDR r0, =x
LDR r1, [r0]
ADD r1, r1, #1
STR r1, [r0]
…
.data
x: .word -2
5
Two Pass Assembly
• Most assemblers read the source file twice.
• First Pass:
▪ Build a symbol table.
▪ Calculate and record values (to be used in the second pass) for
each symbol.
▪ Some symbols may not have a known value.
– They are unresolved.
– The linker will have to fix them.
• Second Pass:
▪ Generate the object file.
▪ Use the symbol table to provide values when needed.
▪ Add information to the object file to tell the linker about any
symbols that are unresolved.
6
Example Code
7
Assembly Listing
8
Syntax of Assembly
• Basic syntax
9
Syntax of Assembly
• Examples
10
Assembly Expressions
• Expressions consist of one or more integer literals or
symbol references, combined using operators.
• Expression can be used as instruction operands or
directive argument.
• Assembler evaluates all expressions.
11
Assembly Expressions
• Constants
▪ Decimal Integer
▪ Hexadecimal integer, prefixed with 0x
▪ Octal integer, prefixed with 0
▪ Binary integer, prefixed with 0b
12
Assembly Expressions
• Operators
▪ Unary Operators: +, -, ~
▪ Binary Operators: +, -, *, /, %
▪ Binary Logical Operators: &&, ||
▪ Binary Bitwise Operators: &. |, ^, >>, <<
▪ Binary Comparison Operators: ==, !=, <, >, <=, >=
13
Assembly Expressions
• Examples
14
Assembly Directives
• String definition
• Data definition
• Alignment
• Space-filling
• Org
• Conditional
• Macro
• Section
• Type
• Symbol Binding
• Instruction Set Selection Directives
15
String definition directives
• Allocates one or more bytes of memory in the current
section, and defines the initial contents of the memory
from a string literal.
• .ascii "string“
▪ .ascii does not append a null byte to the end of the string.
• .asciz "string"
• .string "string“
▪ .asciz and .string append a null byte to the end of the string.
16
String definition directives
• Examples
17
Data definition directives
• These directives allocate memory in the current section, and
define the initial contents of that memory.
18
Data definition directives
• Examples
19
Alignment Directives
• The alignment directives align the current location in the file to a
specified boundary.
20
Alignment Directives
• Examples
21
Space-filling Directives
• .space count [, value]
▪ The .space directive emits count bytes of data, each of which has
value value.
• .fill count [, size [, value]]
▪ The .fill directive emits count data values, each with length size
bytes and value value.
22
Org Directives
• The .org directive advances the location counter in the cu
rrent section to new-location.
23
Org Directives
• Operation
▪ The .org directive can only move the location counter forward,
not backward.
24
Org Directives
• Examples
b: backward
f: forward
25
Conditional Assembly Directives
• These directives allow you to conditionally assemble
sequences of instructions and directives.
• Syntax
26
Conditional Assembly Directives
• Examples
27
Macro Directives
• Syntax
▪ macro_name
– The name of the macro.
▪ parameter_name
– Inside the body of a macro, the parameters can be referred to by
their name, prefixed with \. When the macro is instantiated,
parameter references will be expanded to the value of the
argument.
– Parameters can be qualified in these ways:
28
Macro Directives
• Operation
▪ The .macro directive defines a new macro with name
macro_name. Once a macro is defined, it can be instantiated by
using it like an instruction mnemonic:
• Examples
29
Section Directives
• The section directives instruct the assembler to change the
ELF section that code and data are emitted into.
30
Section Directives
• Examples
▪ Splitting code and data into the built-in .text and .data sections
31
Type Directive
• The default type of a symbol in an object file is the
assembly-time type of the symbol.
▪ Symbolic constants and undefined symbols → @notype
▪ Labels and common symbols → @object
▪ Function names → @function
• The .type directive explicitly sets the type of a symbol.
33
Symbol Binding Directives
• These directives modify the ELF binding of one or more
symbols.
35
Symbol Binding Directives
• Examples
36
Instruction Set Selection Directives
• .arm
▪ The .arm directive instructs the assembler to interpret
subsequent instructions as A32 instructions.
• .thumb
▪ The .thumb directive instructs the assembler to interpret
subsequent instructions as T32 instructions, using the UAL
syntax.
• .thumb_func
▪ This directive specifies that the following symbol is the name of a
Thumb encoded function.
• .syntax [unified | divided]
▪ This directive sets the Instruction Set Syntax.
▪ divided (default for compatibility with legacy)
– ARM and Thumb instructions are used separately
▪ unified
– Enables UAL (Unified Assembly Language) syntax
– Necessary for Thumb2 instructions 37