Professional Documents
Culture Documents
Angel E. Gonzalez-Lizardo,
Ph.D.
Polytechnic University of Puerto Rico
Angel E. Gonzalez-Lizardo,
Ph.D. 1
Introduction
The language of computers is called instructions.
Their vocabulary is called Instruction Set
Computer designers have a common goal:
To find a language that makes easy to build the hardware
and the compiler while maximizing performance and
minimizing cost.
Simplicity of the equipment is a valuable consideration.
The secret of computing: The stored program
Both instructions and data are stored as numbers in the computer.
Angel E. Gonzalez-Lizardo,
Ph.D. 2
# a = b + c;
translates into
add a , b , c
add a , a , d
add a , a , e
Angel E. Gonzalez-Lizardo,
Ph.D. 3
MIPS Assembly
The text after the # is a comment.
Comments always terminate at the end of the line.
Each instruction has three operands, no less, no more.
Angel E. Gonzalez-Lizardo,
Ph.D. 4
Angel E. Gonzalez-Lizardo,
Ph.D. 5
Angel E. Gonzalez-Lizardo,
Ph.D. 6
Instruction
Example
Meaning
Comments
Arithmetic
add
subract
add a, b, c
sub a, b, c
a=b+c
a=bc
Angel E. Gonzalez-Lizardo,
Ph.D. 7
Operands in a Computer
Registers
Operands of arithmetic instructions are always registers.
Registers in the MIPS-32 architecture are 32-bits wide.
The name word is given to such groups of 32-bits
MIPS-32 architecture has only 32 registers.
The reason for that is the Second Design Principle:
Smaller is faster
Angel E. Gonzalez-Lizardo,
Ph.D. 8
Operands in a Computer
Registers
Example: Same as before but using registers
f = ( g + h ) -( i + j );
Assuming registers $s0, $s1, $s2, $s3, and $s4 are assigned to f, g, h, i, and j
respectively,
add $t0 , $s1 , $s2
add $t1 , $s3 , $s4
sub $s0 , $t0 , $t1
# register t0 becomes g + h
# register t1 becomes i + j
# $s0 gets the final result
Registers called $sx are used for variables and the ones called $tx
are used for temporary variables.
Angel E. Gonzalez-Lizardo,
Ph.D. 9
Operands in a Computer
Registers
The processor can keep only a limited number of data elements.
Data structures and arrays must be kept in memory.
Data Transfer Instructions move data from memory to processor
and viceversa.
To access a word in memory, the instruction must provide a
memory address.
Memory is just a large single dimensional array with the address
acting like an index.
Angel E. Gonzalez-Lizardo,
Ph.D. 10
Operands in a Computer
Angel E. Gonzalez-Lizardo,
Ph.D. 11
Operands in a Computer
Example: Memory operation
The instruction
g = h + A [8]
# Temporary register
# g = h + A [8]
The constant (32) in the data transfer is called the Offset while the
register ($s3) is called the Base Register.
Effective Address = Offset + Base Register
Angel E. Gonzalez-Lizardo,
Ph.D. 12
Operands in a Computer
Compiler
The compiler:
Associates register with variables.
Allocates arrays and structures memory locations in memory.
Places the right data address into the data transfer instructions.
In MIPS a word address must be multiple of 4.
This is called Alignment Restriction.
Angel E. Gonzalez-Lizardo,
Ph.D. 13
Operands in a Computer
Immediate Instructions
A constant in the instruction is called an immediate operand.
More than half of MIPS arithmetic instructions use immediate
operands.
Since immediate operands are very frequent, immediate
instructions are included
The instruction
addi $s3 , $s3 , 4
# $s3 = $s3 + 4
Angel E. Gonzalez-Lizardo,
Ph.D. 14
Immediate Instructions
Angel E. Gonzalez-Lizardo,
Ph.D. 15
Immediate Instructions
Angel E. Gonzalez-Lizardo,
Ph.D. 16
Instructions Formats
R-type Instructions
How machine represent numbers? binary format
For example 12310 = 11110112 .
MIPS Fields.
The instruction is divided into fields
Each field specifies part of the information needed for
execution.
The R-type (for Register) instruction format is:
op
rs
rt
rd
shamt
function
6 bits
5 bits
5 bits
5 bits
5 bits
6 bits
Angel E. Gonzalez-Lizardo,
Ph.D. 17
Instructions Formats
R-type Instructions
op. Basic operation of the instruction, called opcode.
rs. The first register source operand.
rt. The second register source operand.
rd. The destination register where the result is stored.
shamt. Shift amount, used for the shift instructions, specify how the
shift is done.
function. Function, used for selecting a variant of the operation
specified by the op field.
Angel E. Gonzalez-Lizardo,
Ph.D. 18
Instructions Formats
Angel E. Gonzalez-Lizardo,
Ph.D. 19
Instructions Formats
I-type Instructions
The I-type (for immediate) instruction used for data transfer is:
The 16-bit address means a load word instruction can load any
word within a region of 215 or 32, 768 bytes (213 or 8, 192 words)
of the address in the base register.
op
rs
rt
6 bits
5 bits
5 bits
immediate
16 bits
Angel E. Gonzalez-Lizardo,
Ph.D. 20
Instructions Formats
Instruction Types so far
Angel E. Gonzalez-Lizardo,
Ph.D. 21
Instructions Formats
Assembly to Machine Language
Lets translate the instruction
A [300] = h + A [300]
which is compiled into
Angel E. Gonzalez-Lizardo,
Ph.D. 22
Instructions Formats
Assembly to Machine Language
In MIPS, registers $s0 to $s7 map onto registers 16 to 23.
Registers $t0 to $t7 map onto registers 8 to 15.
Thus, $s3 is register 18, $t0 is register 8, and $t1 is register 9.
The binary representation of the instruction is in Figure 4.
Angel E. Gonzalez-Lizardo,
Ph.D. 23
Instructions Formats
Angel E. Gonzalez-Lizardo,
Ph.D. 24
Instructions Formats
Summary
Angel E. Gonzalez-Lizardo,
Ph.D. 25
Other Instructions
Logic Instructions
Bitwise operations are useful in a computer.
Logical operations in Java or C translate directly to MIPS assembly.
Table 2 shows a summary of MIPS logical operations.
Table: C or Java to MIPS
Logical Operations
Java
MIPS
Shift Left
<<
<<
sll
Shift Right
>>
>>>
srl
Bit-by-Bit AND
&
&
and, andi
Bit-by-Bit OR
or, ori
Bit-by-Bit NOR
nor
Angel E. Gonzalez-Lizardo,
Ph.D. 26
Other Instructions
Logic Instructions
The first two operations are called shifts.
They move all the bits in the word to the left or right,
The emptied bits are filled with zeros.
For example if the register $s0 contains
0000 0000 0000 0000 0000 0000 0000 1001 = 9 ,
Angel E. Gonzalez-Lizardo,
Ph.D. 27
Other Instructions
Logic Instructions
The machine version of the instruction is:
op
rs
rt
rd
shamt
funct
16
10
AND
Angel E. Gonzalez-Lizardo,
Ph.D. 28
Other Instructions
Logic Instructions
If the instruction executed was or $t0, $t1, $t2, the result would be:
$t0 = 0000 0000 0000 0000 0011 1101 0000 0000
Angel E. Gonzalez-Lizardo,
Ph.D. 29
Other Instructions
Angel E. Gonzalez-Lizardo,
Ph.D. 30
Other Instructions
Decision-Making Instructions
What distinguishes a computer from a calculator?
Its ability to make decisions.
MIPS assembly includes two decision-making instructions.
The instructions are branch if equal (beq), and branch if not
equal (bne)
beq reg1 , reg2 , L1
bne reg1 , reg2 , L1
Angel E. Gonzalez-Lizardo,
Ph.D. 31
Other Instructions
Decision-Making Instructions
Example. Compiling an if statement into a conditional branch. In the
following code segment, f g, h, i, and j are variables.
if ( i == j ) f = g + h ; else f = f - i ;
Assuming that the five variables f through j correspond to the five registers $s0 to $s4,
what is the compiled MIPS code?
Answer
beq $s3 , $s4 , Else
sub $s0 , $s0 , $s3
j Exit
Else : add $s0 , $s1 , $s2
Exit :
# go to Else if i equals j
# f = f - i
# go to exit
# f = g + h
Angel E. Gonzalez-Lizardo,
Ph.D. 32
Other Instructions
Decision-Making Instructions
The compilers frequently
create branches where they do
not appear in the original code.
Angel E. Gonzalez-Lizardo,
Ph.D. 33
Basic Programming
Loops
Decision making instructions are also used for loops.
Compiling a loop with a variable array index Here is a C loop:
Loop :
g = g + A [ i ];
i = i + j;
if ( i != h ) go to Loop ;
Assume A is an array of 100 elements and that g, h, i, and j are associated to registers $s1 to $s4
by the compiler, and $s5 holds the base address of A.
Loop :
#
#
#
#
#
#
#
Angel E. Gonzalez-Lizardo,
Ph.D. 34
Basic Programming
Basic Blocks
These sequence of instructions that end in a branch are so
fundamental to compiling that they have got their own buzzword :
Basic Block.
A Basic Block is a sequence of instructions with
No embedded branches (except at end)
No branch targets (except at beginning)
A compiler identifies basic blocks for optimization
An advanced processor can accelerate execution of basic blocks
Angel E. Gonzalez-Lizardo,
Ph.D. 35
Basic Programming
Example. While Programming
Compile the C code:
while ( save [ i ] == k )
i = i + j;
Assuming i, j, and k are associated to registers $s3, $s4 and $s5 by the compiler, and $s6 holds
the base address of save. Answer:
Loop :
#
#
#
#
#
#
#
2* i
4* i
of save [ i ]
save [ i ]
save [ i ] does not eq
Exit :
Angel E. Gonzalez-Lizardo,
Ph.D. 36
Basic Programming
Angel E. Gonzalez-Lizardo,
Ph.D. 37
Basic Programming
Set if Less Than
What is the code to test if variable a associated to $s0 is less than
variable b in $s1, and then branch to the label Less, if the condition
holds?
Answer:
Less : slt $t0 , $s0 , $s1
bne $t0 , $zero , Less
Exit :
Angel E. Gonzalez-Lizardo,
Ph.D. 38
Basic Programming
Case/Switch Statement
Most programming languages have a case or switch statement.
One way of implementing a switch is through a sequence of
if-then-else.
Another way is using a table of addresses called a jump address
table.
To support this situation MIPS provides the instruction jump
register (jr), an unconditional jump to the address specified in a
register.
Angel E. Gonzalez-Lizardo,
Ph.D. 39
Basic Programming
Case/Switch Statement.
Example: Compiling a switch Statement using a Jump Address
Table.
Consider the code:
switch ( k ) {
case
case
case
case
}
0:
1:
2:
3:
f
f
f
f
=
=
=
=
i
g
g
i
+
+
-
j;
h;
h;
j;
break
break
break
break
;
;
;
;
/*
/*
/*
/*
k
k
k
k
=
=
=
=
0*/
1*/
2*/
3*/
Assume the six variables are contained in registers $s0 to $s5 and that register $t2
contains 4.
Angel E. Gonzalez-Lizardo,
Ph.D. 40
Basic Programming
Case/Switch Statement
Answer
The switch variable k is used to index a jump address table, then jump via the loaded value.
First we make sure k is in the test range.
slt
bne
slt
beq
$t3 ,
$t3 ,
$t3 ,
$t3 ,
$s5 , $zero
$zero , Exit
$s5 , $t2
$zero , Exit
#
#
#
#
$t3 =1 if k < 0
if k < 0 go to Exit
Test if k < 4
if k >= 4 go to Exit
Angel E. Gonzalez-Lizardo,
Ph.D. 41
Basic Programming
Case/Switch Statement
Answer
The instruction jr jumps to the address specified in a register.
jr $t0
Finally, the cases
L0 : add $s0 ,
j Exit
L1 : add $s0 ,
j Exit
L2 : sub $s0 ,
j Exit
L4 : sub $s0 ,
Exit :
$s3 , $s4
# f = i + j
$s1 , $s2
# f = g + h
$s3 , $s4
# f = g - h
$s1 , $s2
# f = i - j
Angel E. Gonzalez-Lizardo,
Ph.D. 42
Angel E. Gonzalez-Lizardo,
Ph.D. 43
Angel E. Gonzalez-Lizardo,
Ph.D. 44
Supporting Procedures
Procedures
Procedures are tools used with two purposes:
1
Make the code easier to understand.
2
Make the code reusable.
Procedures: programs that concentrate in a portion of the task.
Parameters
Allow for separation between the procedure and the rest of the
program and data.
Allow to pass values and return results.
Angel E. Gonzalez-Lizardo,
Ph.D. 45
Supporting Procedures
Procedures
To execute procedures the program must follow six steps:
1
Place the results where the main program can access them.
Angel E. Gonzalez-Lizardo,
Ph.D. 46
Supporting Procedures
Register Allocation
As mentioned, register are the fastest place to hold data.
They must be used as much as possible.
MIPS allocates the following registers for procedure calling:
$a0-$a3: four arguments register to pass parameters.
$v0-$v1: two value register to return values.
$ra: one return address register to return the point of origin.
MIPS includes an instruction just to call procedures: jal
ProcedureAddress
The instruction is jump-and-link save the return address in $ra and
jumps to the target address.
Angel E. Gonzalez-Lizardo,
Ph.D. 47
Supporting Procedures
Register Allocation
The execution of a procedure
The caller program places the parameters in $a0 to $a3
The caller program uses jal X to jump to the procedure X.
The callee program (the procedure) perform its calculations.
The callee program returns control using jr $ra.
Angel E. Gonzalez-Lizardo,
Ph.D. 48
Supporting Procedures
Stack
If more registers for parameters are needed,
If the procedure uses more than 4 register, a stack is used
Any register needed by the caller must be restored to their original
values before the procedure was invoked.
This is called spilling registers.
MIPS software allocates another register for the stack called the
stack pointer, $sp.
MIPS stacks grow form higher address to lower address
This convention means you push values into the stack by
subtracting from the stack pointer.
Adding to the stack pointer shrinks the stack, popping values off the
stack.
COE5320 Computer Architecture
Angel E. Gonzalez-Lizardo,
Ph.D. 49
Supporting Procedures
Leaf Procedure
Example: Compiling a Procedure that does not call another procedure.
Consider the code:
int leaf_example ( int g , int h , int i , int j )
{
int f ;
f = ( g + h ) - ( i - j );
return f ;
}
The compiled program has three parts, saving the registers for the caller,
performing the computations, and restoring the registers:
Angel E. Gonzalez-Lizardo,
Ph.D. 50
Supporting Procedures
Leaf Procedure
The parameters g, h, i, and j correspond to the argument registers $a0 to $a3.,
and f correspond to $s0.
leaf_example :
addi $sp , $sp , -12 # make room in the stack for 3 items .
sw $t1 , 8( $sp )
# save register $t1 to use afterwards
sw $t0 , 4( $sp )
# save register $t0 to use afterwards
sw $s0 , 0( $sp )
# save register $s0 to use afterwards
add $t0 , $a0 , $a1
# $t0 contains g + h
sub $t1 , $a2 , $a3
# $t1 contains i - j
sub $s0 , $t0 , $t1
# f = (g + h) - (i - j)
add $v0 , $s0 , $zero
# returns f
lw $s0 , 0( $sp )
# restore register $s0 for the caller
lw $t0 , 4( $sp )
# restore register $t0 for the caller
lw $t1 , 8( $sp )
# restore register $t1 for the caller
addi $sp , $sp , 12
# adjust the stack pointer back
jr $ra
# jump back to the calling routine
Angel E. Gonzalez-Lizardo,
Ph.D. 51
Supporting Procedures
Angel E. Gonzalez-Lizardo,
Ph.D. 52
Supporting Procedures
Nested Procedures
Procedure that do not call other procedures are called leaf
procedures.
Life would be simpler if all procedures were leaf.
If a procedure A calls a procedure B, both using $a0 to pass
parameters, B must preserve the value of $a0 for A.
One solution
Angel E. Gonzalez-Lizardo,
Ph.D. 53
Supporting Procedures
Nested Procedures
Lets consider the procedure that computes the factorial:
int fact ( int n )
{
if ( n < 1) return (1)
else return ( n * fact (n -1));
}
Assuming we can add or subtract constants, what is the MIPS code for
this procedure?
Angel E. Gonzalez-Lizardo,
Ph.D. 54
Supporting Procedures
Nested Procedures
The parameter n correspond to the argument register $a0. Hence, the value of $a0
must be pushed on the stack.
fact :
addi $sp , $sp , -8
sw $ra , 4( $sp )
sw $a0 , 0( $sp )
Angel E. Gonzalez-Lizardo,
Ph.D. 55
Supporting Procedures
Nested Procedures
If n < 1, fact returns 1 by putting 1 into a value register.
# returns 1
# pops two items off the stack
# return to after jal
# decrement n
# calls fact again
lw $a0 , 0( $sp )
lw $ra , 4( $sp )
addi $sp , $sp , 8
# restore argument
# restore the return address
# pops two items off the stack
# n * fact (n -1)
# return to the caller
Angel E. Gonzalez-Lizardo,
Ph.D. 56
Supporting Procedures
Nested Procedures
Registers $a0-$a3, $s0-$s7, and stack pointer are preserved.
Registers $v0-$v1, $t0-$t9 are not preserved.
The stack above the stack pointer is preserved.
The stack below the stack pointer is not preserved.
Angel E. Gonzalez-Lizardo,
Ph.D. 57
Supporting Procedures
Nested Procedures
Variables local to the procedures are also stored in the stack.
This is done when variables do not fit in the registers.
The procedure frame or activation record is the stack segment
containing a procedure saved register and local variables.
Some MIPS software use a frame pointer ($fp) to point the first
word of a procedure.
Hence, the $fp points to the begin of the procedure frame and the
$sp points to its end.
Angel E. Gonzalez-Lizardo,
Ph.D. 58
Supporting Procedures
Nested Procedures
C language has two storage classes: automatic and static.
Automatic variables are local data discarded when the procedure
exits.
Static variables exist across exits from procedures.
To ease the access to static data MIPS reserves another register
called global pointer, $gp.
Angel E. Gonzalez-Lizardo,
Ph.D. 59
Nested Procedures
The frame pointer points to the first word saved by the
procedure
Figure: Stack Allocation (a) before, (b) during and (c) after the procedure call.
COE5320 Computer Architecture
Angel E. Gonzalez-Lizardo,
Ph.D. 60
Summary
Register Mapping
Figure: MIPS register convention. Register 1, called $at, is used by the assembler and
register 26 and 27, called $k0 and $k1, are reserved to the operating system
Angel E. Gonzalez-Lizardo,
Ph.D. 61
Summary
Figure: Summary
COE5320 Computer Architecture
Angel E. Gonzalez-Lizardo,
Ph.D. 62
Summary
Figure:
COE5320 Computer Architecture
Angel E. Gonzalez-Lizardo,
Ph.D. 63
Communication
Computers were created to crunch numbers
Most computers today use the American Standard Code for
Information Interchange (ASCII) code to represent characters.
ASCII codes characters are 8-bit wide.
MIPS provides special instructions to move bytes.
Load byte (lb) loads a byte from memory placing it in the
rightmost 8 bits of a register.
Store byte (sb) takes a byte from the rightmost 8 bits of a
register and writes them into memory.
Angel E. Gonzalez-Lizardo,
Ph.D. 64
Angel E. Gonzalez-Lizardo,
Ph.D. 65
Angel E. Gonzalez-Lizardo,
Ph.D. 66
Communication
Example: Compiling a string copy procedure (C style).
Angel E. Gonzalez-Lizardo,
Ph.D. 67
strcpy :
addi $sp , $sp , -4
sw $s0 , 4( $sp )
add $s0 , $zero , $zero
L1 : add $t1 , $a1 , $s0
lb $t2 , 0( $t1 )
add $t3 , $a0 , $s0
sb $t2 , 0( $t3 )
addi $s0 , $s0 , 1
bne $t2 , $zero , L1
lw $s0 , 4( $sp )
addi $sp , $sp , 4
jr $ra
Angel E. Gonzalez-Lizardo,
Ph.D. 68
Unicode
There is Universal Encoding or Unicode using 16 bits to represent
a character.
Java, for example uses unicode.
MIPS have a set of instructions to load an store halfwords or
16-bits quantities.
These instructions will not be treated at the moment, but revised
later.
Angel E. Gonzalez-Lizardo,
Ph.D. 69
Why Immediates
A constant can be included in the instruction via the I-type
instructions.
52% of the arithmetic instructions in the gcc compiler use an
immediate operand
69% of the instructions of spice use an immediate operand.
Observe the sequence:
sw $t0 AddrConstant4 ( $zero ) # $t0 = constant 4
add $sp , $sp , $t0
# sp = sp + t0
Angel E. Gonzalez-Lizardo,
Ph.D. 70
Why Immediates
Example: Translating assembly constants into machine language
The add immediate instruction addi adds a constant to a register
addi $sp , $sp , 4
# $sp = $sp +4
The op field for addi is 8. Try to guess the rest of the fields in the
corresponding machine instruction.
Angel E. Gonzalez-Lizardo,
Ph.D. 71
Why Immediates
We know that register $sp maps to register 29 of the register file, so fields rt and rs of
the instruction must be 29. The immediate field contains the constant in the instruction.
op
rs
rt
Immediate
29
29
In binary format:
op
001000
rs
11101
rt
11101
Immediate
0000 0000 0000 0100
Angel E. Gonzalez-Lizardo,
Ph.D. 72
Why Immediates
Immediate operands are also popular for comparisons.
Then MIPS has the instruction :
slti
$t0 , $t2 , 10
Angel E. Gonzalez-Lizardo,
Ph.D. 73
Why Immediates
Make the common case Fast
Constant operands are frequent in arithmetic operations.
Making the operand part of the instruction is much faster than
accessing memory to get them.
Then, immediate addressing is implemented to make common
cases faster.
Angel E. Gonzalez-Lizardo,
Ph.D. 74
26 bits
10000
where the opcode of the jump is 2 and the jump address is 10000.
Angel E. Gonzalez-Lizardo,
Ph.D. 75
This is assembled as
5
16
17
Exit
6 bits
5 bits
5 bits
16 bits
(1)
or in other words
(2)
Angel E. Gonzalez-Lizardo,
Ph.D. 76
Angel E. Gonzalez-Lizardo,
Ph.D. 77
is replaced by
bne $s0 , $s1 , L2
j
L1
L2 :
Angel E. Gonzalez-Lizardo,
Ph.D. 78
Jump Instruction
26-bit address
Angel E. Gonzalez-Lizardo,
Ph.D. 79
Addressing Modes
The five MIPS addressing modes
Register Mode: Where the operand is a register.
Base or displacement addressing: Where the operand is at the
memory location whose address is the sum of a register and a
constant in the instruction.
Immediate addressing: Where the operand is a constant within
the instruction.
PC-relative addressing: Where the address is the sum of the PC
and a constant in the instruction.
Pseudo direct addressing: Where th jump address is the 26 bits
of the instruction concatenated with the upper bits of the PC.
Angel E. Gonzalez-Lizardo,
Ph.D. 80
Addressing Modes
The five MIPS addressing modes
Angel E. Gonzalez-Lizardo,
Ph.D. 81
Angel E. Gonzalez-Lizardo,
Ph.D. 82
Angel E. Gonzalez-Lizardo,
Ph.D. 83
Compilers
Translate a C language program into an assembly language
program.
High level language programs: fewer lines than assembly.
In the 70s many operating systems were written in assembly
because of small memories and inefficient compilers.
As memory capacity increased and compilers improved assembly
programming was not indispensable.
Angel E. Gonzalez-Lizardo,
Ph.D. 84
is in fact executed as a
add $t0 , $zero , $t1
Angel E. Gonzalez-Lizardo,
Ph.D. 85
Assembler
The assembler turns the program into a Object File
The object file is a combination of
Machine language instructions.
Data.
Information needed to place the program in memory.
Assembler keeps track of the labels used by the program in a
symbol table containing pairs of symbols-addresses
Angel E. Gonzalez-Lizardo,
Ph.D. 86
Angel E. Gonzalez-Lizardo,
Ph.D. 87
Angel E. Gonzalez-Lizardo,
Ph.D. 88
Angel E. Gonzalez-Lizardo,
Ph.D. 89
Reads the file header to determine the size of the text and data segments.
Creates an address space large enough for the text and data.
Copies the parameters (if any) to the main program onto the stack.
Initializes the machine registers and sets the stack pointer to the first free
location.
Jumps to a start-up routine that copies the parameters into the argument
registers and calls the main routine of the program.
When the main routine returns, the start-up routine terminates the
program with an exit system call
Angel E. Gonzalez-Lizardo,
Ph.D. 90
Examples
Angel E. Gonzalez-Lizardo,
Ph.D. 91
Angel E. Gonzalez-Lizardo,
Ph.D. 92
Examples
Register Allocation
$a0-$a3 are the registers to pass parameters to procedures.
swap has only two parameters v and k, and one additional variable
temp.
Then $a0 and $a1 are associated with v and k, while temp is
associated with $t0.
We use $t0 since swap is a leaf procedure.
Angel E. Gonzalez-Lizardo,
Ph.D. 93
Examples
Produce code
First multiply the index by 4
add
add
add
# $t1 = k *2
# $t1 = k *4
# $t1 = v +( k *4) , the address of v
$t0 , 0( $t1 )
$t2 , 4( $t1 )
# loads v [ k ] in t0
# loads v [ k +1] in t2
$t2 , 0( $t1 )
$t0 , 4( $t1 )
jr
$ra
# v [ k ] = $t2
# v [ k + 1] = $t0
Angel E. Gonzalez-Lizardo,
Ph.D. 94
Examples
sort Procedure
sort ( int v [] int n )
{
int i j ;
for ( i = 0; i < n ; i = i + 1){
for ( j =i -1; j >=0 && v [ j ] > v [ j +1]; j =j -1) { swap (v , j )
}
}
}
Assume that i is in $s0, j is in $s1, v base address is in $s2, and n is in $s3.
Angel E. Gonzalez-Lizardo,
Ph.D. 95
Examples
sort Procedure
Saving Registers :
sort :
addi
sw
sw
sw
sw
sw
Parameter saving :
move
$s2 , $a0
move
$s3 , $a1
Outer Loop :
move
for1tst : slt
beq
$s0 , $zero
$t0 , $s0 , $s3
$t0 , $zero , exit1
Angel E. Gonzalez-Lizardo,
Ph.D. 96
Examples
sort Procedure
Inner Loop :
addi
for2tst : slti
bne
add
add
add
lw
lw
slt
beq
$s1 ,
$t0 ,
$t0 ,
$t1 ,
$t1 ,
$t2 ,
$t3 ,
$t4 ,
$t0 ,
$t0 ,
$s0 , -1
$s1 , 0
$zero , exit2
$s1 , $s1
$t1 , $t1
$s2 , $t1
0( $t2 )
4( $t2 )
$t4 , $t3
$zero , exit2
$a0 , $s2
$a1 , $s1
swap
Angel E. Gonzalez-Lizardo,
Ph.D. 97
Examples
sort Procedure
Inner loop
addi
j
$s1 , $s1 , -1
for2tst
outer loop
exit2 : addi
j
$s0 , $s0 , 1
for1tst
Restoring Registers
exit1 :
lw
lw
lw
lw
lw
addi
jr
$s0 ,
$s1 ,
$s2 ,
$s3 ,
$ra ,
$sp ,
$ra
0( $sp )
4( $sp )
8( $sp )
12( $sp )
16( $sp )
$sp , 20
Angel E. Gonzalez-Lizardo,
Ph.D. 98
Examples
Arrays vs. Pointers
Modern optimizing compilers can produce just as good code for pointer or arrays.
Consider the code
Angel E. Gonzalez-Lizardo,
Ph.D. 99
Examples
Angel E. Gonzalez-Lizardo,
Ph.D. 100
Examples
move
add
add
add
sw
addi
slt
bne
$t0 , $zero
# i = 0
$t1 , $t0 , $t0
# i = i *2
$t1 , $t1 , $t
# i = i *4
$t2 , $a0 , $t
# $t2 = address of array [ i ]
$zero , 0( $t2 )
# array [ i ] = 0
$t0 , $t0 , 1
# i = i +1
$t3 , $t0 , $a1
# $t3 = (i < size )
$t3 , $zero , loop1
# if (i < size ) go to loop1
Angel E. Gonzalez-Lizardo,
Ph.D. 101
Examples
Pointer version of clear
Assume $a0 and $a1 hold array and size respectively. p is allocated in
register $t0.
loop2 :
move
sw
addi
add
add
add
slt
bne
$t0 , $a0
$zero , 0( $t0 )
$t0 , $t0 , 4
$t1 , $a1 , $a1
$t1 , $t1 , $t1
$t2 , $a0 , $t1
$t3 , $t0 , $t2
$t3 , $zero , loop2
#
#
#
#
#
#
#
#
Angel E. Gonzalez-Lizardo,
Ph.D. 102
Examples
loop2 :
move
add
add
add
sw
addi
slt
bne
$t0 , $a0
$t1 , $a1 , $a1
$t1 , $t1 , $t1
$t2 , $a0 , $t1
$zero , 0( $t0 )
$t0 , $t0 , 4
$t3 , $t0 , $t2
$t3 , $zero , loop2
#
#
#
#
#
#
#
#
Angel E. Gonzalez-Lizardo,
Ph.D. 103
Examples
Comparing
Array Version
Pointer Version
move
$t0 , $zero
add
$t1 , $t0 , $t0
add
$t1 , $t1 , $t1
add
$t2 , $a0 , $t1
sw
$zero , 0( $t2 )
addi
$t0 , $t0 , 1
slt
$t3 , $t0 , $a1
bne
$t3 , $zero , loop1
move
$t0 , $a0
add
$t1 , $t1 , $a1
add
$t1 , $t1 , $t1
add
$t2 , $a0 , $t1
loop2 : sw
$zero , 0( $t0 )
addi
$t0 , $t0 , 4
slt
$t3 , $t0 , $t2
bne
$t3 , $zero , loop2
loop1 :
Angel E. Gonzalez-Lizardo,
Ph.D. 104
Real Stuff
IA-32 Instructions
Designers sometimes provide more powerful instructions than
those found in MIPS.
Their goal is reduce the number of instructions in a program.
The danger is increasing the complexity of the hardware, increasing
the time to execute.
MIPS was the vision of a single small group in 1985.
Not the case of the Intel IA-32, developed by several independent
groups who evolved the architecture over 20 years.
Angel E. Gonzalez-Lizardo,
Ph.D. 105
Real Stuff
IA-32 Milestones
1978: The Intel 8086 was announced as a register dedicated architecture.
1980: The Intel 8087 FP coprocessor is announced. Used the stack instead of
registers. Extended the 8086 architecture in 60 instructions.
1982: The 80286 extended the 8086 architecture by increasing the address space
to 24 bits, creating an elaborate memory-mapping and protection model, and
adding a few instructions to handle the protection.
1985: the 80386 extended the 80286 to 32 bits. Also added new instructions
turning the 386 into a nearly general purpose register machine. Paging support
was also added.
1989-95: The 80486, Pentium, and Pentium Pro aimed for higher performance
adding only 4 new user-visible instructions.
Angel E. Gonzalez-Lizardo,
Ph.D. 106
Real Stuff
IA-32 Milestones
1997: MMX (Multi Media Extension) expanded Pentium and Pentium Pro
architectures. 57 new instructions using the FP stack to accelerate multimedia and
communications applications.
1999: Intel added another 70 instructions labelled SSE (Streaming SIMD
Extensions) as part of Pentium III.
2001: Intel adds another 144 instructions for double precision arithmetic. FP
registers can be used for FP operations instead of the stack.
AMD enhances the IA-32 architecture increasing the address space from 32 to 64
bits. It provides a legacy mode, identical to IA-32 and a compatibility mode
AMD64 (user programs are IA-32, operating system is IA-64).
Intel capitulates and embraces AMD64 enhancing it with a 128-bit compare and
swap instruction. Adds SSE3 supporting complex arithmetics. AMD will offer
SSE3 in subsequent chips.
Angel E. Gonzalez-Lizardo,
Ph.D. 107
Real Stuff
The 80386 Register Set
Angel E. Gonzalez-Lizardo,
Ph.D. 108
Real Stuff
Register
Register
Register
Immediate
Register
Memory
Memory
Register
Memory
Immediate
The IA-32 logical and arithmetic instructions used one operand as source and destination.
Angel E. Gonzalez-Lizardo,
Ph.D. 109
Real Stuff
Addressing Modes
Mode
Description
Register Restrictions
MIPS equivalent
Register Indirect
Address in a register
Address is Base +
(2scale index) where
scale is 0, 1, 2, or 3
Address is Base +
(2scale index) where
scale is 0, 1, 2, or 3
lw $s0, 0($s1)
lw $s0, 100($s1)
mul $t0, $s2,4 add $t0,
$t0, $s1 lw $s0, 0($t0)
mul $t0, $s2,4 add $t0,
$t0, $s1 lw $s0, 100($t0)
Angel E. Gonzalez-Lizardo,
Ph.D. 110
Real Stuff
Classes Of Instructions
Four major classes of instructions
1
Data movement instructions: move, push, pop, etc.
2
Arithmetic and logic instructions: test, integer, decimal
arithmetic operations.
3
Control flow: conditional branches, unconditional jumps,
calls, and returns.
4
String instructions: string move and string compare.
Angel E. Gonzalez-Lizardo,
Ph.D. 111
Real Stuff
MIPS
Angel E. Gonzalez-Lizardo,
Ph.D. 112
Real Stuff
Angel E. Gonzalez-Lizardo,
Ph.D. 113
Angel E. Gonzalez-Lizardo,
Ph.D. 114
Concluding Remarks
The 4 Design Principles
1
Angel E. Gonzalez-Lizardo,
Ph.D. 115
References
Angel E. Gonzalez-Lizardo,
Ph.D. 116