Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Instruction Cycle and Call Stack in Computer Science, Study notes of Logic

The instruction cycle in computer processing, which involves fetching instructions from memory, decoding them, executing them, and handling interrupts. It also covers Python control structures, such as FOR and WHILE loops, and the concept of a call stack in function calls. call types, including call by value and call by reference, and recursive functions.

Typology: Study notes

2021/2022

Uploaded on 09/27/2022

melanycox
melanycox 🇬🇧

5

(8)

227 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Instruction Cycle
1. An instruction is fetched from the memory address
stored in the program counter (PC) and is stored in the
instruction register (IR). The PC is incremented.
2. The decoder interprets the instruction. If it is a jump,
the PC is reset and the cycle ends. Otherwise, any
required data is fetched from main memory and is
placed in data registers.
3. The CPU executes the instruction by reading values
from registers, performing arithmetic or logical
functions on them, and writing the result into a register.
4. The result is stored in main memory or is sent to an
output device.
5. Interrupts are handled.
p. 1/9
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Instruction Cycle and Call Stack in Computer Science and more Study notes Logic in PDF only on Docsity!

Instruction Cycle

  1. An instruction is fetched from the memory address

stored in the program counter (PC) and is stored in theinstruction register (IR). The PC is incremented.

  1. The decoder interprets the instruction. If it is a jump,

the PC is reset and the cycle ends. Otherwise, anyrequired data is fetched from main memory and isplaced in data registers.

  1. The CPU executes the instruction by reading values

from registers, performing arithmetic or logicalfunctions on them, and writing the result into a register.

  1. The result is stored in main memory or is sent to an

output device.

  1. Interrupts are handled.

Instruction Cycle

Call Stack

Function calls are coordinated using a

call stack

Each function call generates a

stack frame

that stores

its arguments, local variables, space for return values,and a return address. When

f

calls

g

, a

g

-frame is initialized and control is

transferred to the first instruction of

g

When

g^

returns, execution of

f

resumes at the

g

return

address. Nested function calls are handled by processing stackframes in first-in first-out order (FIFO).

Call Stack

drawline

arguments local variables^ return value return address

drawbox

arguments local variables^ return value return address

Recursive Functions

A^

recursive

function contains calls to itself in its body.

These calls are regular function calls. The programmer must ensure that the calls terminate. Example: compute

1 + 2 +

· · ·

n

def sum (n):

if

n == 1:

return

return

n +

sum(n - 1)

Base case

of

n

= 1

has no recursion.

Recursive calls terminate because

n

decreases.

Call Stack

sum(1)

local variable:

n

= 1

return value: 1

return address:

<

sum(2)

sum(2)

local variable:

n

= 2

return value:

2 + sum(1)

return address:

<

sum(3)

sum(3)

local variable:

n

= 3

return value:

3 + sum(2)

return address:

<

sum(4)

sum(4)

local variable:

n

= 4

return value:

4 + sum(3)

return address:

<

main