EE2028 - S2Ay2122 - Lecture 2 Assembly - Mem Addr
EE2028 - S2Ay2122 - Lecture 2 Assembly - Mem Addr
Assembly Language
Lecture 2
Dr Henry Tan, ECE, NUS
E-mail: [email protected]
pollev.com/henrytan
Lecture 1 Recap
➢ Outline:
▪ 1. Introduction to ARMv7E-M Assembly Language
• 1.1 Why asm?
• 1.2 Calling asm from C Program
• 1.3 ARMv7E-M Glossary: label, optional, Op2, #Imm, Pre- & Suffix
▪ 2. Memory Addressing
• 2.1 Memory Allocation for Data (using Assembler Directives)
• 2.2 Offset Addressing
• 2.3 Offset Addressing – with Pre/Post Index
• 2.4 PC-Relative Addressing
• 2.5 Pseudo-Instruction Addressing
▪ 3. ARMv7E-M Ctrl & Arithmetic Instructions
• 3.1 Move
• 3.2 Add & Subtract
• 3.3 Multiply & Multiply with Accumulate
• 3.4 Compare
• 3.5 Branch
▪ 4. Conditional Execution & Condition Code Suffixes
• 4.1 Conditional Branch
• 4.2 IT Block
▪ 5. ARMv7E-M Logic Instructions
• 5.1 And, Or, Xor
• 5.2 Not
• 5.3 Shift & Rotate
• 5.4 Test
▪ 6. Stack & Subroutines/Functions
[T]EE2028 Lecture 2: ARM Assembly Language
© Dr Henry Tan, ECE NUS
Overview 3
1. Introduction to ARMv7E-M
1.1 Why learn Assembly Language?
➢ Assembly programs are quicker, smaller & have larger
capabilities than those created with high-level languages.
➢ A direct representation of the actual machine language;
through assembly you can have total control of the CPU.
➢ Assembler allows an ideal optimization in programs, be it
their size or their execution speed.
➢ However, developing applications with assembler is
tedious & error-prone.
➢ Combination of C and asm is a powerful method
→ our approach in this module!
[T]EE2028 Lecture 2: ARM Assembly Language
© Dr Henry Tan, ECE NUS
1. Introduction to ARMv7E-M 4
1.2 Calling an Assembler Function
from a C Program
➢ Call assembler function from C program (.c)
▪ extern int my_asm_func(int x, int y);
▪ It will be treated as just another subroutine by C program
▪ Input parameters: R0, R1, R2, R3 (maximum of 4)
▪ Output (return) parameter: R0
➢ Define assembler function in asm program (.s)
▪ my_asm_func: ...
▪ my_asm_func may use BX LR to return to the calling C
program: Branch Indirect (via register): PC LR (will be covered later)
➢ This method is used in Assignment 1
▪ Refer to the assignment skeleton code
[T]EE2028 Lecture 2: ARM Assembly Language
© Dr Henry Tan, ECE NUS
1. Introduction to ARMv7E-M 5
1.3 ARMv7E-M Glossary: i. label
➢ Common instruction format Comments/Remarks are inserted after “@”
➢ #imm8
▪ 28 Range: 0 to 255 or -128 to 127
➢ #imm8m (more flexible than #imm8)
▪ Ignore the details - treat this like #imm8 !
➢ #imm12
▪ 212 Range: 0 to 4095 or
-2048 to 2047
➢ #imm16
▪ 216 Range: 0 to 65535 or
-32768 to 32767
[T]EE2028 Lecture 2: ARM Assembly Language
© Dr Henry Tan, ECE NUS
1. Introduction to ARMv7E-M 9
1.3 ARMv7E-M Glossary: v. Pre- & Suffix
An Arithmetic/Logic/Move instruction may have:
1. Prefix S- or U-
▪ S: perform signed operation of the instruction
▪ U: perform unsigned operation of the instruction
▪ e.g. for division: SDIV {Rd,} Rn, Rm
vs UDIV {Rd,} Rn, Rm
2. Suffix -S (optional)
▪ op{S}: updates condition code flags according to the result
of Arithmetic/Logic & Move operation op
▪ e.g. ADDS {Rd,} Rn, Op2
MOVS Rd, Op2
[T]EE2028 Lecture 2: ARM Assembly Language
© Dr Henry Tan, ECE NUS
1. Introduction to ARMv7E-M 10
1.3 ARMv7E-M Glossary: v. Suffix
(Condition Code Flags)
➢ The Application Program Status Register (APSR, a special register)
contains the following condition flags:
▪ N : Set to 1 if the result of the operation was negative, else cleared to 0
▪ Z : Set to 1 if the result of the operation was zero, else cleared to 0
▪ C : Set to 1 if the operation resulted in a carry, else cleared to 0
▪ V : Set to 1 if the operation caused overflow, else cleared to 0
➢ A Carry (for unsigned operations) occurs:
▪ if the result of an addition is greater than or equal to 232
▪ if the result of a subtraction is positive or zero
▪ as the result of an inline barrel shifter operation in an arithmetic/logic/move
instruction (to be discussed later in the Logic Instructions section)
➢ Overflow (for signed operations) occurs if the result of an add, subtract or
compare is greater than or equal to 231, or less than –231
▪ A static variable retains its value even when the function exits
▪ Its lifetime (or "extent") is the entire run of the program (i.e. global)
▪ .lcomm reserves the specified number of bytes of memory location for
global variable whose value is not yet available at the time of coding
▪ .equ, .word & .lcomm are all assembler directives, not ARM instructions
– no need to include # for the constants specified
Note: NUM1, POINTER and ANSWER in the above examples are all labels (hardcoded)
Memory Registers
0x……40 0x….0123 R2 0x……40
0x40 (Base Register)
0x……44 0x….0456 R3 0x….0456
0x0123
0x……48 0x…. ..40 R4
0x……4C …. …
0X……50 …. …
➢ That is:
Pre-Indexed Addressing while Post-Indexed Addressing
LDR Rd, [Rn, #offset]! LDR Rd, [Rn], #offset
performs performs
Rd [Rn + offset] Rd [Rn]
followed by followed by
Rn Rn + offset Rn Rn + offset
• R2 R2 + 4 = 0x……44
PC = 1004
Offset=1060-1004=56
R1 Operand
Note: decimal memory addresses shown here for easier offset computation.