Instructions: Language of The Computer
Instructions: Language of The Computer
Instructions: Language of The Computer
CHAPTER 2
INSTRUCTIONS:
LANGUAGE OF THE COMPUTER
1
CE Instructions: Language of the Computer
1. Introduction
2. Operations of the Computer Hardware
3. Operands of the Computer Hardware
4. Signed and Unsigned number
5. Representing Instructions in the Computer
6. Logical Operations
7. Instructions for Making Decisions
8. Supporting Procedures in Computer Hardware
9. Communicating with People
10. MIPS Addressing for 32-Bit Immediates and Addresses
11. Translating and Starting a Program
2
CE Instructions: Language of the Computer
3
CE Instructions: Language of the Computer
1. Introduction
2. Operations of the Computer Hardware
3. Operands of the Computer Hardware
4. Signed and Unsigned number
5. Representing Instructions in the Computer
6. Logical Operations
7. Instructions for Making Decisions
8. Supporting Procedures in Computer Hardware
9. Communicating with People
10. MIPS Addressing for 32-Bit Immediates and Addresses
11. Translating and Starting a Program
4
CE Introduction
To command a computer‟s hardware, you must speak its language. The words
of a computer's language are called instructions, and its vocabulary is called
an instruction set
instruction set: The vocabulary of commands understood by a given architecture
With instruction set, once you learn one, it is easy to pick up others.
This similarity occurs because all computers are constructed from hardware
technologies based on similar underlying principles and because there are a few
basic operations that all computers must provide
• ARM is quite similar to MIPS, and more than three billion ARM processors
were shipped in embedded devices in 2008
• The Intel x86, is inside almost all of the 330 million PCs made in 2008 5
CE Instructions: Language of the Computer
1. Introduction
2. Operations of the Computer Hardware
3. Operands of the Computer Hardware
4. Signed and Unsigned number
5. Representing Instructions in the Computer
6. Logical Operations
7. Instructions for Making Decisions
8. Supporting Procedures in Computer Hardware
9. Communicating with People
10. MIPS Addressing for 32-Bit Immediates and Addresses
11. Translating and Starting a Program
6
CE Operations of the Computer Hardware
Operations:
Example:
operations operands
7
CE Operations of the Computer
Hardware
Example 1. Example 2.
C/Java C/Java
a = b + c; f = (g + h) – (i +
d = a – e; j);
MIPS add t0, g, h MIPS
add a, b, c add t1, i, j
sub d, a, e sub f, t0,
t1
9
CE Instructions: Language of the Computer
1. Introduction
2. Operations of the Computer Hardware
3. Operands of the Computer Hardware
4. Signed and Unsigned number
5. Representing Instructions in the Computer
6. Logical Operations
7. Instructions for Making Decisions
8. Supporting Procedures in Computer Hardware
9. Communicating with People
10. MIPS Addressing for 32-Bit Immediates and Addresses
11. Translating and Starting a Program
10
CE Operands of the Computer Hardware
2. Memory Operands
11
CE Operands of the Computer Hardware
Register Operands:
Unlike programs in high-level languages, the operands of arithmetic instructions are
restricted; they must be from a limited number of special locations built directly in
hardware called registers.
The size of a register in the MIPS architecture is 32 bits; groups of 32 bits occur so
frequently that they are given the name word in the MIPS architecture.
(note: a word in other instruction sets is able to not be 32 bits)
12
CE Operands of the Computer Hardware
With MIPS instructions, arithmetic operations occur only on registers; thus, MIPS
must include instructions that transfer data between memory and registers. Such
instructions are called data transfer instructions.
Data transfer instruction: A command that moves data between memory and
registers
To access a word in memory, the instruction must supply the memory address
Address: A value used to delineate the location of a specific data element within a
memory array.
13
CE Operands of the Computer Hardware
Fig.2 Memory addresses and contents Fig.3 Actual MIPS memory addresses and contents
of memory at those locations. of memory for those words.
This is a simplification of the MIPS The changed addresses are highlighted to contrast
addressing; Fig.3 shows the actual with Fig.2. Since MIPS addresses each byte, word
MIPS addressing for sequential word addresses are multiples of four: there are four bytes
addresses in memory. in a word.
14
CE Operands of the Computer Hardware
offset Base
address in a base register
• A constant (20) and register ($s2) used to access memory. The sum of
the constant and the contents of the second register forms the
memory address.
15
CE Operands of the Computer Hardware
The constant in a data transfer instruction (8) is called the offset, and the register
added to form the address ($s3) is called the base register
16
CE Operands of the Computer Hardware
- In MIPS, words must start at addresses that are multiples of 4. This requirement is
called an alignment restriction, and many architectures have it. (why alignment leads
to faster data transfers – read more in Chapter 5 suggests )
- Computers divide into those that use the address of the leftmost or “big end” byte
as the word address versus those that use the rightmost or “little end” byte. MIPS is in
the Big Endian camp.
17
CE Operands of the Computer Hardware
offset
Base address in a base register
18
CE Operands of the Computer Hardware
Compile:
lw $t0,32($s3) # Temporary reg $t0 gets A[8]
add $t0,$s2,$t0 # Temporary reg $t0 gets h + A[8]
sw $t0,48($s3) # Stores h + A[8] back into A[12]
19
CE Operands of the Computer Hardware
Notes:
Although the MIPS registers considered here are 32 bits wide, there is a 64-bit version of
the MIPS instruction set with 32 64-bit registers. To keep them straight, they are officially
called MIPS-32 and MIPS-64.
(In this chapter, a subset of MIPS-32 is used; see more MIPS-64 in Appendix E)
Since MIPS supports negative constants, there is no need for subtract immediate in MIPS
20
CE Instructions: Language of the Computer
1. Introduction
2. Operations of the Computer Hardware
3. Operands of the Computer Hardware
4. Signed and Unsigned number
5. Representing Instructions in the Computer
6. Logical Operations
7. Instructions for Making Decisions
8. Supporting Procedures in Computer Hardware
9. Communicating with People
10. MIPS Addressing for 32-Bit Immediates and Addresses
11. Translating and Starting a Program
21
CE Signed and Unsigned number
Humans are taught to think in base 10, but numbers may be represented in any base.
For example, 123 base 10 = 1111011 base 2.
Numbers are kept in computer hardware as a series of high and low electronic
signals, and so they are considered base 2 numbers.
Example: the drawing below shows the numbering of bits within a MIPS word and the
placement of the number 1011:
The MIPS word is 32 bits long, so the numbers from 0 to 232-1 (4.294.967.295)
The least significant bit: the rightmost bit in a MIPS word (bit 0)
The most significant bit: the leftmost bit in a MIPS word (bit 31)
22
CE Signed and Unsigned number
23
CE Signed and Unsigned number
The positive half of the numbers, from 0 to 2,147,483,647ten (231 – 1), use the same
representation as before.
1000…0000two = -2,147,483,648ten
1000…0001two = -2,147,483,647ten
1111…1111two = -1ten
The 32nd bit is called the sign bit. We can represent positive and negative 32-bit
numbers in terms of the bit value times a power of 2
Two‟s complement does have one negative number -2,147,483,648ten, that has
no corresponding positive number.
Every computer today uses two‟s complement binary representations for signed
number
Note: The sign bit is multiplied by -231, and the rest of the bits are then multiplied
by positive versions of their respective base values. 24
CE Signed and Unsigned number
Example
Answer
25
CE Signed and Unsigned number
Sign Extension:
How to convert a binary number represented in n bits to a number represented with
more than n bits?
Example:
Convert 16-bit binary versions of 2ten and -2ten to 32-bit binary numbers.
2ten:
-2ten:
Taking the sign bit and replicating it to fill the new bits of larger quantity. The
old
bits are simply copied into the right portion of the new word.
26
CE Instructions: Language of the Computer
1. Introduction
2. Operations of the Computer Hardware
3. Operands of the Computer Hardware
4. Signed and Unsigned number
5. Representing Instructions in the Computer
6. Logical Operations
7. Instructions for Making Decisions
8. Supporting Procedures in Computer Hardware
9. Communicating with People
10. MIPS Addressing for 32-Bit Immediates and Addresses
11. Translating and Starting a Program
27
CE Representing Instructions in the Computer
28
CE Representing Instructions in the Computer
29
CE Representing Instructions in the Computer
In MIPS assembly language, registers $s0 to $s7 map onto registers 16 to 23, and
registers $t0 to $t7 map onto registers 8 to 15
Each of these segments of an instruction format is called a field.
The first and last fields (containing 0 and 32 in this case) in combination tell the
MIPS computer that this instruction performs addition.
The second field gives the number of the register that is the first source operand of
the addition operation (17 = $s1)
The third field gives the other source operand for the addition (18 = $s2).
The fourth field contains the number of the register that is to receive the sum (8 =
$t0).
The fifth field is unused in this instruction, so it is set to 0.
30
CE Representing Instructions in the Computer
31
CE Representing Instructions in the Computer
rd: The register destination operand. It gets the result of the operation.
shamt: Shift amount. (the next part explains shift instructions and this term; it will
not be used until then, and hence the field contains zero.)
funct: Function. This field selects the specific variant of the operation in the op
field and is sometimes called the function code. 32
CE Representing Instructions in the Computer
The 16-bit address means a load word instruction can load any word within a region
of ± 215 or 32,768 bytes (±213 or 8192 words) of the address in the base register rs.
Similarly, add immediate is limited to constants no larger than ± 215
33
CE Representing Instructions in the Computer
Fig.6 shows the numbers used in each field for some MIPS
instructions
“n.a.” (not applicable) means this field does not appear in this format.
Note that “add” and “sub” instructions have the same value in the “op” field;
the hardware uses the “funct” field to decide the variant of the operation:
“add”
(32) or “subtract” (34). 34
CE Representing Instructions in the Computer
Answer:
35
CE Representing Instructions in the Computer
In conclusion,
1. Instructions are represented as numbers.
Treating instructions in the same way as data greatly simplifies both the
memory hardware and the software of computer systems.
In order to perform a program, simply loading the program and data into
memory and then telling the computer to begin executing at a given
location in memory.
36
CE Instructions: Language of the Computer
1. Introduction
2. Operations of the Computer Hardware
3. Operands of the Computer Hardware
4. Signed and Unsigned number
5. Representing Instructions in the Computer
6. Logical Operations
7. Instructions for Making Decisions
8. Supporting Procedures in Computer Hardware
9. Communicating with People
10. MIPS Addressing for 32-Bit Immediates and Addresses
11. Translating and Starting a Program
37
CE Logical Operations
Fig.7 C and Java logical operators and their corresponding MIPS instructions.
Shifts: moving all the bits in a word to the left or right, filling the emptied
bits with 0s (Shifting left by i bits gives the same result as multiplying by 2i)
AND is a bit-by-bit operation that leaves a 1 in the result only if both bits of
the
operands are 1
OR is a bit-by-bit operation that places a 1 in the result if either operand bit is
a1
NOT takes one operand and places a 1 in the result if one operand bit is a 0,
and vice versa.
NOR: NOT OR
Constants are useful in AND and OR logical operations as well as in arithmetic 38
CE Instructions: Language of the Computer
1. Introduction
2. Operations of the Computer Hardware
3. Operands of the Computer Hardware
4. Signed and Unsigned number
5. Representing Instructions in the Computer
6. Logical Operations
7. Instructions for Making Decisions
8. Supporting Procedures in Computer Hardware
9. Communicating with People
10. MIPS Addressing for 32-Bit Immediates and Addresses
11. Translating and Starting a Program
39
CE Instructions for Making Decisions
Conditional branch: An instruction that requires the comparison of two values and
that allows for a subsequent transfer of control to a new address in the program based
on the outcome of the comparison.
40
CE Instructions for Making Decisions
41
CE Instructions for Making Decisions
if (i == j) f = g + h; else f = g – h;
Answer:
bne $s3,$s4,Else
# go to Else if i != j
add $s0, $s1,
# f = g + h (skipped if i !=
$s2 j exit
j) # go to Exit
else: sub $s0,
# f = g – h (skipped if i = j)
$s1, $s2
exit:
42
CE Instructions for Making Decisions
Assume that i and k correspond to registers $s3 and $s5 and the base of the
array save is in $s6. What is the MIPS assembly code corresponding to this C
segment?
Answer:
add $t1,$t1,$s6
Loop: sll $t1,$s3,2 # Temp reg#$t1
$t1==4address
*i of save[i]
lw $t0,0($t1) # Temp reg $t0 = save[i]
bne $t0,$s5, Exit # go to Exit if save[i] !=
add $s3,$s3,1 k #i=i+1
j Loop # go to Loop
Exit:
43
CE Instructions: Language of the Computer
1. Introduction
2. Operations of the Computer Hardware
3. Operands of the Computer Hardware
4. Signed and Unsigned number
5. Representing Instructions in the Computer
6. Logical Operations
7. Instructions for Making Decisions
8. Supporting Procedures in Computer Hardware
9. Communicating with People
10. MIPS Addressing for 32-Bit Immediates and Addresses
11. Translating and Starting a Program
44
CE Supporting Procedures in Computer Hardware
Procedures allow the programmer to concentrate on just one portion of the take at a
time.
45
CE Supporting Procedures in Computer Hardware
Registers are the fastest place to hold data in a computer, so we want to use them as
much as possible.
MIPS software follows the following convention for procedure calling in allocating
its 32 registers:
46
CE Supporting Procedures in Computer Hardware
jal ProcedureAddress
Nowadays, computers like MIPS use jump register instruction (jr), meaning
an
unconditional jump to the address specified in a register:
jr $ra
47
CE Supporting Procedures in Computer Hardware
Nested procedure:
49
CE Supporting Procedures in Computer Hardware
frame pointer: A value denoting the location of the saved registers and local
variables for a given procedure.
Fig.8 Illustration of stack allocation (a) before, (b) during, (c) after the
procedure call.
50
CE Supporting Procedures in Computer Hardware
Fig.10 MIPS register conventions. Register 1, called $at, is reserved for the assembler, and
registers 26-27, called $k0-$k1, are reserved for the operating system.
52
CE Instructions: Language of the Computer
1. Introduction
2. Operations of the Computer Hardware
3. Operands of the Computer Hardware
4. Signed and Unsigned number
5. Representing Instructions in the Computer
6. Logical Operations
7. Instructions for Making Decisions
8. Supporting Procedures in Computer Hardware
9. Communicating with People
10. MIPS Addressing for 32-Bit Immediates and Addresses
11. Translating and Starting a Program
53
CE Communicating with People
Most computers today offer 8-bit bytes to represent characters, with the American
Standard Code for Information Interchange (ASCII) being the representation that nearly
everyone follows.
1. Introduction
2. Operations of the Computer Hardware
3. Operands of the Computer Hardware
4. Signed and Unsigned number
5. Representing Instructions in the Computer
6. Logical Operations
7. Instructions for Making Decisions
8. Supporting Procedures in Computer Hardware
9. Communicating with People
10. MIPS Addressing for 32-Bit Immediates and Addresses
11. Translating and Starting a Program
55
MIPS Addressing for 32-bit Immediates and Addresses
CE
Although keeping all MIPS instructions 32 bits long simplifies the hardware,
there are times where it would be convenient to have a 32-bit constant or 32-bit
address.
32-Bit Immediate Operands
Example
Answer
56
MIPS Addressing for 32-bit Immediates and Addresses
CE
Note: The instruction lui transfers the 16-bit immediate constant field value into
the leftmost 16 bits of the register, filling the lower 16 bits with 0s.
57
MIPS Addressing for 32-bit Immediates and Addresses
CE
58
CE MIPS Addressing for 32-bit Immediates and Addresses
4. PC-relative addressing, where the branch address is the sum of the PC and a constant
in the instruction
5. Pseudo direct addressing, where the jump address is the 26 bits of the instruction
concatenated with the upper bits of the PC
59
CE MIPS Addressing for 32-bit Immediates and Addresses
Example
60
CE Instructions: Language of the Computer
1. Introduction
2. Operations of the Computer Hardware
3. Operands of the Computer Hardware
4. Signed and Unsigned number
5. Representing Instructions in the Computer
6. Logical Operations
7. Instructions for Making Decisions
8. Supporting Procedures in Computer Hardware
9. Communicating with People
10. MIPS Addressing for 32-Bit Immediates and Addresses
11. Translating and Starting a Program
61
CE Translating and Starting a Program
This section describes the four steps in transforming a C program in a
file on disk into a program running on a computer.