Professional Documents
Culture Documents
18CS34 CO Module 1 Part2
18CS34 CO Module 1 Part2
3 B,C
Dr. Jagadishwari
9 September 2020
Addressing Modes (Recap)
Na Asse bler sy tax Addressin function
⚫ The different ways in me m n g
which the location of Immediat # alue O eran = V alue
an operand is e V p d
specified in an Register Ri E = Ri
instruction are A
Absolute (Direct) LO E = LO
referred to as C A C
addressing modes. Indirect ( i) E = [R i ]
R
(LOC A
E = [LOC
) A ]
Index X( i ) E = [R i ] + X
R A
Bas with index ( i ,R j ) E = [R i ] + [R j ]
e R A
Bas with index X( i ,R j ) E = [R i ] + [R j ] + X
e an offset R A
d
Relative X(PC E = [PC] + X
) A
Autoincreme t ( i )+ E = [R i ] ;
n R AIncreme t Ri
n
Autodecremen − ( i) Decrem t Ri ;
t R en E = [R i ]
9 September 2020
A
Assembly Language
9 September 2020
Program to add n numbers using looping
Memory
4 Bytes
9 September 2020
Program to add n numbers using looping
Address
Memory
4 Bytes
Move N,R1 3000
Address
Move #Num1,R2 3004
N 4 1000
Clear R0 3008
Sum 1004
Loop: Add (R2), R0 3012
Num1 2 1008
Num2 1 1012 Add #4,R2 3016
3032
9 September 2020
Program to add n numbers using looping
After Executing the Program Address
Memory
4 Bytes
Move N,R1 3000
Address
Move #Num1,R2 3004
N 4 1000
Clear R0 3008
Sum 16 1004
Loop: Add (R2), R0 3012
Num1 2 1008
Num2 1 1012 Add #4,R2 3016
3032
9 September 2020
Question
Modify the following program to add n numbers using looping,
but access the elements of the array using Autoincrement addressing mode
Address
Memory
4 Bytes
Move N,R1 3000
Address
Move #Num1,R2 3004
N 4 1000
Clear R0 3008
Sum 1004
Loop: Add (R2), R0 3012
Num1 2 1008
Num2 1 1012 Add #4,R2 3016
3032
9 September 2020
Answer
Program to add n numbers using looping.
Accessing the elements of the array using Autoincrement addressing mode
Address
Memory
4 Bytes
Move N,R1 3000
Address
Move #Num1,R2 3004
N 4 1000
Clear R0 3008
Sum 1004
Loop: Add (R2)+, R0 3012
Num1 2 1008
Decrement R1 3016
Num2 1 1012
Num3 3 1016 Branch>0 Loop 3020
3028
3032
9 September 2020
Program to find sum of Test1, Test2 and Test3 marks of all students
Memory
4 Bytes Address
N 2 1000
Sum1 1036
Sum2 1040
Sum3 1044
9 September 2020
Program to find sum of Test1, Test2 and Test3 marks of all students
Address
Memory
4 Bytes Move #List,R0 3000
Address
N 2 1000
Clear R1 3004
N 2 1000
Clear R1 3004
9 September 2020
Stack
9 September 2020
Stack
⚫ A stack is a special type of data structure where elements are inserted from one end and elements are deleted
from the same end. This end is called the top of the stack
⚫ The various operations performed on stack:
◦ Insert: An element is inserted from top end. Insertion operation is called push operation.
◦ Delete: An element is deleted from top end. Deletion operation is called pop operation.
⚫ A processor-register is used to keep track of the address of the element of the stack that is at the top at any
given time. This register is called the Stack Pointer.
⚫ If we assume a byte-addressable memory with 32-bit word length
◦ The Push operation can be implemented as
Subtract #4,SP
Move NEWITEM,(SP)
◦ The Pop operation can be implemented as
Move (SP), ITEM
Subtract #4,SP
9 September 2020
Stack Organization
⚫ Memory Stack
PC 0
◦ PUSH
1
SP ← SP – 1 2
M[SP] ← DR
◦ POP AR 100
101
DR ← M[SP]
102
SP ← SP + 1
200
SP 201
202
9 September 2020
Stack: Push Operation Implementation
Subtract #4,SP
Move NEWITEM,(SP)
Before Executing the Instruction After Executing the Instruction
Memory Memory
4 Bytes 4 Bytes
Memory Memory
Address Contents Address Contents
NEWITEM 2000 6 NEWITEM 2000 6
ITEM 2004 ITEM 2004
3012 3012
3016 3016
3020 S 3020 6
P
SP 3024 SP 3020
9 September 2020
Stack: Push Operation Implementation
Subtract #4,SP Move NEWITEM,-(SP)
(OR)
Move NEWITEM,(SP)
Before Executing the Instruction After Executing the Instruction
Memory Memory
4 Bytes 4 Bytes
Memory Memory
Address Contents Address Contents
NEWITEM 2000 6 NEWITEM 2000 6
ITEM 2004 ITEM 2004
3012 3012
3016 3016
3020 S 3020 6
P
SP 3024 SP 3020
9 September 2020
Stack: Pop Operation Implementation
Move (SP), ITEM
Add #4,SP
Before Executing the Instruction After Executing the Instruction
Memory Memory
4 Bytes 4 Bytes
Memory Memory
Address Contents Address Contents
NEWITEM 2000 6 NEWITEM 2000 6
ITEM 2004 ITEM 2004 6
3012 3012
3016 3016
S 3020 6 3020 6
P S
P
SP 3020 SP 3024
9 September 2020
Stack: Pop Operation Implementation
Move (SP), ITEM (OR) Move (SP)+, ITEM
Add #4,SP
Before Executing the Instruction After Executing the Instruction
Memory Memory
4 Bytes 4 Bytes
Memory Memory
Address Contents Address Contents
NEWITEM 2000 6 NEWITEM 2000 6
ITEM 2004 ITEM 2004 6
3012 3012
3016 3016
S 3020 6 3020 6
P S
P
SP 3020 SP 3024
9 September 2020
Question
Register R5 is used in a program to point to top of a stack. Consider each word length in stack is of 32-bits.Write a sequence of
instructions using the Index, Autoincrement and Autodecrement addressing modes to perform each of the following
a. Pop the top two items off the stack, add them and then push the result onto the stack.
b. Copy the fifth item from the top into register R3 4
Bytes
c. Remove the top ten items from stack Address Memory Contents
R 3000 10
5 3004 20
3008 30
3012 40
3016 50
3020 60
3024 70
3028 80
3032 90
3036 100
3040
9 September 2020
Answer
Register R5 is used in a program to point to top of a stack. Consider each word length in stack is of 32-bits.Write a sequence of
instructions using the Index, Autoincrement and Autodecrement addressing modes to perform each of the following
a. Pop the top two items off the stack, add them and then push the result onto the stack.
b. Copy the fifth item from the top into register R3 4
Bytes
c. Remove the top ten items from stack Address Memory Contents
Answer R 3000 10
(a) 5 3004 20
Move (R5)+,R0
3008 300
Add (R5)+,R0
3012 400
Move R0,-(R5)
3016 500
3020 60
3024 70
3028 80
3032 90
3036 100
3040
9 September 2020
Subroutines
9 September 2020
Subroutine to Program to add n numbers using looping
Subroutine
Main LISTADD Address
Address Program
Clear R0 2000
3000 Move N,R1
Loop: Add (R2)+, R0 2004
3004 Move #Num1,R2
3008 Call LISTADD Decrement R1 2008
Return 2016
Memory
4 Bytes Address
N 4 1000
Sum 1004
Num1 2 1008
Num2 1 1012
Num3 3 1016
Num4 10 1020
9 September 2020
Subroutines: Parameter Passing
⚫ The exchange of information between a calling program and a subroutine is
referred to as Parameter passing.
⚫ The parameters may be passed using
◦ Registers
◦ Memory Location
◦ Stack
9 September 2020
Subroutine: Passing of Parameters through Registers
Subroutine
Main LISTADD Address
Address Program
Clear R0 2000
3000 Move N,R1
Loop: Add (R2)+, R0 2004
3004 Move #Num1,R2
3008 Call LISTADD Decrement R1 2008
Return 2016
Memory
4 Bytes Address
N 4 1000 In calling program,
Register R1 and R2 is used to pass the parameters N and
Sum 1004 address of Num1 location
Num1 2 1008
- N is passed using: Pass by Value
Num2 1 1012 - Address of Num1 is passed using: Pass by Reference.
Num3 3 1016
The called Subroutine: LISTADD, returns the parameter
Num4 10 1020 total value through the register R0
9 September 2020
Subroutine: Passing of Parameters through Stack
Main Subroutine
Address Program LISTADD Address
Memory
4 Bytes
Clear R0 2020
Address
Loop: Add (R2)+, R0 2024
N 3 1000
Decrement R1 2028
Sum 1004
Branch>0 Loop 2032
Num1 2 1008
Num2 1 1012 Move R0,20(SP) 2036
Move
9 September 2020 (SP)+, R0 2048
Stack Frame
⚫ Stack Frame refers to locations that constitute a private work-space for the subroutines
⚫ The work space is
◦ Created at the time the subroutine is entered and
◦ Freed up when the subroutine returns control to the calling program
Figure 2.27:
Subroutine stack
frame example
9 September 2020
Frame Pointer and Operation on Stack Frame
9 September 2020
Shift Instructions
Shift Instructions
Logical Arithmetic
Left Right
Left Right
9 September 2020
Logical Left Shift Instruction
C 0
MS LS
B B
Logical Left Shift
General Syntax: LShiftL Count,
DestinationOperand
LShiftL #2, R1
Before Executing the Instruction After Executing the Instruction
C R C R
1 1
0 1 0 1 0 1 1 0 1 1 0 1 0 1 1 0 1 0
After 1st
Shift
C R
1
0 1 0 1 1 0 1 0 0
After 2nd
Shift
9 September 2020
Logical Right Shift Instruction
0 C
MS LS
B B
Logical Right Shift
General Syntax: LShiftR Count, DestinationOperand
LShiftR #2, R1
Before Executing the Instruction After Executing the Instruction
R C R C
1 1
1 0 1 0 1 1 0 1 0 0 1 0 1 0 1 1 0 1
After 1st
Shift
R C
1
0 0 1 0 1 0 1 1 0
After 2nd
Shift
9 September 2020
Arithmetic Right Shift Instruction
C
MS LS
B B
Arithmetic Right Shift
General Syntax: AShiftR Count,
DestinationOperand
RShiftR #2, R1
Before Executing the Instruction After Executing the Instruction
R C R C
1 1
1 0 1 0 1 1 0 1 0 1 1 0 1 0 1 1 0 1
After 1st
Shift
R C
1
1 1 1 0 1 0 1 1 0
After 2nd
Shift
9 September 2020
Question
The content of a 4-bit register is initially 1101. The register
is logically shifted 2 times to the right. What is the content
of the register after each shift?
a. 1110, 0111
b. 0001, 1000
c. 1101, 1011
d. 0110, 0011
9 September 2020
Question
The content of a 4-bit register is initially 1101. The register
is logically shifted 2 times to the right. What is the content
of the register after each shift?
a. 1110, 0111
b. 0001, 1000
c. 1101, 1011
d. 0110, 0011
9 September 2020
Question
If a register containing data 11001100 is subjected to
logical shift left operation of 1 bit, then the content of the
register after 'LshiftL' shall be
a. 01100110
b. 10011001
c. 11011001
d. 10011000
9 September 2020
Question
If a register containing data 11001100 is subjected to
logical shift left operation of 1 bit, then the content of the
register after 'LshiftL' shall be
a. 01100110
b. 10011001
c. 11011001
d. 10011000
9 September 2020
Rotate Instructions
Rotate Instructions
Left Right
Left Right
9 September 2020
Rotate Left with Carry
C
MS LS
B B
General Syntax: RotateLC Count, DestinationOperand
RotateLC #1, R1
Before Executing the Instruction After Executing the Instruction
C R C R
1 1
0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 0
9 September 2020
Rotate Left without Carry
C
MS LS
B B
General Syntax: RotateL Count, DestinationOperand
RotateL #1, R1
Before Executing the Instruction After Executing the Instruction
C R C R
1 1
0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1
9 September 2020
Rotate Right with Carry
C
MS LS
B B
General Syntax: RotateRC Count, DestinationOperand
RotateRC #1, R1
Before Executing the Instruction After Executing the Instruction
R C R C
1 1
1 0 1 1 0 1 0 0 0 0 1 0 1 1 0 1 0 0
9 September 2020
Rotate Right without Carry
C
MS LS
B B
General Syntax: RotateR Count, DestinationOperand
RotateR #1, R1
Before Executing the Instruction After Executing the Instruction
R C R C
1 1
1 0 1 1 0 1 0 0 0 0 1 0 1 1 0 1 0 0
9 September 2020
Basic Input/Output
Operations
9 September 2020
I/O
⚫ The data on which the instructions operate are not
necessarily already stored in memory.
⚫ Data need to be transferred between processor and
outside world (disk, keyboard, etc.)
⚫ I/O operations are essential, the way they are performed
can have a significant effect on the performance of the
computer.
9 September 2020
Program-Controlled I/O Example
⚫ Read in character input from a keyboard and produce
character output on a display screen.
⮚ Rate of data transfer (keyboard, display, processor)
⮚ Difference in speed between processor and I/O device creates the need for
mechanisms to synchronize the transfer of data.
⮚ A solution: on output, the processor sends the first character and then waits
for a signal from the display that the character has been received. It then
sends the second character. Input is sent from the keyboard in a similar way.
9 September 2020
Program-Controlled I/O Example
- Registers
- Flags
- Device interface
9 September 2020
Program-Controlled I/O Example
⚫ Machine instructions that can check the state of the status
flags and transfer data:
READWAIT Branch to READWAIT if SIN = 0
Input from DATAIN to R1
9 September 2020
Program-Controlled I/O Example
⚫ Memory-Mapped I/O – some memory address values are
used to refer to peripheral device buffer registers. No
special instructions are needed. Also use device status
registers.
9 September 2020
Program-Controlled I/O Example
9 September 2020
Encoding of Machine
Instructions
9 September 2020
Encoding of Machine Instructions
⚫ Assembly language program needs to be converted into machine instructions. (ADD = 0100
in ARM instruction set)
⚫ In the previous section, an assumption was made that all instructions are one word in length.
⚫ OP code: the type of operation to be performed and the type of operands used may be
specified using an encoded binary pattern
⚫ Suppose 32-bit word length, 8-bit OP code (how many instructions can we have?), 16
registers in total (how many bits?), 3-bit addressing mode indicator.
⚫ Add R1, R2
⚫ Move 24(R0), R5
⚫ LshiftR #2, R0 8 7 7 1
⚫ Move #$3A, R1 0
⚫ Branch>0 LOOP OP
code
Sourc
e
Des
t
Other info
9 September 2020
Encoding of Machine Instructions
⚫ What happens if we want to specify a memory operand using the Absolute
addressing mode?
⚫ Move R2, LOC
⚫ 14-bit for LOC – insufficient
⚫ Solution – use two words
9 September 2020
Encoding of Machine Instructions
⚫ Then what if an instruction in which two operands can be specified
using the Absolute addressing mode?
⚫ Move LOC1, LOC2
⚫ Solution – use two additional words
⚫ This approach results in instructions of variable length. Complex
instructions can be implemented, closely resembling operations in
high-level programming languages – Complex Instruction Set
Computer (CISC)
9 September 2020
Encoding of Machine Instructions
⚫ If we insist that all instructions must fit into a single 32-bit word, it is
not possible to provide a 32-bit address or a 32-bit immediate operand
within the instruction.
⚫ It is still possible to define a highly functional instruction set, which
makes extensive use of the processor registers.
⚫ Add R1, R2 ----- yes
⚫ Add LOC, R2 ----- no
⚫ Add (R3), R2 ----- yes
9 September 2020