Mod 23
Mod 23
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
0 R0 C
after: 0 0 0 1 1 1 0 . . . 0 1
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
. . .
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 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
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
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
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–
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
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
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
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
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
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
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
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