Module 2 Lk1
Module 2 Lk1
• Assume byte addressable memory and each number is of 32-bits. So, each
number occupies 4 byte space in memory.
• Store the order of the matrix (`n') in location NUM.
• Results are stored starting from location MATC.
Example 1. Consider a database of test scores of students which is stored in memory as shown in Fig.
4. Each student is allocated a 4-word memory block indicating student ID and the marks scored in 3
tests. There are 'n' students in the class and this value n is stored in memory location N. Compute the
sum of the scores obtained by all the students in II test.
Solution: Word length is assumed to be 32-bits. So each entry in the memory occupies 4 byte space.
For the given problem, we need to add the scores obtained by all the students in II test.
We use the memory map given in Fig.6.
• The details of the students are stored from location LIST onwards.
• Let R2 register be used as index register.
• Rl holds the count value (n)
• R0 is used for summing
Compiled by: Prof. Krishnananda L, HoD, Department of ECE, GEC Mosalehosahalli, Hassan 4
COMPUTER ORGANIZATION AND ARCHITECTURE BEC306C MODULE-2
Fig.7 ALP to add marks of students in 2nd test using Indexed addressing mode.
Note: Index mode is useful when an operand location is specified relative to a reference or base
address in the data structure
Modified program
Compiled by: Prof. Krishnananda L, HoD, Department of ECE, GEC Mosalehosahalli, Hassan 5
COMPUTER ORGANIZATION AND ARCHITECTURE BEC306C MODULE-2
(b) Auto-decrement Mode: This mode is useful to access an array in the reverse order.
The value of the pointer register specified in the instruction is decremented first and
this value is used as the effective address of the operand.
We denote the auto-decrement mode by putting the specified register in parentheses
preceded by a minus sign to indicate that the value of register is decremented before
accessing the operand. Thus, it is represented as, -(Ri). Again, the decrement value is
according to the size of the operand.
These two modes are useful to implement a data structure called stack.
Example 2.4 Write an assembly language program for addition of `n' numbers stored
in memory using (i) Auto-increment and (ii) Auto-decrement addressing modes.
Solution: The memory map is shown in Fig.8. The numbers are stored consecutively
starting from NUM1 until NUMn. We take a byte-addressable memory and 32-bit
operands. i.e., address NUM2 = NUM1 + 4.
Compiled by: Prof. Krishnananda L, HoD, Department of ECE, GEC Mosalehosahalli, Hassan 6
COMPUTER ORGANIZATION AND ARCHITECTURE BEC306C MODULE-2
Auto-decrement mode:
2.2 Assemblers
`Assembler' is a system software which translates the programs written in assembly
language into a sequence of 0s and 1s, called machine language.
The assembler is stored in the memory of the computer. A user/source program is
written in Assembly language using a Text editor.
▪ The user program is called the source program (.asm) and the assembler
converts it to object program (.obj). The object program is later converted to
Executable mode (.exe) with library functions, which is a binary sequence of 0s
and 1s.. These patterns are taken by the processor and will be executed.
Compiled by: Prof. Krishnananda L, HoD, Department of ECE, GEC Mosalehosahalli, Hassan 7
COMPUTER ORGANIZATION AND ARCHITECTURE BEC306C MODULE-2
▪ With the help of another system software called loader, assembler also assigns
physical addresses to the instructions of the source program.
We need to make changes to the program at every place where the variable is used.
Similarly, we may need to reserve some locations in the memory in advance, to store
the data. All such kinds of applications require special support from the assembler.
So, we use some commands which inform the assembler to do a specific task. Such
commands are called assembler directives. These commands are not translated into
machine language. So they are not executed by the processor and hence are known as
pseudo OPcodes or pseudo codes.
Compiled by: Prof. Krishnananda L, HoD, Department of ECE, GEC Mosalehosahalli, Hassan 8
COMPUTER ORGANIZATION AND ARCHITECTURE BEC306C MODULE-2
➢ General instructions like MOVE cannot be used for I/O data transfer.
➢ The address space is not shared between I/O and memory. Hence, there is no
conflict between I/O access and memory access.
The keyboard needs a buffer register to store the code of the key pressed. Similarly the
display unit is required to store the character sent from the processor. These registers
are called DATAIN and DATAOUT, respectively.
When I/O devices are connected to the processor, there will be speed mismatch and
mechanisms are needed to synchronize data transfer between them.
Our task is to read a character from keyboard and display it on the screen.
Compiled by: Prof. Krishnananda L, HoD, Department of ECE, GEC Mosalehosahalli, Hassan 10
COMPUTER ORGANIZATION AND ARCHITECTURE BEC306C MODULE-2
Operation:
The operation of reading a character from the keyboard and displaying it on the output
screen can now be formally stated as below:
→→ Write a program to monitor the status of FIN. When FIN is 1, a valid character is
present in DATAIN register and processor can read this register. When the character
is transferred to the processor register, say R1, FIN is automatically cleared to 0. If a
second character is entered, FIN is again set to 1 and the process repeats. Thus, FIN =
0 is the condition when processor waits and when FIN = 1, it can read the data.
This operation can be written as,
READWAIT Branch to READWAIT if FIN = 0
Input from DATAIN to R1
→→ The data buffer registers DATAIN and DATAOUT and the corresponding status
flags FIN and FOUT form part of a circuitry known as device interface.
→→ The two buffer registers DATAIN and DATAOUT can be accessed as if they are
two memory locations. The status flags FIN and FOUT are made a part of device status
register called STATUS, by assigning bit position to them as shown:
The Testbit instruction tests the state of the specified bit in the destination specified
i.e., STATUS register. If the bit tested is equal to 0, then the branch condition is true
and a branch is made to the beginning of the wait loop. When the testbit is 1, data is
read from the input buffer or written into the output buffer
Example 2.5 Write a program to read a line of text from keyboard and display it.
Solution: The characters are read one by one,stored in memory and then sent (echoed)
to the display. The program checks whether the character keyed in is a carriage return
(CR) key. If not, it proceeds to read the next character. If yes, the program terminates.
The characters are stored in memory starting from address labelled LOC. Register R0
is used to point to the memory and is initially loaded with the address LOC. R0 is
incremented for each character using auto increment addressing mode. The program
structure is shown in Fig. 12.
Fig.12 Program to read a line of text from keyboard and display on the screen
Compiled by: Prof. Krishnananda L, HoD, Department of ECE, GEC Mosalehosahalli, Hassan 12
COMPUTER ORGANIZATION AND ARCHITECTURE BEC306C MODULE-2
Note: pop operation will not remove the element from the top of stack. Instead, TOS
element is copied into the destination location
Compiled by: Prof. Krishnananda L, HoD, Department of ECE, GEC Mosalehosahalli, Hassan 14
COMPUTER ORGANIZATION AND ARCHITECTURE BEC306C MODULE-2
2.4.4 Queues:
·· This is a data structure similar to stack which can be used to hold data items.
▪ The queue is a First-in-First-out (FIFO) structure. Thus, a queue has two ends:
one entry end and one exit end.
▪ New data are added at the back or tail (high-address) end and retrieved from the
front or head (low-address) end of the queue.
▪ It is like a pipeline where items can be entered from one end and removed from
the other end.
2.5 Subroutines
Any programming task involves execution of a small set of instructions many times.
Such a subtask, consisting of a set of instructions, which is executed many times
(repeated) is called a subroutine.
• Only one copy of the subroutine is placed in memory (saves memory space) and
any program that requires the subroutine, will branch to the starting location of
the subroutine for fetching the first instruction.
• When a program branches to a subroutine, we say that it is calling the subroutine
and this branching is done with a Call instruction.
• Once the subroutine task is over, the calling program must resume execution
starting from the instruction immediately following the Call instruction. i.e.,
control is to be transferred (returned) back to the calling program. This is done
by executing a Return instruction at the end of the Subroutine.
Compiled by: Prof. Krishnananda L, HoD, Department of ECE, GEC Mosalehosahalli, Hassan 15
COMPUTER ORGANIZATION AND ARCHITECTURE BEC306C MODULE-2
Instead of LINK register, the processor stack is used to store the return address when
the Call instruction is executed. When Return instruction is executed, the return address
is popped from the stack and placed into PC.
Compiled by: Prof. Krishnananda L, HoD, Department of ECE, GEC Mosalehosahalli, Hassan 16
COMPUTER ORGANIZATION AND ARCHITECTURE BEC306C MODULE-2
Compiled by: Prof. Krishnananda L, HoD, Department of ECE, GEC Mosalehosahalli, Hassan 17
COMPUTER ORGANIZATION AND ARCHITECTURE BEC306C MODULE-2
We illustrate the program of adding ‘n’ numbers using stack. See Fig 20a)
• The parameter passed to this subroutine are the address of the first number in the
list and the size of the list. The subroutine performs the addition and returns the
computed sum.
• The stack structure is shown in Fig 20b). Before the subroutine is called, the SP
is at level 1. The calling program saves the address NUM1 and the value `n' into
the stack and calls the subroutine ARRAYADD. The “call" instruction pushes
the return address onto the stack. Now, SP is at level 2.
Compiled by: Prof. Krishnananda L, HoD, Department of ECE, GEC Mosalehosahalli, Hassan 18
COMPUTER ORGANIZATION AND ARCHITECTURE BEC306C MODULE-2
Compiled by: Prof. Krishnananda L, HoD, Department of ECE, GEC Mosalehosahalli, Hassan 19
COMPUTER ORGANIZATION AND ARCHITECTURE BEC306C MODULE-2
➢ The calling program stores the result in location SUM and top-of-stack is
restored to its original value by adding a constant 8 to SP. Now, SP is again at
level 1.
The program shows two methods of passing parameters. When actual operands or
entities are sent, it is called Call-by-value. For example, entity `n' is passed by value.
When the calling program passes the address of the variable instead of the variable
itself, it is called Call-by-reference. For example, we have passed NUM1 to the
subroutine which is the address of the first entry in the list.
• Not dst
NOT operation provides the 1's-complement of the operand specified in dst. The dst
may be a processor register or a memory location.
Similarly, the AND, OR and XOR operations are performed on two operands using
each pair of bits in the corresponding position. Ex: AND R1, R2
Compiled by: Prof. Krishnananda L, HoD, Department of ECE, GEC Mosalehosahalli, Hassan 20
COMPUTER ORGANIZATION AND ARCHITECTURE BEC306C MODULE-2
Example:
Consider that register R0 (32-bit) has four ASCII characters.
➢ We want to check whether the right most character is A.
➢ The ASCII code for A is 01000001 which is expressed in hexadecimal notation
as 41H.
➢ We need to mask the upper 3 bytes of the register R0 content leaving the lowest
byte as it is.
➢ Now, a compare instruction is to be used to check whether this byte is character
`A' or not. The instruction sequence is given below.
And #$000000FF, R0
Compare #$00000041, R0
Branch = 0 CHAR_FOUND
ASCII Table
Compiled by: Prof. Krishnananda L, HoD, Department of ECE, GEC Mosalehosahalli, Hassan 21
COMPUTER ORGANIZATION AND ARCHITECTURE BEC306C MODULE-2
For general operands, logical shift is used. For a signed number, arithmetic shift is used
which preserves the sign of the number.
There are many applications which require shifting or rotation of the numbers or
characters. Ex: Scrolling display
Compiled by: Prof. Krishnananda L, HoD, Department of ECE, GEC Mosalehosahalli, Hassan 22
COMPUTER ORGANIZATION AND ARCHITECTURE BEC306C MODULE-2
Ex: Illustrate the use of shift and logic operations for packing of decimal digits.
Solution:
➢ Let ASCII codes for decimal digits are stored in memory locations LOC and
LOC+1. The result is stored at location RESULT. The result is known as packed-
BCD format.
➢ We see that each character or digit is coded using 7-bits ASCII code. For ex,
digit 9 is represented as ASCII value 39 (0111001)
➢ The rightmost 4 bits of the ASCII code for a decimal digit corresponds to the
BCD code for the digit.
➢ Now, our task is to extract the lower 4-bits (nibble) in each of the data stored at
location LOC and LOC + 1. Then, these two nibbles should be concatenated as
a single byte and stored at location RESULT.
Compiled by: Prof. Krishnananda L, HoD, Department of ECE, GEC Mosalehosahalli, Hassan 23
COMPUTER ORGANIZATION AND ARCHITECTURE BEC306C MODULE-2
In this case, the sign bit is repeated into the bit towards its right and it is also put back
into the same position. Zeros are not inserted at the leftmost position as in logical right
shift.
An example is shown in Fig.
• Rotate instructions shift the bits of the operand either towards left or right.
• The rotation may or may not include the carry flag C.
• Fig. shows all the four possible cases of rotate instructions.
When two n-bit numbers are multiplied, the product can be as large as `2n' bits.
Compiled by: Prof. Krishnananda L, HoD, Department of ECE, GEC Mosalehosahalli, Hassan 24
COMPUTER ORGANIZATION AND ARCHITECTURE BEC306C MODULE-2
So, the product cannot be stored in register Rj. Hence, many processors accommodate
the product in two registers by placing high-order and low-order n-bits in two registers.
Example: Write a program to compute the dot product of two vectors A and B.
Solution: Consider two vectors A and B each of length n. The dot product between A
and B is
defined as,
Compiled by: Prof. Krishnananda L, HoD, Department of ECE, GEC Mosalehosahalli, Hassan 25