Machine Instructions and Number Representation
Machine Instructions and Number Representation
Machine Instructions
and Programs
1
Objectives
⚫ Machine instructions and program execution,
including branching and subroutine call and return
operations.
⚫ Number representation and addition/subtraction in
the 2’s-complement system.
⚫ Addressing methods for accessing register and
memory operands.
2
Number, Arithmetic
Operations, and
Characters
3
Signed Integer Representations
⚫ 3 major representations:
Sign and magnitude
One’s complement
Two’s complement
4
Binary, Signed-Integer
Representations
B Values represented
Page 28
Sign and
b3 b2b1b0 magnitude 1's complement 2's complement
0 1 1 1 + 7 +7 + 7
0 1 1 0 + 6 +6 + 6
0 1 0 1 + 5 +5 + 5
0 1 0 0 + 4 +4 + 4
0 0 1 1 + 3 +3 + 3
0 0 1 0 + 2 +2 + 2
0 0 0 1 + 1 + 1 + 1
0 0 0 0 + 0 +0 + 0
1 0 0 0 - 0 -7 - 8
1 0 0 1 - 1 -6 - 7
1 0 1 0 - 2 -5 - 6
1 0 1 1 - 3 -4 - 5
1 1 0 0 - 4 -3 - 4
1 1 0 1 - 5 -2 - 3
1 1 1 0 - 6 - 1 - 2
1 1 1 1 - 7 -0 - 1
⚫ - x = 1’s complement of x
⚫ 1’s complement is invert 0 to 1 and 1 to 0
⚫ Two representations for 0 (0000 is +0, 1111 is -0). causes some
problems. Some complexities in addition, subtraction
⚫ Subtraction (X-Y) implemented by addition & 1's complement (x – y = X +
1’s complement of Y = X + Y' )
7
Two’s Complement
Representation
-1 +0
-2 1111 0000 +1
1110 0001
-3 +2 +
like 1's comp 1101 0010
⚫ - x = 2’s complement of x
⚫ Range : -2n-1 to (2n-1 - 1)
⚫ 2’s complement is just 1’s complement + 1
⚫ Only one representation for 0 ( 0000 => 1111+1 => 10000 => 0000 in 4 bits,
ignore the carry out / MSB 1)
⚫ Addition, Subtraction Very Simple
8
2’s-Complement Add and
Subtract Operations
(a) 0010 ( + 2) (b) 0100 ( + 4)
+ 0011 ( + 3) + 1010 (- 6 )
Page 31 (- 2 )
0101 ( + 5) 1110
(c) 1011 (- 5) (d) 0111 ( + 7)
+ 1110 (- 2) + 1101 ( - 3)
1001 (- 7) 0100 ( + 4)
(e) 1101 (- 3) 1101
- 1001 (- 7) + 0111
0100 ( + 4)
(f) 0010 ( + 2) 0010
- 0100 ( + 4) + 1100
1110 ( - 2)
(g) 0110 ( + 6) 0110
- 0011 ( + 3) + 1101
0011 ( + 3)
(h) 1001 ( - 7) 1001
- 1011 (- 5) + 0101
1110 ( - 2)
(i) 1001 (- 7) 1001
- 0001 ( + 1) + 1111
1000 ( - 8)
(j) 0010 ( + 2) 0010
- 1101 ( - 3) + 0011
0101 ( + 5)
-1 +0 -1 +0
-2 1111 0000 +1 -2 1111 0000 +1
1110 0001 1110 0001
-3 +2 -3
1101 1101 +2
0010 0010
-4 -4
1100 0011 +3 1100 0011 +3
-5 1011 -5 1011
0100 +4 0100 +4
1010 1010
-6 0101 -6 0101
1001
+5 +5
0110 1001 0110
-7 1000 0111 +6 -7 1000 +6
0111
-8 +7 -8 +7
5 + 3 = -8 -7 - 2 = +7
0101 + 0011 = 1000 1001 + 1100 = 10111 => 0111
x
10
Overflow Condition – Carry in
to MSB ≠ Carry out from MSB
0 1 1 1←carry-in 1000
5 0101 -7 1001
Overflow Overflow
3 0011 -2 1100
-8 1000 7 10111
0000 1111
No 5 0101 1101
overflow -3
No overflow
2 0010 -5 1011
7 0111 -8 11000
12
Memory Location, Addresses,
and Operation n bits
first word
⚫ Memory consists of many second word
millions of storage cells,
each of which stores 1 bit. •
•
⚫ Data is usually accessed •
in n-bit groups, called a
“word”. i th word
b 31 b 30 b1 b0
•
•
•
Sign bit: b 31= 0 for positive numbers
b 31= 1 for negative numbers
Word
address Byte address Byte address
0 0 1 2 3 0 3 2 1 0
4 4 5 6 7 4 7 6 5 4
• •
• •
• •
k k k k k k k k k k
2 -4 2 -4 2 -3 2 - 2 2 - 1 2 - 4 2 - 1 2 - 2 2 -3 2 -4
20
“Must-Perform” Operations for
computer instructions:
⚫ Data transfers between the memory and the
processor registers
⚫ Arithmetic and logic operations on data
⚫ Program sequencing and control
⚫ I/O transfers
21
Computer instructions must be capable of performing 4 types of
operations:
iv. Input/output transfers to transfer data to and from the real world.
22
Examples of different types of instructions in assembly language notation:
Move A, B (B = A).
Move A, R1 (R1 = A).
Add A, B, C (C = A + B)
❑ Sequencing:
OP src_op dest_op
25
CPU Organization: Internal
Storage Architecture:
⚫ Controls how its instructions use the operand(s).
⚫ The type of internal storage in a processor is the
most basic differentiation.
i. Single Accumulator (AC) CPU Organization
⚫ One operand is implicitly the Accumulator Register.
⚫ The Accumulator is both an implicit input operand and
a source or a destination, depending on the instruction.
⚫ Accumulator (AC) has to be saved to memory quite often.
26
ii. Register-Memory CPU Organization:
28
Instruction Formats
⚫ Three-Address Instructions
⚫ ADD R2, R3, R1 R1 ← [R2] + [R3]
⚫ Two-Address Instructions
⚫ ADD R2, R1 R1 ← [R1] + [R2]
⚫ One-Address Instructions (usually use for Single
Accumulator CPU organization): AC register is always an
implicit operand
⚫ ADD X (AC ← [AC]+M[X] (AC is the Accumulator Register))
⚫ Zero-Address Instructions (Usually for Stack CPU
organization) No explicit operands, both operands are implicit
⚫ ADD (TOS ← TOS + (TOS – 1); TOS means Top of Stack)
⚫ RISC Instructions: An instruction can have 3~4 registers!
Memory Access is restricted Only to LOAD and STORE instructions
Instruction
Opcode Operand(s) or Address(es) 29
Instruction Formats
Example: Evaluate X ← (A+B) (C+D)
⚫ Three-Address Format
1. ADD A, B, R1 ; R1 ← M[A] + M[B]
2. ADD C, D, R2 ; R2 ← M[C] + M[D]
3. MUL R1, R2, X ; M[X] ← [R1] [R2]
30
Instruction Formats
Example: Evaluate X←(A+B) (C+D)
⚫ Two-Address instruction format
1. MOV A, R1 ; R1 ← M[A]
2. ADD B, R1 ; R1 ← [R1] + M[B]
3. MOV C, R2 ; R2 ← M[C]
4. ADD D, R2 ; R2 ← [R2] + M[D]
5. MUL R2, R1 ; R1 ← [R1] [R2]
6. MOV R1, X ; M[X] ← [R1]
32
Instruction Formats
Example: Evaluate X = (A+B) (C+D)
⚫ Zero-Address Instruction Format
(must use stack processor organization.
TOS means Top of Stack)
1. PUSH A ; TOS ← A
2. PUSH B ; TOS ← B
3. ADD ; TOS ← (A + B)
4. PUSH C ; TOS ← C
5. PUSH D ; TOS ← D
6. ADD ; TOS ← (C + D)
7. MUL ; TOS ←(C+D)(A+B)
8. POP X ; M[X] ← TOS
33
Instruction Formats
Example: Evaluate X = (A+B) (C+D)
⚫ RISC Instruction Format (i).RISC can use 3
registers in a single instruction; (ii).Only the LOAD
and STORE instructions can access memory
1. LOAD A, R1 ; R1 ← M[A]
2. LOAD B, R2 ; R2 ← M[B]
3. LOAD C, R3 ; R3 ← M[C]
4. LOAD D, R4 ; R4 ← M[D]
5. ADD R1, R2, R1 ; R1 ← [R1] + [R2]
6. ADD R3, R4, R3 ; R3 ← [R3] + [R4]
7. MUL R1, R3, R1 ; R1 ← [R1] [R3]
8. STORE R1, X ; M[X] ← [R1]
34
Using Registers
⚫ Registers are faster
⚫ Shorter instructions
⚫ Potential speedup
⚫ Minimize the frequency with which data is
moved back and forth between the memory
and processor registers.
35
Instruction Execution
⚫ Basic instruction cycle
Fetch/Execute cycle
⚫ Execution of an instruction takes place in two phases:
⚫ Instruction fetch.
⚫ Instruction execute.
⚫ Instruction fetch:
⚫ Fetch the instruction from the memory location whose address is in the Program
Counter (PC).
⚫ Place the instruction in the Instruction Register (IR).
⚫ Instruction execute:
⚫ Instruction in the IR is examined (decoded) to determine which operation is to be
performed.
⚫ Fetch the operands from the memory or registers.
⚫ Execute the operation.
⚫ Store the results in the destination location.
⚫ Basic fetch/execute cycle repeats indefinitely.
37
Straight-line sequencing
⚫ The processor control circuits use the
information in the program counter (PC) to
fetch and execute instructions, one at a time,
in the order of increasing addresses.
⚫ This is called straight- line sequencing.
38
Instruction Execution and
Straight-Line Sequencing
Assumptions:
Address Contents
- One memory operand
Begin execution here i Mov A,R0
per instruction
3-instruction - 32-bit word length
i+4 Add B,R0 program
segment - Memory is byte
i+8 Mov R0,C
addressable
- Each instruction fits in
ONE word (Full memory
A address can be directly
specified in a single-word
instruction
B Data for (though this is not realistic!!!)
the program
Two-phase procedure
-Instruction fetch
-Instruction execute
C
39
Branching
⚫ Branch instructions load a new value into the
program counter.
⚫ As a result, the processor fetches and
executes the instruction at this new address,
called the branch target, instead of the
instruction at the location that follows the
branch instruction in sequential address
order.
40
Conditional Branching
⚫ A conditional branch instruction causes a
branch only if a specified condition is
satisfied.
⚫ If the condition is not satisfied, the PC is
incremented in the normal way, and the next
instruction in sequential address order is
fetched and executed.
41
i Move NUM1,R0
Straight-Line i+4 Add NUM2,R0
•
•
•
•
•
•
NUM n
Branching Clear R0
•
•
•
NUM n
43
Book Examples (Fig. 2.12): Move N,R1
Clear R0
Indirect Addressing to
LOOP
Compute the Array Determine address of
"Next" number and add
Program "Next" number to R0
Sum loop
Decrement R1
Branch>0 LOOP
Move R0,SUM
•
•
•
SUM
N n
NUM1
NUM2
•
•
•
NUM n
44
Book Examples (Fig. 2.12): Indirect Addressing
to Compute the Array Sum (Fig. 2.10)
45
Condition Codes / Status flags
⚫ Condition code flags (Status flags) of the CPU
⚫ Condition code flags are the bits of a special register (i.e., status
register) within the processor. They are affected by the most recent ALU
operations
⚫ N (negative) or S (sign) flag (i.e., bit of the status register) is Set to 1 if the
result of most recent arithmetic operation is negative
⚫ Is used by some instructions, such as: Branch<0 LOOP
⚫ Z (zero) flag: is set if the result of the most recent arithmetic operation is
ZERO
⚫ Used by some instructions, like: Branch==0 LABEL
46
Example: How Condition Codes
or Status Flags Set/Reset
⚫ Example: A: 11110000
⚫ A: 1 1 1 1 0 0 0 0 +(−B): 1 1 1 0 1 1 0 0
⚫ B: 0 0 0 1 0 1 0 0 1 11011100
⚫ Absolute mode
⚫ Operand is in a memory location.
Main
memory
B Operand A B
R1 B Register B Operand
R1 1000
63
Addressing Modes (contd..)
•Relative mode:
•The Instruction Branch > 0 Loop
•Suppose that the loop starts at address 1000, and
the branch instruction at address 1012.
•The PC value now is 1016.
•To branch to location Loop (1000), the offset value is
1000 – 1016 = -16
•When the assembler processes such instruction, it
computes the required offset value, and generates
the corresponding machine instruction using the
addressing mode:
-16(PC)
64
Addressing Modes (contd..)
⚫ Autoincrement mode:
⚫ Effective address of the operand is the contents of a register specified
in the instruction.
⚫ After accessing the operand, the contents of this register are
automatically incremented to point to the next consecutive memory
location.
⚫ (R1)+
⚫ Autodecrement mode
⚫ Effective address of the operand is the contents of a register specified
in the instruction.
⚫ Before accessing the operand, the contents of this register are
automatically decremented to point to the previous consecutive
memory location.
⚫ -(R1)
⚫ Autoincrement and Autodecrement modes are useful for implementing
“Last-In-First-Out” data structures.
65
Addressing modes (contd..)
68
Book Examples (Fig. 2.33): Computing Dot
Product of two vectors ... (2)
69