EE 344
Digital Computer Systems
Lecture 5:
Assembly Language, an Overview
Where are we?
Very close to the hardware
Link between HLL, ISA, and
microarchitecture
One HLL instruction may be implemented with
several assembly language instructions
Symbolic presentation of the instruction set
architecture (ISA) level
Programs written with different HLL can be
transformed to the same assembly language
Not Easy!
Limited storage resources (memory and
registers)
No notion of types, it is the programmer
responsibility to use the correct instructions
Only go to control instructions
Amount of work done by an instruction is
smaller than a HLL instructions
Much longer to debug and much harder to
maintain
Difficult to think at that low level
Not portable
Why bother, then?
Performance (speed and cost)
Much smaller than HLL
Much faster than HLL
Complete access to the hardware
features
Device drivers
Interrupts
Some real-time embedded systems
etc...
The view/model
(registers and memory)
The assembly programmer
sees memory and registers
Assembly-Language
Instruction types
Data Types
the key difference between mediocre
and excellent programmers is whether
or not they know assembly language.
The Memory
Large number of locations
Each location has a fixed size width
(usually the width is 1 byte)
Collection of all locations is called
memory address space
In assembly we use labels
Memory address space is used to store
instructions and data
Typical Memory Organization
Addresses
0x00000000
0x00000001
.
.
.
Memory Address Space
Hold the instructions
Usually read-only from
Programmer point of view
OS can still write to it
Text
Data
To store the data (local
and global) during run-time
Direction of growth
At run-time
Stack
To store data required during
Subroutine activation, including
Local variables
Register Model FAQ
Where are the registers?
Inside the processor chip
What is their function
Holding data needed frequently
How many registers do we have?
Usually few
Why?
Decoder used in selection is much smaller than the one used
for memory
Limited space on-chip
Few bits to specify a register
Technology
Commonly Used Registers
PC: program counter
ACC: Accumulator
SP: Stack Pointer
Link register
General purpose registers (GPRs)
Flag register
N---negative result
Z--- zero result
V--- overflow
C--- carry
other
What about Data Types?
There is no notion of variables
Type is specified based on the
instruction used
Instructions deal with few data types:
Signed/unsigned integers
Floating point
Decimal (BCD)
characters
Instructions
Each assembly-level architecture has its
own group of instructions -> instruction
set
Instructions can be grouped into four
categories:
Data Transfer
Data Manipulation
Program Sequencing and Control
Trap/system call
Instructions: Data Transfer
Instructions: Data Manipulation
Instructions: Program Sequencing
and Control
Welcome to MIPS Assembly
Language
Why MIPS?
Patterson and Hennessy use MIPS assembly language. Given the
overwhelming acceptance of their textbook as the standard, this
is already a strong argument.
RISC architectures actually are dominant.
It's best to teach a subject by going from the simple to the
complex. The MIPS R2000 instruction set architecture is
probably the simplest in existence for a real processor with a
significant market. In contrast, the Intel architecture, is
extremely complex.
Electrical and software engineers do, in fact, need to write
assembly-language programs for embedded processors. (e.g. HP
4000 laser printer, Nintendo, Sony playstation, etc.)
Other system integrators, such as router and telephone-switch
manufacturers, make use of embedded processors.
In all cases, these RISC architectures are extremely similar to
the MIPS R2000 architecture that Hennessy and Patterson use
throughout their text. A student who has learned the MIPS
R2000 instruction set is in a good position to write programs for
any embedded RISC processor.
You really should do
http://www.cs.unibo.it/~solmi/teaching/arch_20022003/AssemblyLanguageProgDoc.pdf
(MIPS assembly Language Programmers Guide -> download it!)
http://logos.cs.uic.edu/366/notes/MIPS%20Quick%20Tutorial.htm
(A quick start on MIPS assembly -> look at it!)
Get Some books:
MIPS Assembly Language Programming
by Robert Britton. Published by Prentice Hall.
See MIPS Run
by Dominic Sweetman. Publisher: Morgan Kaufmann
Computer Structure
Keyboard,
Mouse
Computer
Processor
Control
(brain)
Datapath
Memory
Devices
(where
programs,
data
live when
running)
Input
Output
Disk
(where
programs,
data
live when
not running)
Display,
Printer
MIPS arithmetic
All instructions have 3 operands and order is fixed (destination
first)
Example:
C code:
a=b+c
MIPS code:
add a, b, c
The natural number of operands for an operation like addition is
threerequiring every instruction to have exactly three operands,
no more and no less, conforms to the philosophy of keeping the
hardware simple
MIPS arithmetic
Design Principle: simplicity favors regularity.
Of course this complicates some things...
(what are these things?)
C code: a = b + c + d;
MIPS code:
add a, b, c
add a, a, d
Operands must be registers,
Only 32 registers provided
Each register contains 32 bits
Design Principle: smaller is faster.
What about programs with lots of variables?
10
11
12
13
14
Meaning:
$t0 = Memory[$s3+24]
Let $s3 = 1001 0100
15
16
Memory Organization
Viewed as a large, single-dimension array, with an address.
A memory address is an index into the array
"Byte addressing" means that the index points to a byte of
memory.
0
8 bits of data
8 bits of data
8 bits of data
8 bits of data
8 bits of data
8 bits of data
8 bits of data
...
17
Memory Organization
Bytes
are nice, but most data items use larger
"words"
For MIPS, a word is 32 bits or 4 bytes.
Registers hold 32 bits of data
232 bytes with byte addresses from 0 to 232-1
230 words with byte addresses 0, 4, 8, ... 232-4
Instructions
Load and store instructions
Example:
C code:
A[12] = h + A[8];
MIPS code:
lw $t0, 32($s3)
add $t0, $s2, $t0
sw $t0, 48($s3)
Can refer to registers by name (e.g., $s2, $t2) instead of
number
Store word has destination last
Remember arithmetic operands are registers, not memory!
Cant write:
add 48($s3), $s2, 32($s3)
Byte Addressing: 4x8=32
$s3 is called Base Register
Data Transfer Instruction: A[8] is offset
18
Summary
MIPS
loading words but addressing bytes
arithmetic on registers only
Instruction
Meaning
add $s1, $s2, $s3
sub $s1, $s2, $s3
lw $s1, 100($s2)
sw $s1, 100($s2)
$s1 = $s2 + $s3
$s1 = $s2 $s3
$s1 = Memory[$s2+100]
Memory[$s2+100] = $s1
19
ori = OR immediate
20
21
22
Examples of sll &srl
Sll $t2, $s0, 2
Multiply by 4
.0010
00100 Shift left once (* by 2)
001000 Shift left twice(* by 4)
srl $t2, $s0, 2
Divide by 4
001
0001 Shift right once (div by 2)
00001Shift right twice(div by 4)
23
24
MIPS Instruction Classes
25
26
27
28
(Section 2.8)
29
See you next time.
30