Transcript COMS 361
COMS 361
Computer Organization
Title: Instructions
Date: 9/28/2004
Lecture Number: 10
1
Announcements
• Homework 4
– Due 10/05/04
2
Review
• Instructions
– unconditional branch
– J-Type instruction format
• MIPS history
• SPIM
– MIPS simulator
3
Outline
• SPIM
– MIPS simulator
4
SPIM Program
• Show me the code!
• MIPS/SPIM program files usually end in .s
• File is loaded into SPIM
– Assembled into MIPS machine code
– Executed and debugged in SPIM
5
SPIM Program
## exit.s - program simply makes a system call to exit
##
## text segment
##
.text
main:
addi $v0, $zero, 10 # exit call code
syscall
# terminate program execution
##
## data segment
##
##
## end exit.s
6
SPIM Program
## exit.s - program simply makes a system call to exit
Comments in MIPS assembly
code are denoted by the
sharp (#) sign
Similar to the C++ (//)
comment. Starts at instance
and goes to the end of the
line
Assembly code in NOT selfdocumenting. Use comments
liberally, like associated with
each instruction
##
## text segment
##
.text
main:
addi $v0, $zero, 10 # exit call code
syscall
# terminate program execution
##
## data segment
##
##
## end exit.s
7
SPIM Program
## exit.s - program simply makes a system call to exit
Comments indicating the start
of the code
After text segment is
converted into its binary
representation, it is stored in
the text memory segment
. indicates an assembler
directive
.text directs the assembler to
treat what follows as
instructions
##
## text segment
##
.text
main:
addi $v0, $zero, 10 # exit call code
syscall
# terminate program execution
##
## data segment
##
##
## end exit.s
8
Text Segment
• The source code format is relatively standard
[label:] operation [operand], [operand], [operand] [# comment]
– Proper indentation is fundamentally important in
assembly language programming
– [] indicate optional fields
• Not all fields appear on a line of code
9
SPIM Program
## exit.s - program simply makes a system call to exit
main: is a label
Just as C++ programs need a
main function, MIPS
programs need a main label
main: labels the first
instruction of the program
##
## text segment
##
.text
main:
addi $v0, $zero, 10 # exit call code
syscall
# terminate program execution
##
## data segment
##
##
## end exit.s
10
Labels
• :
– Tells the assembler that the proceeding
alphanumerics including (_) and (.) constitutes the
label
– Opcodes are reserved words and are not
permitted to be used as labels
– Use appropriate names for labels
• Labels associate a symbol with
– The address of an instruction
– The address of a variable
11
SPIM Program
## exit.s - program simply makes a system call to exit
Program is to simple call the
OS with the exit call code
In the real world the OS
cleans up after the program
The call code for exit is the
decimal number 10, which
needs to be put into the $v0
register (convention)
addi with the proper operands
can achieve this goal
##
## text segment
##
.text
main:
addi $v0, $zero, 10 # exit call code
syscall
# terminate program execution
##
## data segment
##
##
## end exit.s
12
SPIM Program
## exit.s - program simply makes a system call to exit
Call the OS with the proper
call code in the $v0 register
##
## text segment
##
.text
main:
addi $v0, $zero, 10 # exit call code
syscall
# terminate program execution
##
## data segment
##
##
## end exit.s
13
SPIM Program
• Show me the program execute!
– And some things about SPIM City!!
• exit.s
• hello.s
14
Datapath Diagram
• Main functional units
– Control unit
Program Counter (PC)
Cache Memory
Instruction Register
Out
Address
ALU
Contro l
Log ic
Rd
Rs
Rt
4
Data In
Register File
15
Datapath Diagram
• Main functional units
– Register file
Program Counter (PC)
Cache Memory
Instruction Register
Out
Address
ALU
Contro l
Log ic
Rd
Rs
Rt
4
Data In
Register File
16
Datapath Diagram
• Main functional units
– Arithmetic and logic unit (ALU)
Program Counter (PC)
Cache Memory
Instruction Register
Out
Address
ALU
Contro l
Log ic
Rd
Rs
Rt
4
Data In
Register File
17
Datapath Diagram
• Main functional units
– Program counter (PC)
Program Counter (PC)
Cache Memory
Instruction Register
Out
Address
ALU
Contro l
Log ic
Rd
Rs
Rt
4
Data In
Register File
18
Datapath Diagram
• Main functional units
– Memory
Program Counter (PC)
Cache Memory
Instruction Register
Out
Address
ALU
Contro l
Log ic
Rd
Rs
Rt
4
Data In
Register File
19
Datapath Diagram
• Main functional units
– Instruction register (IR)
Program Counter (PC)
Cache Memory
Instruction Register
Out
Address
ALU
Contro l
Log ic
Rd
Rs
Rt
4
Data In
Register File
20
Datapath Diagram
• Other operational units
– bus
Program Counter (PC)
Cache Memory
Instruction Register
Out
Address
ALU
Contro l
Log ic
Rd
Rs
Rt
4
Data In
Register File
21
Datapath Diagram
• Other operational units
– Multiplexor (data selector)
Program Counter (PC)
Cache Memory
Instruction Register
Out
Address
ALU
Contro l
Log ic
Rd
Rs
Rt
4
Data In
Register File
22
Fetch and Execute R-type
• Instruction Fetch Phase
– Fetch the word in memory at the address
specified by the Program Counter (PC)
– Load instruction into the IR
– Increment the PC (add 4)
• Operand Fetch Phase
– Decode the Rs and Rt fields within the instruction
Decode the Op Code
23
Fetch and Execute Cycle
• Execute Phase
– Perform ALU operation defined by the function
code on the source operands
• Write Back Phase
– Result if the ALU is written into the decoded Rd
register
24
MIPS Instructions
• MIPS instruction set architecture
– Assembly instructions that convert into machine
(binary) instructions
• Assembly instructions can be converted directly into
machine instructions
– One-to-one mapping
• Assembly instructions can be converted into more than
one machine instruction
– One-to-many mapping
– Native machine instructions
– Pseudo-, Macro-, Synthetic-machine instructions
• Consists of more than one native machine instruction
25
MIPS Instructions
• Assembler converts pseudo-instructions with
the corresponding set of actual MIPS
instructions
– Pseudo-instructions simplify the task of writing
assembly code
pseudo instruction
la $a0 label
load address of label into register $a0
actual MIPS instructions
lui $at, upper 16 bits of label
ori $Rd, $at, Lower 16 bits of label
26
Assembler Directives
• Used to create data structures that are
available at run-time
– To allocate a one-dimensional array in C++
int ARRAY[1024];
– Corresponds to the MIPS assemble directive
.data
Allocate space in the data
segment of the SPIM
ARRAY:
.space
4096
memory model
Allocates 4096 bytes in the
data segment
27
Assembler Directives
– Allocate and initialize a one-dimensional array
int ARRAY[] = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024 };
– Corresponds to the MIPS assemble directive
.data
ARRAY:
.word
1, 2, 4, 8, 16, 32, 64,
128, 256, 512, 1024
la
$a0 ARRAY
# $a0 = &ARRAY[0]
lw
$s0 8($a0)
# $s0 = MEM[$ao + 8]
$a0 contains a pointer to the array
28
Assembler Directives
– String literal definition
.data
helloStr: .ascii “Hello, World!\n”
– helloStr is the memory location of the array of
characters
– The string is not null terminated
• Can cause problems (hell01.s)
helloStr:
.data
.asciiz
“Hello, World!\n”
29