Computer Organization and Architecture: Chapter 1: Introduction

Introduction of Computer Organization and Architecture: Important Example Solved Problems

Computer Organization and Architecture: Anna University Solved Problems, Assignment Problems and Important Solved Problems


Operands of Computer Hardware


Example:1

Let's assume that A is an array of 50 words and that the compiler has associated the variables p and q with the registers $s1 and $s2. Let's also assume that the array's starting address, or base address, is in $s3. Compile this C assignment statement : p = q + A[7];

Solution:

Here, one of the operands is in the memory, so we have first to transfer A[7] to a temporary register. This is achieved by –

 lw $t0, 7 ($s3)   // Temporary reg $t0 gets A[7]

The address of this array element is the sum of the base of the array A, found in register $s3, plus the number to select element 7.

The instruction must add q (contained in $s2) to A[7] (contained in $t0) and put the sum in the register corresponding to p (associated with $s1). This is achieved by –

 add $s1, $s2, $t0  // p = q + A[7]

Note: The constant in a data transfer instruction is called the offset, and the register added to form the address ($s3) is called the base register.


Example:2

Assume variable p is associated with register $s2 and the base address of the array A is in $s3. What is the MIPS assembly code for the C assignment statement A[10]=p+A[7] ?

Solution:

We have first to transfer A[7] to a temporary register. This is achieved by ‒

lw $t0, 28 ($s3)  //Temporary reg $t0 gets A[7]

The instruction must add p (contained in $s2) to A[7] (contained in $t0) and put the sum in the temporary register since it is finally should be stored in memory.

add $to, $s2, $t0 //Temporary reg $t0 gets p + A[7]

The final instruction stores the sum into A[10], using 40 (4 × 10) as the offset and register $s3 as the base register.

sw $t0, 40 ($s3)  // Stores p + A[7] back into A[10]



Example: 3

Assume variable b is associated with register $s2 and the base address of the array A is in $s3. What is the MIPS assembly code for the C assignment statement below?

A[10] A[5]+b;

Solution:

Although there is a single operation in the C statement, now two of the operands are in memory, so we need even more MIPS instructions.

lw $t0,20($s3)   //Temporary reg $t0 gets A[5] using 16 (4 × 5)

add $t0,$s2,$to   //Temporary reg $t0 gets A[5] + b

sw $t0,40($s3)   // Stores b + A[5] back into A[10] using 40 (4 x10) as the offset and

         // register $s3 as the base register.


Example:4

Assume that the variables f, g, h, i and j are assigned to registers $50, $s1, $s2, $s3, and $s4, respectively. Assume that the base address of the arrays A and B are in registers $s6 and $s7, respectively. C Code : f= g + A[B[4]‒B[3]]; For the C statement above, what is the corresponding MIPS assembly code?

Solution :

lw $t0, 16($s7)   // $t0 = B[4]

lw $t1, 12($s7)   // $t1 = B[3]

sub $t0, $t0, $t1  // $t0 = B[4] – B[3]

sll $t0, $t0, 2    // $t0 = $t0 * 4

add $t0, $t0, $s6  // $t0 = &A[B[4] – B[3]]

lw $t1, 0($t0)   // $t1 = A[B[4] ‒ B[3]]

add $s0, $s1, $t1  // f = g + A[B[4] ‒ B[3]]


Example:5

Assume that the variables f and g are assigned to registers $s0 and $s1 respectively. Assume that the base address of the array A is in register $$2. Assume f is zero initially.

f = ‒g ‒ A[4]

A[5] = f+100;

Translate the above C statements into MIPS code. How many MIPS assembly instructions are needed to perform the C statements and how many different registers are needed to carry out the C statements?

Solution :

lw $t0, 16[$s2]   // Save A[4] into register t0

sub $s0, $s0, $t0  // f = f ‒ A[4]

sub $s0, $s0, $s1  //f = f‒g

add i $s0, $s0, 100  // f + 100

lw 20[$s2], $s0   // Save f into A[5]

For registers are needed to carry out the C statements


Example:6

The following problems deal with translating from C to MIPS. Assume that the variables f, g, h, i and j are assigned to registers $s0, $s1, $s2, $s3 and $s4, respectively. Assume that the base address of the arrays A and B are in registers $s6 and $s7, respectively.

a.ƒ = ‒ g ‒A[5]; b. B[7] =A[ i ‒ j];

Solution :

a.

lw $t0, 20[$s6]   // save A[5] into register to

add $t0, $t0, $s1   // g + A[5]

sub $s0, $zero, $t0  //f=0‒(g+ A[5])

b.

$t0, $s3, $s4   //i‒j

add $t0, $t0, $s6 // puts i‒ j into array

lw $t0, 0($t0)   // makes to its own address

sw $t0, 28($s7)  // throws B[7] into register to



Assembly Language Programming



Example 1: Add Two Numbers

Program to add two numbers stored in memory and store the result back in memory.

Program:

.data

num1: .word 10

num2: .word 20

result: .word 0.

.text

main:

lw $t0, num1  # Load num1 → $t0

lw $t1, num2  # Load num2 → $t1

add $t2, $to, $t1 # $t2 = $t0 + $t1

sw $t2, result  #Store result→ memory

j end     #End program

end:


Example 2 : Subtract and Store Result

Program to perform subtraction of two numbers and save the result in memory.

Program:

.data

num1: .word 50

num2: .word 30

diff: .word 0

.text

main:

       lw $t0, num1

       lw $t1, num2

       sub $t2, $t0, $t1 # $t2 = $t0 ‒ $t1

       sw $t2, diff

       j end

end:


Example 3 : Sum of First 10 Natural Numbers

Program to calculate 1+2+3+ ... + 10 and store the result.

Program:

.data

sum: .word 0

.text

main:

       li $t0, 1  #i=1

       li $t1, 0  # sum = 0

loop:

       add $t1, $t1, $t0  #sum= sum + i

        addi $t0, $t0, 1  #sum #i = i+1

       ble $t0, 10, loop  # continue until i < = 10

       sw $t1, sum   # store result

       j end

end:


Example 4 : Find the Larger of Two Numbers

Program to compare two numbers and store the larger one.

Program:

.data

num1: .word 45

num2: .word 78

larger: .word 0

.text

main:

       lw $t0, num1

       lw $t1, num2

       bgt $t0, $t1, num1_is_larger

       sw $t1, larger # if num2 > num1

       j end

       num1_is_larger:

       sw $t0, larger

end:


Example 5 : Factorial of a Number (Loop‒Based)

Program to calculate the factorial of 5 (i.e., 5!= 120) and store the result in memory.

Program:

.data

num: .word 5

fact: .word 1

.text

main:

       lw $t0, num    #n = 5

       li $t1, 1     # fact = 1

loop:

       mul $t1, $t1, $t0  # fact fact* n

       addi $to, $t0, ‒1  # n=n‒1

       bgtz $t0, loop   # loop until n > 0

       sw $t1, fact    #store factorial

       j end  

end:


Example 6 : Array Sum

Program to sum the elements of an array,

Program:

 .data

array: .word 1, 2, 3, 4, 5

sum: .word 0

n: .word 5

.text

main:

       la $t0, array  # address of array

       lw $t3, n   # number of elements (5)

       li $t1, 0    # sum = 0

       li $t2, 0    # i = 0

loop:

       lw $t4, 0($t0)   # load array[i]

       add $t1, $t1, $t4 # sum += array[i]

       addi $t0, $t0, 4  # move to next element

       addi $t2, $t2, 1  #i++

       blt $t2, $t3, loop # loop if i < n

       sw $t1, sum   # store result

       j end

end:


Performance Metrics



Example : 1

If computer A runs a program in 10 seconds and computer B runs the same program in 15 seconds, how much faster is A than B ?

Solution:

We know that A is n times faster than B if

PerformanceA / Performance= Execution timeB / Execution time= n

Thus the performance ratio is

15/10 = 1.5

and A is therefore 1.5 times faster than B.

• In the above example, we could also say that computer B is 1.5 times slower than computer A, since

Performance A / PerformanceB = 1.5

means that

PerformanceA / 15 = PerformanceB

• For simplicity, we will normally use the terminology faster than when we try to compare computers quantitatively. Because performance and execution time are reciprocals, increasing performance requires decreasing execution time. To avoid the potential confusion between the terms increasing and decreasing, we usually say "improve performance" or "improve execution time" when we mean "increase performance" and "decrease execution time".


Example : 2

Computer A runs a program in 12 seconds with a 3 GHz clock. We have to design a computer B such that it can run the same program with in 9 seconds. Determine the clock rate for computer B. Assume that due to increase in clock rate, CPU design of computer B is affected and it requires 1.2 times as many clock cycles as computer A for execution this program.

Solution:

Given: Clock rateA = 3 x 109 cycles/sec

CPU time A = 12 seconds

 CPU timeB = 9 seconds

We have,

CPU time= CPU clcok cyclesA / Clock rateA

12 seconds = CPU clock cycles/ (3 x 109 cycles/sec)

CPU clock cycles= 12 seconds × 3 × 10cycle / sec = 36 x 109 cycles

The CPU time for computer B can be given as

CPU time= CPU clcok cyclesB / Clock rate= [ 1.2× CPU clock cyclesA ] / [ Clock rate]

9 seconds = [ 1.2 × 36 × 109 cycles ] / [ Clock rate]

Clock rate= [ 1.2 × 36 × 109 cycles ] / [ 9 seconds ] = 4.8 cycles/sec = 4.8 GHz


Example : 3

A program runs in 10 seconds on computer A, which has a 2 GHz clock. Try to help a computer designer build a computer, B, which will run this program in 6 seconds. The designer has determined that a substantial increase in the clock rate is possible, but this increase will affect the rest of the CPU design, causing computer B to require 1.2 times as many clock cycles as computer A for this program. What clock rate should we tell the designer to target?

Solution: Given: Clock rateA = 2 × 109 cycles/sec

CPU timeA = 10 seconds

CPU timeB = 6 seconds

We have,

CPU timeA = CPU clock cyclesA / Clock rateA

10 seconds = CPU clock cyclesA / [2× 109 (cycles/second)]

CPU clock cyclesA = 10 seconds × [2× 109 (cycles/second)] = 20 x 109 cycles

CPU time for B can be found using this equation:

CPU time= [1.2 × CPU clock cyclesA ] / Clock rateB

6 seconds = [1.2 × 20 × 109 cyclesA] / Clock raеB

Clock rateB = [ 1.2 × 20 × 109 cycles] / 6 seconds = [ 0.2 × 20 × 109 cycles] / seconds

= [4 × 109 cycles] / seconds = 4 GHz

To run the program in 6 seconds, B must have twice the clock rate of A.

Basic performance equation

The basic performance equation is given by

T = [N× CPI ] / R = (N× CPI) / Clock rate = N × CPI × Clock cycle time

where CPI: The average number of clock cycles each instruction takes to execute.

It is given by

CPI = CPU clock cycles / Instruction count

N: Number of instructions.

R: Clock rate measured in clocks / seconds.


Example: 4

Let us assume that two computers use same instruction set architecture. Computer A has a clock cycle time of 250 ps and a CPI of 2.0 for same program and computer B has a clock cycle time of 500 ps and a CPI of 1.2 for the same program. Which computer is faster for this program and by how much?

Solution: We know that each computer executes the same number of instructions for the program; let's call this number N. First, find the number of processor clock cycles for each computer:

CPU clock cyclesA = N×20

CPU clock cyclesB = N× 1.2

The CPU time for each machine will be

CPU timeA = CPU clock cycles× Clock cycle timeA

= N× 2.0 × 250 ps = 500 N ps

CPU timeB = CPU clock cycles× Clock cycle timeB

= N × 1.2 × 500 ps = 600 N ps

Thus we can say that computer A is faster. The amount faster is given by the ratio of the execution times.

CPU PerformaceA / CPU PerformanceB = Execution timesB / Execution time=

600 N ps 500 N ps =1.2

We can conclude that computer A is 1.2 times faster than computer B for this program.


Example: 5

Table 1.11.1 (a) shows the two code sequences with number of instructions of different instruction classes within each code sequence, respectively. The instructions are classified as A, B and C according to the CPI as shown in Table 1.11.1 (b). From the given information:

i) Determine which code sequence executes the most instructions.

ii) Determine which code sequence will execute quickly,

iii) Determine the CPI for each code sequence.


Solution:

 i) Code sequence 1 executes : 4+2 + 4 = 10 instructions.

Code sequence 2 executes : 8+2+2 = 12 instruction.

Therefore, code sequence 2 executes more instructions.

ii) CPU clock cycles required to execute these code sequences is given as

CPU clock cycles 1 = (4 × 1) + (2 × 2) + (4 × 3) = 20 cycles

CPU clock cycles 2 = (8 × 1) + (2 × 2) + (2 × 3) = 18 cycles

Thus, we can say that code sequence 2 is faster than code sequence 1, even though it executes two extra instructions.

iii) We know that

CPI = CPU clock cycles / Instruction count

CPI1 = 20 / 10 and CPI2 = 18 / 12 = 1.5



Example: 6

Consider three different processors P1, P2 and P3 executing the same instruction set. Pl has a 3 GHz clock rate and a CPI of 1.5. P2 has a 2.5 GHz clock rate and a CPI of 1.0. P3 has a 4.0 GHz clock rate and has a CPI of 2.2.

a) Which processor has the highest performance expressed in instructions per second?

b) If the processors each execute a program in 10 seconds, find the number of cycles and the number of instructions in each processor.

Solution:

a) P2 has the highest performance.

Performance of P1 (instructions/sec) = (3 x 109) / 1.5 = 2 × 109

Performance of P2 (instructions/sec) = (2.5 x 109) / 1.0 = 2.5 × 109

Performance of P3 (instructions/sec) = (4 × 109) / 2.2 = 1.82 x 109

b)

 Number of cycles = Time × Clock rate

Cycles (P1) = 10 × 3 × 109 = 30 × 109

Cycles (P2) = 10 × 2.5 × 109 = 25 × 109

Cycles (P3) = 10 × 4.0 × 10= 40 × 109

Time = (Number of instructions × CPI) / Clock rate

Number of instructions = (Time × Clock rate) / CPI = Number of cycles / CPI

No. of Instructions (P1) = 30×109 /1.5 = 20x 109

No. of Instructions (P2) = 25×109 / 1.0 = 25 × 109

No. of Instructions (P3) = 40×109 / 2.2 = 18.18 × 109


Example: 7

Consider two different implementations of the same instruction set architecture. The instructions can be divided into four classes according to their CPI (class A, B, C and D). P1 with a clock rate of 2.5 GHz and CPIs of 1, 2, 3 and 3 respectively and P2 with a clock rate of 3 GHz and CPIs of 2, 2, 2 and 2 respectively. Given a program with a dynamic instruction count of 1.0 × 106 instructions divided into classes as follows: 10% class A, 20% class B, 50 % class C and 20 % class D, which implementation is faster? What is the global CPI for each implementation? Find the What is the clock cycles required in both cases

Solution:

a) Class A: 105 instruction,

Class B: 2 × 105 instruction,

Class C: 5 × 105 instruction,

Class D: 2 × 105 instruction.

Time = No. instruction × CPI / clock rate

Total time P1 = (10+ 2×105×2 + 5×10× 3 + 2×105×3) / (2.5 × 109) = 10.4 × 10 ‒ 4 S

Total time P2 = (105×2+ 2×105 × 2 + 5×105 × 2+2×105 × 2) / (3 × 109)

      = 6.66 × 10‒ 4 S

CPI (P1) = 10.4× 10‒ 4 × 2.5 × 109 /106 = 2.6.

CPI (P2) = 6.66 × 10‒ 4 × 3 × 109 /106 = 2.0

P2 implementation is faster.

b)

Clock cycles (P1) =105 × 1 + 2 × 105 × 2+5 × 105 × 3+2 × 105 × 3 = 26 × 105

Clock cycles (P2) =105 × 2+2 ×105 ×2+5× 105 ×2 + 2 × 105 ×2+2 × 105 × 2

        = 20 × 105

 

Computer Organization and Architecture: Chapter 1: Introduction : Tag: Computer : - Introduction of Computer Organization and Architecture: Important Example Solved Problems


Computer Organization and Architecture: Chapter 1: Introduction



Under Subject


Computer Organization and Architecture

CW25201 3rd Semester IT department. | 2025 Regulation | 3rd Semester 2025 Regulation



Related Subjects


Discrete Mathematics

MA25C14 3rd Semester CSE,IT,CY,AIDS departments. | 2025 Regulation | 3rd Semester 2025 Regulation


Data Structures

CS25C08 3rd Semester CSE,IT,CY,AIDS departments. | 2025 Regulation | 3rd Semester 2025 Regulation


Computer Organization and Architecture

CW25201 3rd Semester IT department. | 2025 Regulation | 3rd Semester 2025 Regulation


Object Oriented Programming

CS25C07 3rd Semester IT department. | 2025 Regulation | 3rd Semester 2025 Regulation


Web Technologies

IT25301 3rd Semester IT department. | 2025 Regulation | 3rd Semester 2025 Regulation


English Communication Skills Laboratory I

EN25C03 3rd Semester all department. | 2025 Regulation | 3rd Semester 2025 Regulation


Skill Development Course I

3rd Semester all department. | 2025 Regulation | 3rd Semester 2025 Regulation