CMPEN
Lecture
9
331
Addressing Modes
• Addressing modes are the ways of specifying an operand or a memory address.
• It is how an address (memory or register) is determined.
• Instruction type is how the instruction is put together.
• Example: addi, beq, and lw are all I-types instructions.
• addiusesimmediateaddressingmode • bequsespc-relativeaddressing
• lwusesbaseaddressing
Chapter 2 — Instructions: Language of the Computer — 2
Addressing Modes
• IMMEDIATE: a numeric value embedded in the instruction is the actual operand.
• REGISTER: a source or destination operand is specified as content of one of the registers $0-$31. This is used in the jr (jump register) instruction
• PC-RELATIVE: a data or instruction memory location is specified as an offset relative to the incremented PC. This is used in the beq and bne (branch equal, branch not equal) instructions.
• BASE: a data or instruction memory location is specified as assigned offset from a register. This is used in the lw and sw (load word, store word) instructions.
• PSEUDO-DIRECT: the memory address is(mostly) embedded in the instruction. This is used in the j (jump) instruction.
Chapter 2 — Instructions: Language of the Computer — 3
Addressing Modes
• Register addressing Operand is in register
add $s1, $s2, $s3 means $s1 ← $s2 + $s3
• Base addressing Operand is in memory.
The address is the sum of a register and a constant. lw $s1, 32($s3) means $s1 ← M[s3 + 32]
• Immediate addressing The operand is a constant.
addi $s1, $zero, 7 means $s1 ← 0 + 7
• PC-relative addressing
The operand address = PC + an offset Implements position-independent codes.
• Pseudo-direct addressing
Used in the J format. The target address is the concatenation of the 4 MSB’s of the PC with the 28-bit offset. This is a minor variation of the PC-relative addressing format.
Chapter 2 — Instructions: Language of the Computer — 4
Addressing Mode Summary
Chapter 2 — Instructions: Language of the Computer — 5
CMPEN
Lecture 10
331
C
• Illustrates use of assembly instructions for a C bubble sort function
• Swap procedure (leaf)
void swap(int v[], int k)
{
int temp;
temp = v[k];
v[k] = v[k+1];
v[k+1] = temp;
}
• vin$a0,kin$a1,tempin$t0
Chapter 2 — Instructions: Language of the Computer — 7
Swap
Example
§2.13 A C Sort Example to Put It All Together
The Procedure Swap
swap:sll$t1,$a1,2 #$t1=k*4
add $t1, $a0, $t1 # (address of v[k]) lw $t0, 0($t1) # $t0 (temp) = v[k]
lw $t2, 4($t1) # $t2 = v[k+1]
sw $t2, 0($t1) # v[k] = $t2 (v[k+1])
sw $t0, 4($t1) # v[k+1] = $t0 (temp)
jr $ra # return to calling routine
Chapter 2 — Instructions: Language of the Computer — 8
Assembler Pseudoinstructions
• Most assembler instructions represent machine instructions one-to-one
• Pseudoinstructions:
move $t0, $t1 → add $t0, $zero, $t1
blt $t0, $t1, L → slt $at, $t0, $t1 bne $at, $zero, L
• $at(register1):assemblertemporary
Chapter 2 — Instructions: Language of the Computer — 9
Arrays vs. Pointers
• Array indexing involves
• Multiplyingindexbyelementsize • Addingtoarraybaseaddress
• Pointers correspond directly to memory addresses • Canavoidindexingcomplexity
Chapter 2 — Instructions: Language of the Computer — 10
Example: Clearing and Array
clear1(int array[], int size) { int i;
for (i = 0; i < size; i += 1) array[i] = 0;
}
clear2(int *array, int size) { int *p;
for (p = &array[0]; p < &array[size]; p = p + 1)
*p = 0; }
loop1:
move $t0,$zero # i = 0
sll $t1,$t0,2 # $t1 = i * 4 add $t2,$a0,$t1 # $t2 =
# &array[i] sw $zero, 0($t2) # array[i] = 0
addi$t0,$t0,1 #i=i+1 slt $t3,$t0,$a1 # $t3 =
# (i < size)
bne $t3,$zero,loop1 # if (...)
# goto loop1
loop2:
move $t0,$a0 # p = & array[0] sll $t1,$a1,2 # $t1 = size * 4 add $t2,$a0,$t1 # $t2 =
# &array[size] sw $zero,0($t0) # Memory[p] = 0
addi$t0,$t0,4 #p=p+4 slt $t3,$t0,$t2 # $t3 =
#(p<&array[size]) bne $t3,$zero,loop2 # if (...)
# goto loop2
Chapter 2 — Instructions: Language of the Computer — 11
What is a pointer
• In a generic sense, a “pointer” is anything that tells us where something can be found.
• When declaring a variable, the compiler sets aside memory storage with a unique address to store that variable.
• The compiler associates that address with the variable’s name.
• We can manipulate the memory address by using pointers which means that we create a second variable for storing the memory address.
Chapter 2 — Instructions: Language of the Computer — 12
Pointers
§ Let store the nRate's memory address, in pTonRate variable. § So, pTonRate now holds the nRate's memory address, where
the actual data (10) is stored.
§ Pointer variable declaration becomes something like this,
int *pTonRate;
§ The asterisk (*) is used to show that is it the pointer variable instead of normal variable.
Chapter 2 — Instructions: Language of the Computer — 13
,©
Pointers
§ A variable name directly references a value.
§ A pointer indirectly references a value. Referencing a value through a
pointer is called indirection.
§ A pointer variable must be declared before it can be used.
§ C uses two pointer operators,
1. Indirection operator (*) – asterisk symbol, has been explained
previously.
2. Address-of-operator (&) – ampersand symbol, means return the
address of...
§ When & operator is placed before the name of a variable, it will returns the memory address of the variable instead of stored value.
Chapter 2 — Instructions: Language of the Computer — 14
,©
Pointers
int x = 1, y = 2, z[10];
int *ip; /* ip is a pointer to an int */
ip = &x; /* ip points to (contains the memory address of) x */
• & "address operator" which gives or produces the memory address of a data variable
• * "dereferencing operator" which provides the contents in the memory location specified by a pointer
• The * operator is a complement of & operator.
Chapter 2 — Instructions: Language of the Computer — 15
,©
int * pToInt;
pToInt = &nLocation;
nLocation = 100;
Pointers
Chapter 2 — Instructions: Language of the Computer — 16
,©
Example: Clearing and Array
clear1(int array[], int size) { int i;
for (i = 0; i < size; i += 1) array[i] = 0;
}
clear2(int *array, int size) { int *p;
for (p = &array[0]; p < &array[size]; p = p + 1)
*p = 0; }
loop1:
move $t0,$zero # i = 0
sll $t1,$t0,2 # $t1 = i * 4 add $t2,$a0,$t1 # $t2 =
# &array[i] sw $zero, 0($t2) # array[i] = 0
addi$t0,$t0,1 #i=i+1 slt $t3,$t0,$a1 # $t3 =
# (i < size)
bne $t3,$zero,loop1 # if (...)
# goto loop1
loop2:
move $t0,$a0 # p = & array[0] sll $t1,$a1,2 # $t1 = size * 4 add $t2,$a0,$t1 # $t2 =
# &array[size] sw $zero,0($t0) # Memory[p] = 0
addi$t0,$t0,4 #p=p+4 slt $t3,$t0,$t2 # $t3 =
#(p<&array[size]) bne $t3,$zero,loop2 # if (...)
# goto loop2
Chapter 2 — Instructions: Language of the Computer — 17
Comparison of Array vs. Ptr
• Array version requires shift to be inside loop • PartofindexcalculationforincrementedI
• incrementingpointer
• Compiler can achieve same effect as manual use of pointers
• Inductionvariableelimination(eliminatingarray address calculations within loops)
• Bettertomakeprogramclearerandsafer
Chapter 2 — Instructions: Language of the Computer — 18
Translation and Startup
Many compilers produce object modules directly
Chapter 2 — Instructions: Language of the Computer — 19
Static linking
§2.12 Translating and Starting a Program
Producing an Object Module
• Assembler(orcompiler)translatesprograminto machine instructions
• Providesinformationforbuildingacomplete program from the pieces
• Header: described contents of object module
• Text segment: translated instructions
• Static data segment: data allocated for the life of the program
• Relocation info: for contents that depend on absolute location of loaded program
• Symbol table: global definitions
• Debug info: for associating with source code
Chapter 2 — Instructions: Language of the Computer — 20
Linking Object Modules
• Produces an executable image
1. Merges segments
2. Resolve labels (determine their addresses)
3. Patch location-dependent and external refs
Chapter 2 — Instructions: Language of the Computer — 21
Loading a Program
• Load from image file on disk into memory
1. Read header to determine segment sizes
2. Create virtual address space
3. Copy text and initialized data into memory
• Or set page table entries so they can be faulted in
4. Set up arguments on stack
5. Initialize registers (including $sp, $fp, $gp)
6. Jump to startup routine
• Copies arguments to $a0, ... and calls main • When main returns, do exit system call
Chapter 2 — Instructions: Language of the Computer — 22
Dynamic Linking
• Only link/load library procedure when it is called
• Requiresprocedurecodetoberelocatable
• Avoidsimagebloatcausedbystaticlinkingofall (transitively) referenced libraries
• Automaticallypicksupnewlibraryversions
Chapter 2 — Instructions: Language of the Computer — 23
Lazy Linkage
Indirection table
Stub: Loads routine ID, Jump to linker/loader
Linker/loader code
Dynamically mapped code
Chapter 2 — Instructions: Language of the Computer — 24
ARM & MIPS Similarities
• ARM:themostpopularembeddedcore • SimilarbasicsetofinstructionstoMIPS
ARM
MIPS
Date announced
1985
1985
Instruction size
32 bits
32 bits
Address space
32-bit flat
32-bit flat
Data alignment
Aligned
Aligned
Data addressing modes
9
3
Registers
15 × 32-bit
32 × 32-bit
Input/output
Memory mapped
Memory mapped
Chapter 2 — Instructions: Language of the Computer — 25
§2.16 Real Stuff: ARM Instructions
Instruction Encoding
Chapter 2 — Instructions: Language of the Computer — 26
The Intel x86 ISA
• Evolutionwithbackwardcompatibility
• 8080 (1974): 8-bit microprocessor
• Accumulator, plus 3 index-register pairs
• 8086 (1978): 16-bit extension to 8080 • Complex instruction set (CISC)
• 8087 (1980): floating-point coprocessor • Adds FP instructions and register stack
• 80286 (1982): 24-bit addresses
• Segmented memory mapping and protection
• 80386 (1985): 32-bit extension (now IA-32) • Additional addressing modes and operations
• Paged memory mapping as well as segments
Chapter 2 — Instructions: Language of the Computer — 27
§2.17 Real Stuff: x86 Instructions
The Intel x86 ISA
• Furtherevolution...
• i486 (1989): pipelined, on-chip caches and FPU • Compatible competitors: AMD, Cyrix, ...
• Pentium (1993): superscalar, 64-bit datapath
• Later versions added MMX (Multi-Media eXtension) instructions
• The infamous FDIV bug
• Pentium Pro (1995), Pentium II (1997)
• New microarchitecture (see Colwell, The Pentium Chronicles)
• Pentium III (1999)
• Added SSE (Streaming SIMD Extensions) and associated registers
• Pentium 4 (2001)
• New microarchitecture
• Added SSE2 instructions
Chapter 2 — Instructions: Language of the Computer — 28
The Intel x86 ISA
• And further...
• AMD64 (2003): extended architecture to 64 bits
• EM64T – Extended Memory 64 Technology (2004)
• AMD64 adopted by Intel (with refinements) • Added SSE3 instructions
• Intel Core (2006)
• Added SSE4 instructions, virtual machine support
• AMD64 (announced 2007): SSE5 instructions • Intel declined to follow, instead...
• Advanced Vector Extension (announced 2008) • Longer SSE registers, more instructions
• IfInteldidn’textendwithcompatibility,its competitors would!
Chapter 2 — Instructions: Language of the Computer — 29
Basic x86 Registers
Chapter 2 — Instructions: Language of the Computer — 30
x86 Instruction Encoding
• Variable length encoding
• Postfixbytesspecify addressing mode
• Prefixbytesmodify operation
• Operand length, repetition, locking, ...
Chapter 2 — Instructions: Language of the Computer — 31
x86
Typical Operation
Chapter 2 — Instructions: Language of the Computer — 32
Arithmetic for Computers
Chapter 3
Assembly Language for Intel-Based Computers 5/e, 2007.
33
Arithmetic for Computers
• Operations on integers
• Additionandsubtraction • Multiplicationanddivision • Dealingwithoverflow
• Floating-point real numbers
• Representationandoperations
Chapter 3 — Arithmetic for Computers — 34
§3.1 Introduction
Integer Addition
• Example: 7 + 6
n Overflow if result out of range
n Adding +ve and –ve operands, no overflow
n Adding two +ve operands n Overflow if result sign is 1
n Adding two –ve operands n Overflow if result sign is 0
Chapter 3 — Arithmetic for Computers — 35
§3.2 Addition and Subtraction
Integer Subtraction
• Addnegationofsecondoperand
• Example:7–6=7+(–6)
+7: 0000 0000 ... 0000 0111 –6: 1111 1111 ... 1111 1010 +1: 0000 0000 ... 0000 0001
• Overflowifresultoutofrange
• Subtracting two +ve or two –ve operands, no overflow
• Subtracting +ve from –ve operand • Overflow if result sign is 0
• Subtracting –ve from +ve operand • Overflow if result sign is 1
Chapter 3 — Arithmetic for Computers — 36