Mod 2
Mod 2
Machine Instructions
and Programs
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.
Assembly language for representing machine instructions, data, and
programs.
Program-controlled Input/Output operations.
Number, Arithmetic Operations,
and Characters
Computers operate on information represented by two
valued electrical signals labeled as 0 and 1
The amount information represented by such signal is
defined as the bit of information
Bit stands for binary digit.
A number in a computer system is represented by a string
of bits, called as binary number
A text character is also represented by a string of bits,
called as Character code
Number representation
Signed Integer
3 major representations:
Sign and magnitude
One’s complement
Two’s complement
Assumptions:
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
-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
Overflow Conditions
1000
5 0101 -7 1001
3 0011 -2 1100
Overflow Overflow
0000 1111
5 0101 -3 1101
2 0010 -5 1011
No overflow No overflow
Overflow when carry-in to the high-order bit does not equal carry out
Sign Extension
Task:
Given w-bit signed integer x
Convert it to w+k-bit integer with same value
Rule:
Make k copies of sign bit:
X= x
w–1 ,…, xw–1 , xw–1 , xw–2 ,…, x0
w
X • • •
k copies of MSB
• • •
X • • • • • •
k w
Sign Extension Example
b 31 b 30 b1 b0
•
•
•
Sign bit: b 31= 0 for positive numbers
b 31= 1 for negative numbers
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
Two-Address Instructions
• ADD A,B
• MOVE B,C
One-Address Instructions
• LOAD A
• ADD B
• STORE C
Using Registers
Registers are faster
Shorter instructions
The number of registers is smaller (e.g. 32 registers need 5 bits)
Minimize the frequency with which data is moved back and
forth between the memory and processor registers.
Let Ri represents a general purpose register,
LOAD A,Ri
STORE Ri,A
ADD A,Ri
Only one memory address is specified in instruction, still may
1. LOAD A ; AC ← [A]
2. ADD B ; AC ← AC +
[B]
3. STORE T ; [T] ← AC
4. LOAD C ; AC ← [C]
5. ADD D ; AC ← AC +
[D]
6. MUL T ; AC ← AC
[T]
7. STORE X ; [X] ← AC
Instruction Execution and Straight-Line
Sequencing
Address Contents
i
Assumptions:
Begin execution here Move A,R0
i+4
3-instruction
program
- One memory operand
Add B,R0
segment per instruction
i+8 Move R0,C
- 32-bit word length
- Memory is byte
addressable
A - Full memory address
can be directly specified
in a single-word instruction
B Data for
the program
Two-phase procedure
-Instruction fetch
-Instruction execute
C
Page 43
•
•
•
i + 4n - 4 Add NUMn,R0
i + 4n Move R0,SUM
•
•
•
SUM
NUM1
NUM2
•
•
•
NUMn
Branching LOOP
Determine address of
"Next" number and add
Program "Next" number to R0
loop
Decrement R1
Branch>0 LOOP
Branch target
Move R0,SUM
Conditional branch
•
•
•
SUM
N n
NUM1
•
•
•
NUMn
Condition Codes
Condition code flags
Condition code register / status register
N (negative)
Z (zero)
V (overflow)
C (carry)
Different instructions affect different flags
Conditional Branch Instructions
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 11011100
C=1 Z=0
S=1
V=0
Status Bits
Cn-1
A B
Cn ALU
F
V Z S C
Fn-1
Zero Check