Additional Instructions
Logic Instructions
AND,OR, NOT are the bitwise logic instructions.
NOT R0
AND #$F0,R0
OR R1,R0
Negate R0
Logical Shifts
Logical shift – shifting left (LShiftL) and shifting right
(LShiftR)
C R0 0
before: 0 0 1 1 1 0 . . . 0 1 1
LShiftL count, dst
after: 1 1 1 0 . . . 0 1 1 0 0
(a) Logical shift left
LShiftL #2,R0
0 R0 C
LShiftR count, dst
before: 0 1 1 1 0 . . . 0 1 1 0
after: 0 0 0 1 1 1 0 . . . 0 1
(b) Logical shift right
LShiftR #2,R0
Arithmetic Shifts
R0 C
AshiftR count,dst
before: 1 0 0 1 1 . . . 0 1 0 0
after: 1 1 1 0 0 1 1 . . . 0 1
(c) Ar ithmetic shift right 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
(a) Rotate left without carry
RotateL #2,R0
C R0
before: 0 0 1 1 1 0 . . . 0 1 1
after: 1 1 1 0 . . . 0 1 1 0 0
(b) Rotate left with carry
RotateLC #2,R0
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 carry
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 right with carry
RotateRC #2,R0
Figure 2.32. Rotate instructions.
Multiplication and Division
Not very popular (especially division)
Multiply R , R
i j
Rj ← [Ri] х [Rj]
2n-bitproduct case:
high-order half in R(j+1),Low-order half in R(j)
Divide R , R
i j
Rj ← [Ri] / [Rj]
Quotient is in Rj, remainder may be placed in R(j+1)
Stacks
7
Stacks
A stack is a list of data elements, usually words, with the accessing
restriction that elements can be added or removed at one end of the list
only. This end is called the top of the stack, and the other end is called
the bottom. The structure is sometimes referred to as a pushdown
stack.
Last-in–first-out (LIFO) data structure.
The term push is used to describe placing a new item on the stack .
The term pop is used to describe removing the top item from the stack.
The stack pointer, SP, is used to keep track of the address of the top
element of the stack.
8
Stack Organization
we assume that the stack grows in the
direction of decreasing addresses in
the memory, new data are added at
the top and also retrieved from the
top. (lower-address end).
9
Stack Operation
PUSH
Memory Stack
SUBTRACT #4,SP
PUSH MOVE NEWITEM,(SP)
SP ← SP – 4 POP
[SP] ← DR
MOVE (SP), ITEM
POP ADD #4,SP
DR ← [SP] PUSH
SP ← SP + 4 MOVE NEWITEM,-(SP)
POP
MOVE (SP)+, ITEM
10
Stack Operation
11
Stack Operation
Compare src,dst
Performs operation [dst] – [src] 12
And sets condition flags according to the result
Queue
13
Queue
FIFO Data structure.
Data are stored in and retrieved from a queue on a first-in–
first-out (FIFO) basis. Thus, if we assume that the queue
grows in the direction of increasing addresses in the memory,
new data are added at the back (high-address end) and
retrieved from the front (low-address end) of the queue.
14
Differences between a stack and a queue
Stack Queue
LIFO FIFO
One end is fixed other end for One end is to add item and
PUSH and POP item other is to remove item
One pointer used Two Pointer is used
Fixed Size Not fixed size
15
Subroutines
16
Subroutines
In a given program, it is often necessary to perform a particular task many times on
different data values. It is prudent to implement this task as a block of instructions
that is executed each time the task has to be performed. Such a block of instructions
is usually called a subroutine.
However, to save space, only one copy of this block is placed in the memory, and
any program that requires the use of the subroutine simply branches to its starting
location.
When a program branches to a subroutine we say that it is calling the subroutine.
The instruction that performs this branch operation is named a Call instruction.
After a subroutine has been executed, the calling program must resume execution,
continuing immediately after the instruction that called the subroutine. The subroutine
is said to return to the program that called it, and it does so by executing a Return
instruction.
17
Subroutines
Since the subroutine may be called from different places in a calling program,
provision must be made for returning to the appropriate location. The location
where the calling program resumes execution is the location pointed to by the
updated program counter (PC) while the Call instruction is being executed.
Hence, the contents of the PC must be saved by the Call instruction to enable
correct return to the calling program.
The way in which a computer makes it possible to call and return from subroutines
is referred to as its subroutine linkage method.
The simplest subroutine linkage method is to save the return address in a specific
location, which may be a register dedicated to this function. Such a register is called
the link register. When the subroutine completes its task, the Return instruction
returns to the calling program by branching indirectly through the link register.
18
Subroutines
The Call instruction is just a special branch instruction that
performs the following operations:
Store the contents of the PC in the link register
Branch to the target address specified by the Call instruction
The Return instruction is a special branch instruction that
performs the operation
Branch to the address contained in the link register
19
Subroutines
20
Subroutine Nesting and the Processor
Stack
A common programming practice, called subroutine nesting, is to
have one subroutine call another.
In this case, the return address of the second call is also stored in
the link register, overwriting its previous contents. Hence, it is
essential to save the contents of the link register in some other
location before calling another subroutine. Otherwise, the return
address of the first subroutine will be lost.
That is, return addresses are generated and used in a last-in–first-
out order. This suggests that the return addresses associated with
subroutine calls should be pushed onto the processor stack.
21
Parameter Passing
When calling a subroutine, a program must provide to the
subroutine the parameters, that is, the operands or their
addresses, to be used in the computation. Later, the subroutine
returns other parameters, which are the results of the computation.
This exchange of information between a calling program and a
subroutine is referred to as parameter passing.
Parameter passing may be accomplished in several ways. The
parameters may be placed in registers, in memory locations, or on
the processor stack where they can be accessed by the
subroutine.
22
Program of subroutine
Parameters passed through registers.
Calling Program Subroutine
1. Move N, R1 1. LISTADD: Clear R0
2. Move #NUM1,R2 2. LOOP: Add (R2)+,R0
3. Call LISTADD 3. Decrement
4. Move R0,SUM R1
4. Branch>0
LOOP
5. Return
23
Parameter Passing by Value and by
Reference
Instead of passing the actual Value(s), the calling program
passes the address of the Value(s). This technique is called
passing by reference.
The second parameter is passed by value, that is, the actual
number of entries, is passed to the subroutine.
24
Program of subroutine
Parameters passed on the stack.
MoveMultiple R0-R2, -(SP)
MoveMultiple to store contents of register R0 through R2 on
he stack
25
Basic Input/Output
Operations
26
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.
27
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.
28
Program-Controlled I/O Example
Bus
Processor
DATAIN DATAOUT
SIN SOUT
- Registers
- Flags Keyboard Display
- Device interface
Figure 2.19 Bus connection for processor , keyboard, and display.
29
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
WRITEWAIT Branch to WRITEWAIT if SOUT = 0
Output from R1 to DATAOUT
30
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.
E.g. Movebyte DATAIN,R1
Movebyte R1,DTATOUT
READWAIT Testbit #3, INSTATUS
Branch=0 READWAIT
MoveByte DATAIN, R1
WRITEWAIT Testbit #3, OUTSTATUS
Branch=0 WRITEWAIT
MoveByte R1, DATAOUT
31
32
Program-Controlled I/O Example
Assumption – the initial state of SIN is 0 and the
initial state of SOUT is 1.
Any drawback of this mechanism in terms of
efficiency?
Two wait loopsprocessor execution time is wasted
Alternate solution?
Interrupt
33