Chapter2 Part 2 Machine Instructions and Programs
Chapter2 Part 2 Machine Instructions and Programs
Machine
Instructions and
Programs
Objectives
l Machine instructions and program execution,
including branching and subroutine call and return
operations.
l Number representation and addition/subtraction in
the 2’s-complement system.
l Addressing methods for accessing register and
memory operands.
l Assembly language for representing machine
instructions, data, and programs.
l Program-controlled Input/Output operations.
Indexing and Arrays
l Index mode – the effective address of the operand is
generated by adding a constant value to the
contents of a register.
l Index register
l X(Ri): EA = X + [Ri]
l The constant X may be given either as an explicit
number or as a symbolic name representing a
numerical value.
l If X is shorter than a word, sign-extension is needed.
Indexing and Arrays
l In general, the Index mode facilitates access
to an operand whose location is defined
relative to a reference point within the data
structure in which the operand appears.
l Several variations:
(Ri, Rj): EA = [Ri] + [Rj]
X(Ri, Rj): EA = X + [Ri] + [Rj]
Relative Addressing
l Relative mode – the effective address is determined
by the Index mode using the program counter in
place of the general-purpose register.
l X(PC) – note that X is a signed number
l Branch>0 LOOP
l This location is computed by specifying it as an
offset from the current value of PC.
l Branch target may be either before or after the
branch instruction, the offset is given as a singed
num.
Additional Modes
l Autoincrement mode – the 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 item in a list.
l (Ri)+. The increment is 1 for byte-sized operands, 2 for 16-bit
operands, and 4 for 32-bit operands.
l Autodecrement mode: -(Ri) – decrement first
Move N,R1
Move #NUM1,R2 Initialization
Clear R0
LOOP Add (R2)+,R0
Decrement R1
Branch>0 LOOP
Move R0,SUM
Figure 2.16. The Autoincrement addressing mode used in the program of Fig
Assembly Language
Types of Instructions
l Data Transfer Instructions
Name Mnemonic Data value is
Load LD not modified
Store ST
Move MOV
Exchange XCH
Input IN
Output OUT
Push PUSH
Pop POP
Data Transfer Instructions
Mode Assembly Register Transfer
Direct address LD ADR AC ← M[ADR]
Indirect address LD @ADR AC ← M[M[ADR]]
Relative address LD $ADR AC ← M[PC+ADR]
Immediate operand LD #NBR AC ← NBR
Index addressing LD ADR(X) AC ← M[ADR+XR]
Register LD R1 AC ← R1
Register indirect LD (R1) AC ← M[R1]
Autoincrement LD (R1)+ AC ← M[R1], R1 ← R1+1
Data Manipulation Instructions
l Arithmetic Name Mnemonic
Increment INC
l Logical & Bit Manipulation Decrement DEC
Add ADD
l Shift Subtract SUB
Multiply MUL
Divide DIV
Name Mnemonic Add with carry ADDC
Clear CLR Subtract with borrow SUBB
Complement COM Negate NEG
Name Mnemonic
AND AND Logical shift right SHR
OR OR Logical shift left SHL
Exclusive-OR XOR Arithmetic shift right SHRA
Clear carry CLRC Arithmetic shift left SHLA
Set carry SETC Rotate right ROR
Complement carry COMC Rotate left ROL
Enable interrupt EI Rotate right through carry RORC
Disable interrupt DI Rotate left through carry ROLC
Program Control Instructions
Name Mnemonic
Branch BR
Jump JMP
Skip SKP
Subtract A – B but
Call CALL don’t store the result
Return RET
Compare
CMP
(Subtract) 10110001
Test (AND) TST
00001000
Mask
00000000
Conditional Branch
Instructions
before: 0 0 1 1 1 0 . . . 0 1 1
after: 1 1 1 0 . . . 0 1 1 0 0
0 R0 C
before: 0 1 1 1 0 . . . 0 1 1 0
after: 0 0 0 1 1 1 0 . . . 0 1
R0 C
before: 1 0 0 1 1 . . . 0 1 0 0
after: 1 1 1 0 0 1 1 . . . 0 1
(c) Ar
ithmetic shift
ight
r AShiftR #2,R0
C R0
. . .
Rotate
before: 0 0 1 1 1 0 0 1 1
after: 1 1 1 0 . . . 0 1 1 0 1
C R0
before: 0 0 1 1 1 0 . . . 0 1 1
after: 1 1 1 0 . . . 0 1 1 0 0
R0 C
before: 0 1 1 1 0 . . . 0 1 1 0
after: 1 1 0 1 1 1 0 . . . 0 1
(c) Rotateight
r without carr
y RotateR #2,R0
R0 C
before: 0 1 1 1 0 . . . 0 1 1 0
after: 1 0 0 1 1 1 0 . . . 0 1
(d) Rotate
ight
r with carr
y RotateRC #2,R0
• Example:
C code: A = B + C
compiler’s job to associate
variables with registers
• MIPS code: add $s0, $s1, $s2
MIPS Arithmetic
• Of course this complicates some things...
C code: A = B + C + D;
E = F - A;
MIPS code add $t0, $s1, $s2 Allowing variable number
of operands would
(arithmetic): add $s0, $t0, $s3 simplify the assembly
sub $s4, $s5, $s0 code but complicate the
hardware.
Load/Store Instructions
• Load and Store instructions
• Example:
C code: A[8] = h + A[8];
l
value offset address
• MIPS
l loading words but addressing bytes
l arithmetic on registers only
l Instruction Meaning
31 26 25 21 20 16 15 11 10 6 5 0
rt
add $4, $3, $2
rs
31 26 25 21 20 16 15 11 10 6 5 0
0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0
opcode rs rt rd shamt funct
0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0
Encoding = 0x00622020
29
Machine Language
31 26 25 21 20 16 15 0
rt
Immediate
lw $5, 3000($2)
rs
31 26 25 21 20 16 15 0
1 0 0 0 1 1 0 0 0 1 0 0 0 1 0 1 0 0 0 0 1 0 1 1 1 0 1 1 1 0 0 0
opcode rs rt Immediate Value
1 0 0 0 1 1 0 0 0 1 0 0 0 1 0 1 0 0 0 0 1 0 1 1 1 0 1 1 1 0 0 0
Encoding = 0x8C450BB8
31
MIPS Encoding: I-Type
31 26 25 21 20 16 15 0
rt
Immediate
sw $5, 3000($2)
rs
31 26 25 21 20 16 15 0
1 0 1 0 1 1 0 0 0 1 0 0 0 1 0 1 0 0 0 0 1 0 1 1 1 0 1 1 1 0 0 0
opcode rs rt Immediate Value
1 0 1 0 1 1 0 0 0 1 0 0 0 1 0 1 0 0 0 0 1 0 1 1 1 0 1 1 1 0 0 0
Encoding = 0xAC450BB8
32
The immediate value is signed
Immediate Operands
op rs rt 16 bit number
Shift Operations
op rs rt rd shamt funct
6 bits 5 bits 5 bits 5 bits 5 bits 6 bits
31 26 25 21 20 16 15 0
31 26 25 21 20 16 15 0
0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0
opcode rs rt Immediate Value
0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0
Encoding = 0x1009000A
36
Control: Unconditional Branch
(Jump)
l MIPS unconditional branch instructions:
j Label
l Example:
if (i!=j) beq $s4, $s5, else
h=i+j; add $s3, $s4, $s5
else j exit
h=i-j; else: sub $s3, $s4, $s5
exit: ...
l J-type (“J” for Jump) instruction format
l Example: j Label # addr. Label = 100 word-relative
addressing:
25 words = 100 bytes
000010 00000000000000000000011001
6 bits 26 bits
op 26 bit number
l Any Queries?