11 Risc Cisc and Assemblers I
11 Risc Cisc and Assemblers I
11 Risc Cisc and Assemblers I
Hakim Weatherspoon
CS 3410, Spring 2013
Computer Science
Cornell University
See P&H Appendix B.1-2, and Chapters 2.8 and 2.12; als 2.16 and 2.17
Big Picture: Where are we now?
compute
jump/branch
targets
A
memory register
D
alu
file
B
+4
addr
inst
PC din dout
M
control
B
memory
imm
extend
new
forward
pc detect
unit
hazard
Instruction Instruction Write-
ctrl
ctrl
ctrl
Fetch Decode Execute Memory Back
IF/ID ID/EX EX/MEM MEM/WB
Big Picture: Where are we going?
C int x = 10;
compiler x = 2 * x + 15;
r0 = 0
MIPS
addi r5, r0, 10 r5 = r0 + 10
assembly muli r5, r5, 2 r5 = r5<<1 #r5 = r5 * 2
assembler addi r5, r5, 15 r5 = r15 + 15
op = addi r0 r5 10
machine 00100000000001010000000000001010
code 00000000000001010010100001000000
00100000101001010000000000001111
op = addi r5 r5 15
CPU
op = r-type r5 r5 shamt=1 func=sll
Circuits
Gates
Transistors
3
Silicon
Goals for Today
Instruction Set Architectures
• ISA Variations (i.e. ARM), CISC, RISC
Next Time
• Program Structure and Calling Conventions
Next Goal
Is MIPS the only possible instruction set
architecture (ISA)?
What are the alternatives?
MIPS Design Principles
Simplicity favors regularity
• 32 bit instructions
Smaller is faster
• Small register file
I-type op rs rt immediate
6 bits 5 bits 5 bits 16 bits
Gates
Transistors
25
Silicon
Assembler
Translates text assembly language to binary
machine code
libc.o
Compiler
libm.o
Assembler linker
Assembler
Translates text assembly language to binary
machine code
Control flow
• BEQ, BNE, BLEZ, BLTZ, BGEZ, BGTZ
• J, JR, JAL, JALR, BEQL, BNEL, BLEZL, BGTZL
Special
• LL, SC, SYSCALL, BREAK, SYNC, COPROC
Pseudo-Instructions
Pseudo-Instructions
NOP # do nothing
• SLL r0, r0, 0
MOVE reg, reg # copy between regs
• ADD R2, R0, R1 # copies contents of R1 to R2
LI reg, imm # load immediate (up to 32 bits)
LA reg, label # load address (32 bits)
B label # unconditional branch
BLT reg, reg, label # branch less than
• SLT r1, rA, rB # r1 = 1 if R[rA] < R[rB]; o.w. r1 = 0
• BNE r1, r0, label # go to address label if r1!=r0; i.t. rA < rB
Program Layout
Programs consist of segments
used for different purposes
• Text: holds instructions “cornell cs”
13
• Data: holds statically allocated data
25
program data such as
variables, strings, etc.
add r1,r2,r3
text
ori r2, r4, 3
...
Assembling Programs
Assembly files consist of a mix of
.text
+ instructions
.ent main
main: la $4, Larray + pseudo-instructions
li $5, 15 + assembler (data/layout) directives
...
(Assembler lays out binary values
li $4, 0
jal exit in memory based on directives)
.end main Assembled to an Object File
.data • Header
Larray: • Text Segment
.long 51, 491, 3991 • Data Segment
• Relocation Information
• Symbol Table
• Debugging Information
Assembling Programs
Assembly with a but using (modified) Harvard
architecture
• Need segments since data and program stored
together in memory
Registers 00100000001
Control 00100000010
data, address, 00010000100
ALU ...
control
Data
CPU 10100010000
Memory
10110000011
00100010101
...
Program
Memory
Takeaway
Assembly is a low-level task
• Need to assemble assembly language into machine
code binary. Requires
• Assembly language instructions
• pseudo-instructions
• And Specify layout and data using assembler directives