Vous êtes sur la page 1sur 32

Topic 2

Assembly Programming
- Operations and Operands
Levels of Program Code
n High-level language (translator: compiler)
n Level of abstraction closer to problem domain
n Provides productivity and portability
n Assembly language (translator: assembler)
n Low-level language
n Symbolic representation of binary machine code
n Direct correspondence to machine code
n More readable than machine code
n Machine language
n Binary digits (bits) language of digital circuits
n Composed of instructions (commands for computer)
and data
n Instructions and data encoded in binary digitals
Chapter 2 Instructions: Language of the Computer 2
Processing Different Languages
my_Asm.S
Preprocessor

my_Asm.o linker script


Assembler
other.o
my_C.c/cpp libraries

Complier Assembler Linker Formatter

my_C.s my_C.o my_proj.elf machine code


(machine code) (executable specific to a target
machine code) microprocessor
Preprocessor (.hex, .abs, .exe, etc)
Syntactic Analyzer
Optimizer

Chapter 2 Instructions: Language of the Computer 3


Assembly Language
n When to use?
n Compilers introduce uncertainty about execution
time and size
n Use when speed and size of program are critical
n Can mix high-level language with assembly
n Drawbacks of Assembly language
n Can be very time consuming
n No assembler optimization
n Almost impossible to be portable
n Different computers support different assembly
languages that requires different assembler
n Assembly languages are similar
n Hard to debug

Chapter 2 Instructions: Language of the Computer 4


Instruction Set
n or ISA, all commands that computer
understands including data types, registers,
addressing modes, etc.
n Different computers have different
instruction sets
n But with many common aspects
n Types of
n Reduced Instruction Set Computer RISC
n Complex Instruction Set Computer CISC

Chapter 2 Instructions: Language of the Computer 5


The MIPS Instruction Set
n Used as the example throughout the book
n Originated from Stanford MIPS commercialized by
MIPS Technologies (www.mips.com)
n Large share of embedded core market
n Typical features of many modern ISAs
n See MIPS Reference Card, and Appendixes B and E

Chapter 2 Instructions: Language of the Computer 6


Arithmetic Operations
n Add and subtract, three operands
n Two sources and one destination
add a, b, c # a = b + c
n All MIPS arithmetic operations have this
regular form
n Design Principle 1: Simplicity favors
regularity
n Regularity makes implementation simpler
n Simplicity enables higher performance at
lower cost

Chapter 2 Instructions: Language of the Computer 7


Arithmetic Example
n C/C++ code:
f = (g + h) - (i + j);

n Compiled pseudo-MIPS assembly code:


add t0, g, h # temp t0 = g + h
add t1, i, j # temp t1 = i + j
sub f, t0, t1 # f = t0 - t1

Chapter 2 Instructions: Language of the Computer 8


Operands in MIPS Assembly
n Register operands
n Memory operands
n Immediate operands (constant)

Chapter 2 Instructions: Language of the Computer 9


Register Operands
n Arithmetic instructions use register
operands
n MIPS architecture has a 32 32-bit register file
n Used for frequently accessed data
n Numbered 0 to 31
n Each register is a 32-bit word
n Names recognized by MIPS assembler
n $t0~$t9, $s0~$s7, etc.
n Or $0-$31, accepted by certain assemblers
n Design Principle 2: Smaller is faster

Chapter 2 Instructions: Language of the Computer 10


Register Operands
n $zero: constant 0 (reg 0, also written as $0)
n $at: Assembler Temporary (reg 1, or $1)
n $v0, $v1: result values (regs 2 and 3, or $2 and $3)
n $a0 $a3: arguments (regs 4 7, or $4 - $7)
n $t0 $t7: temporaries (regs 8 15, or $8 - $15)
n $s0 $s7: saved (regs 16 23, or $16 - $23)
n $t8, $t9: temporaries (regs 24 and 25, or $24 and $25)
n $k0, $k1: reserved for OS kernel (regs 26 and 27, $26/27)
n $gp: global pointer for static data (reg 28, or $28)
n $sp: stack pointer (reg 29, or $29)
n $fp: frame pointer (reg 30, or $30)
n $ra: return address (reg 31, or $31)

Chapter 2 Instructions: Language of the Computer 11


Register Operand Example
n C/C++ code:
f = (g + h) - (i + j);
n Put f, g, h, i, and j in $s0, $s1, $s2, $s3, and

$s4, respectively
n Compiled MIPS code:
add $t0, $s1, $s2
add $t1, $s3, $s4
sub $s0, $t0, $t1

Chapter 2 Instructions: Language of the Computer 12


Memory Operands
n Memory used mainly for composite data
n Arrays, structures, dynamic data
n In arithmetic operations
n Load values from memory into registers
n Store result from register to memory
n MIPS memory is byte addressable
n Each address identifies an 8-bit byte
n Memory is organized in words
n Word address must be a multiple of 4 alignment
restriction
n MIPS is Big Endian (except some MIPS extension)
n Most-significant byte at least address of a word
n Little Endian: least-significant byte at least address

Chapter 2 Instructions: Language of the Computer 13


Memory Operand Example 1
n C/C++ code:
g = h + A[8]; //g, h, A are words
n g in $s1, h in $s2, base address of A in $s3

n Compiled MIPS code:


n Index 8 requires offset of 32 (4 bytes/word)

lw $t0, 32($s3) # load word


add $s1, $s2, $t0
offset base address register

Chapter 2 Instructions: Language of the Computer 14


Memory Operand Example 2
n C code:
A[12] = h + A[8];
n h in $s2, base address of A in $s3

n Compiled MIPS code:


n Index 8 requires offset of 32
lw $t0, 32($s3) # load word
add $t0, $s2, $t0
sw $t0, 48($s3) # store word

Chapter 2 Instructions: Language of the Computer 15


Registers vs. Memory
n Registers are faster to access than
memory
n Operating on memory data requires loads
and stores
n More instructions to be executed
n Compiler must use registers for variables
as much as possible
n Only spill to memory for less frequently used
variables
n Register optimization is important!

Chapter 2 Instructions: Language of the Computer 16


Immediate Operands
n Immediate operands constant data
specified in an instruction
addi $s3, $s3, 4
n No subtract immediate instruction
n Just use a negative constant
addi $s2, $s1, -1
n Design Principle 3: Make the common
case fast
n Small constants are common
n Immediate operand avoids loading data from
memery
Chapter 2 Instructions: Language of the Computer 17
The Constant Zero
n MIPS register 0 ($zero) is the constant 0
n Cannot be overwritten
n Useful for common operations
n E.g., move between registers
add $t2, $s1, $zero

Chapter 2 Instructions: Language of the Computer 18


Logical Operations
n Instructions for bitwise manipulation
Operation C Java MIPS
Shift left << << sll
Shift right >> >>> srl
Bitwise AND & & and, andi
Bitwise OR | | or, ori
Bitwise NOT ~ ~ nor

n Useful for extracting and inserting


groups of bits in a word
Chapter 2 Instructions: Language of the Computer 19
Shift Operations
sll/srl rd, rt, shamt
n rt: source register

n rd: destination register

n shamt: how many bits to shift

n Shift left logical

n Shift left and fill vacated bits with 0 bits


n sll by i bits = multiplies by 2i
n Shift right logical
n Shift right and fill vacated bits with 0 bits
n srl by i bits = divides by 2i (unsigned only)
Chapter 2 Instructions: Language of the Computer 20
AND Operations
n Useful to mask bits in a word
n Select some bits, clear others to 0
and $t0, $t1, $t2

$t2 0000 0000 0000 0000 0000 1101 1100 0000

$t1 0000 0000 0000 0000 0011 1100 0000 0000

$t0 0000 0000 0000 0000 0000 1100 0000 0000

Chapter 2 Instructions: Language of the Computer 21


OR Operations
n Useful to include bits in a word
n Set some bits to 1, leave others unchanged
or $t0, $t1, $t2

$t2 0000 0000 0000 0000 0000 1101 1100 0000

$t1 0000 0000 0000 0000 0011 1100 0000 0000

$t0 0000 0000 0000 0000 0011 1101 1100 0000

Chapter 2 Instructions: Language of the Computer 22


NOT Operations
n Useful to invert bits in a word
n Change 0 to 1, and 1 to 0
n MIPS doesnt have NOT instruction,
implemented with NOR instruction
nor $t0, $t1, $zero Register $0: always
read as zero

$t1 0000 0000 0000 0000 0011 1100 0000 0000

$t0 1111 1111 1111 1111 1100 0011 1111 1111

Chapter 2 Instructions: Language of the Computer 23


Load 32-bit Constants
n Most constants are small
n 16-bit immediate is sufficient
n For the occasional 32-bit constant
lui rt, constant
n Copies 16-bit constant to left 16 bits of rt
n Clears right 16 bits of rt to 0

lui $s0, 61 0000 0000 0011 1101 0000 0000 0000 0000

ori $s0, $s0, 2304 0000 0000 0011 1101 0000 1001 0000 0000

Chapter 2 Instructions: Language of the Computer 24


Branch/Jump Operations
n Branch to a labeled instruction if a
condition is true
n Otherwise, continue sequentially
n beq rs, rt, L1
n if (rs == rt) branch to instruction labeled L1;
n bne rs, rt, L1
n if (rs != rt) branch to instruction labeled L1;
n j L1
n unconditional jump to instruction labeled L1

Chapter 2 Instructions: Language of the Computer 25


Compiling If Statements

n C code:
if (i==j) f = g+h;
else f = g-h;
n f,g,h,i,j in $s0, $s1, $s2, $s3, $s4 respectively
n Compiled MIPS code:
bne $s3, $s4, Else
add $s0, $s1, $s2
j Exit
Else: sub $s0, $s1, $s2
Exit: Assembler calculates addresses
Chapter 2 Instructions: Language of the Computer 26
Compiling Loop Statements
n C code:
while (save[i] == k) i += 1;
n i in $s3, k in $s5, address of save in $s6
n Compiled MIPS code:
Loop: sll $t1, $s3, 2
add $t1, $t1, $s6
lw $t0, 0($t1)
bne $t0, $s5, Exit
addi $s3, $s3, 1
j Loop
Exit:

Chapter 2 Instructions: Language of the Computer 27


Basic Blocks
n A basic block is a sequence of instructions
with
n No embedded branches (except at end)
n No branch targets (except at beginning)

n A compiler identifies basic


blocks for optimization
n An advanced processor
can accelerate execution
of basic blocks

Chapter 2 Instructions: Language of the Computer 28


Conditional Operations
n Set result to 1 if a condition is true,
otherwise, set to 0
n slt rd, rs, rt
n if (rs < rt) rd = 1; else rd = 0;
n slti rt, rs, constant
n if (rs < constant) rt = 1; else rt = 0;
n Use in combination with beq, bne
slt $t0, $s1, $s2 # if ($s1 < $s2)
bne $t0, $zero, L # branch to L

Chapter 2 Instructions: Language of the Computer 29


Branch Instruction Design
n Why not blt, bge, etc?
n Hardware for <, , slower than =,
n Combining branch involves more work per
instruction, requiring a slower clock
n All instructions penalized!
n beq and bne are the common cases
n This is a good design compromise

Chapter 2 Instructions: Language of the Computer 30


Assembler Pseudoinstructions
n Most assembly instructions and machine
instructions have one-to-one
correspondence
n Pseudoinstructions: not a real
implementation, assemblers imagination
move $t0, $t1 add $t0, $t1, $zero
blt $t0, $t1, L slt $at, $t0, $t1
bne $at, $zero, L
n $at (register 1): assembler temporary

Chapter 2 Instructions: Language of the Computer 31


Benchmark Programs
n Measure MIPS instruction executions in
benchmark programs
n Consider making the common case fast
n Consider compromises
Instruction class MIPS examples SPEC CPU2006 SPEC CPU2006
INT FP
Arithmetic add, sub, addi 16% 48%
Data transfer lw, sw, lb, lbu, lh, 35% 36%
lhu, sb, lui
Logical and, or, nor, andi, 12% 4%
ori, sll, srl
Cond. Branch beq, bne, slt, slti, 34% 8%
sltiu
Jump j, jr, jal 2% 0%
Chapter 2 Instructions: Language of the Computer 32

Vous aimerez peut-être aussi