0% found this document useful (0 votes)
136 views56 pages

Week 5

1. Parameters are pushed onto the stack before calling the subroutine 2. Within the subroutine, parameters are accessed from the stack 3. Returning values are placed on the stack 4. The stack pointer is adjusted to remove parameters and returning values
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
136 views56 pages

Week 5

1. Parameters are pushed onto the stack before calling the subroutine 2. Within the subroutine, parameters are accessed from the stack 3. Returning values are placed on the stack 4. The stack pointer is adjusted to remove parameters and returning values
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

Stack

Bindu Agarwalla

1
STACK

0
.

SP=1980 -12 Current top element


1984 23
1988 88
Stack
1992
1996
BOTTOM: 2000 56 Bottom element
.

2k -1
STACK [Assumption: The machine is byte addressable
and each element in the stack occupies 4Bytes]
Push Operation 0
• SP always points to the top element, so .
to push a newitem, first, it has to be
updated to the location, where a new
element can be pushed. SP=1976 19 Current top
• As stack grows downward in the 1980 -12 element
memory, so first SP is decremented,
then the NEWITEM is pushed. 1984 23

Note: This rule is applicable for pushing 1988 88 Stack


any (all) element onto the stack. 1992
Substract #4, SP 1996
Move NEWITEM, (SP) 2000 56 Bottom
↓ element
.
Move NEWITEM, -(SP)

2k -1
19 NEWITEM
STACK [Assumption: The machine is byte addressable
and each element in the stack occupies 4Bytes]
Push Operation 0
• If stack stack starts in memory from .
2000, then the initial value of SP
should be 2004, then only the 1st
element will be pushed at correct SP=1976 19 Current top
location. element
1980 -12
1984 23
1988 88 Stack
1992
1996
2000 56 Bottom
element
.

2k -1
19 NEWITEM
STACK [Assumption: The machine is byte addressable
and each element in the stack occupies 4Bytes]
Pop Operation 0
• SP always points to the top element, so .
to pop an element, first, it has to be
popped off, then the SP need to be
updated to the next top element.
• As stack grows downward in the SP=1980 -12 Current top
memory, so SP is incremented, after element
the top item is popped off. 1984 23
1988 88 Stack
Move (SP), ITEM 1992
ADD #4,SP 1996
↓ 2000 56 Bottom
Move (SP)+, ITEM element
.

2k -1
-12 ITEM
Limitation of Link Register
and use of stack for subroutine
Linkage

Bindu Agarwalla

6
STACK[Assumption: the stack is from 2000 to 1500]
SafePush Operation
In a full stack, push operation should not be
SAFEPUSH Compare #1500, SP done. So, if the value of SP is 1500 or less than
1500, that means already the stack is full. Hence,
Branch <= 0 FULLERROR compare operation gives either 0 or a negative
value after the operation. If SP is 1500, then the
Move NEWITEM, -(SP) next push operation will be done at 1496, which
is not the part of stack.
SafePop Operation

SAFEPOP Compare #2000, SP


From a empty stack, no element should be
Branch > 0 EMPTYERROR popped out. So, if the value of SP is greater than
2000, that means already the stack is empty.
Move (SP)+, ITEM Hence, compare operation gives a positive value
after the operation.
SUBROUTINES
The way in which a computer makes it possible to call and return from
subroutines is referred to as subroutine linkage method.
Linkage using Link Register
On Call

1. Store the contents of the PC in the link register.

2. Branch to the target address specified by the instruction

On returning from a subroutine


Branch to the address contained in the link register
Subroutine Linkage using link register

1000 CALL Sum 2000 First Instruction


1004 Next Instruction

Return
2000

PC 1004

Link 1004

Call Return

Limitation??
No support for Nesting of Functions.
Limitation of Link Register
and use of stack for subroutine
Linkage

Bindu Agarwalla

10
Limitation of Link Register
Nesting of functions/subroutines is not supported.
Solution
Using Stack: To support nesting of functions.

Using Stack: Calling a function and returning from it.

Assumptions:
Parameters are passed through general purpose registers.
Returning values through general purpose registers.
Example:

We are going to add N numbers stored in consecutive memory locations starting from
the symbolic address NUM1 using a function. the function is going to return the
summation result to the caller.
Limitation of Link Register
Nesting of functions/subroutines is not supported.
Stack as Subroutine Linkage Method
Calling program

1000 Move N, R1

1004 Move #Num1, R2


1008 Call LISTADD
1012 Move R0, SUM
.
. SP =1996 1012
.
SP =2000 14 14
Subroutine
LISTADD Clear R0
LOOP Add (R2)+, R0
Stack
Decrement R1
Branch > 0 LOOP
Return
Stack as Subroutine Linkage Method
Calling program

1000 Move N, R1

1004 Move #Num1, R2


1008 Call LISTADD
1012 Move R0, SUM
.
. SP =1996 1012
.
SP =2000 14 14
Subroutine
LISTADD Clear R0
LOOP Add (R2)+, R0
Stack
Decrement R1
Branch > 0 LOOP
Return
Stack as Subroutine Linkage Method [Parameter
Passing and returning value using stack
When a large numbers of parameters are required to pass to a function, we may not
have that many general purpose registers.

Solution:

Parameters are passed onto the stack, before calling the function.

Returning values through the stack.


Stack as Subroutine Linkage Method[Parameter Passing and
returning value using stack
Calling program

1000 Move #NUM1, -(SP)


1004 Move N, -(SP)
1008 Call LISTADD 1992 1012 (Ret Addr)

1012 Move 4(SP), SUM 1996 10 (Counter)


2000 3000 (Base Addr)
1016 ADD #8, SP
2004 28
14
Stack as Subroutine Linkage Method[Parameter
Passing and returning value using stack
Subroutine
LISTADD MoveMultiple R0-R2, -(SP)

MOVE 16(SP), R1
MOVE 20(SP), R2 SP=1980 [R2]
CLEAR R0 1984 [R1]

LOOP Add (R2)+, R0 1988 [R0]


1992 1012
Decrement R1
1996 1410
Branch > 0 LOOP
2000 3000
2004 28
Stack as Subroutine Linkage Method[Parameter
Passing and returning value using stack

LISTADD MoveMultiple R0-R2, -(SP)

MOVE 16(SP), R1
MOVE 20(SP), R2 1980 [R2]
CLEAR R0 1984 [R1]

LOOP Add (R2)+, R0 1988 [R0]


1992 1012
Decrement R1
1996 1410
Branch > 0 LOOP
2000 RESULT OF
Move R0, 20(SP)
SUMMATION
MoveMultiple (SP)+, R0-R2
2004 28
Stack as Subroutine Linkage Method[Parameter
Passing and returning value using stack

LISTADD MoveMultiple R0-R2, -(SP)

MOVE 16(SP), R1
MOVE 20(SP), R2
CLEAR R0
LOOP Add (R2)+, R0
1992 1012
Decrement R1
1996 14 10
Branch > 0 LOOP
Move R0, 20(SP) 2000 RESULT OF
SUMMATION
MoveMultiple (SP)+, R0-R2
RETURN 2004 28
Stack as Subroutine Linkage Method[Parameter
Passing and returning value using stack

LISTADD MoveMultiple R0-R2, -(SP)

MOVE 16(SP), R1
MOVE 20(SP), R2
CLEAR R0
LOOP Add (R2)+, R0
Decrement R1
1996 10
14
Branch > 0 LOOP
2000 Result of summation
MoveMultiple (SP)+, R0-R2
RETURN 2004 28
Stack as Subroutine Linkage Method[Parameter
Passing and returning value using stack]
Calling program

1000 Move #NUM1, -(SP)


1004 Move N, -(SP)
1008 Call LISTADD
1012 Move 4(SP), SUM 1996 10
2000 Result of Summation
1016 ADD #8, SP
2004 28
14
Numerical
The content of the top of memory stack is 2452.The content of SP is
1258. A two byte call subroutine instruction is located in memory
address 1456 followed by address field of 5490 at location 1457.What
are the content of PC , SP and top of stack;
1.Before call instruction execution
2.After call instruction execution
3.After return from subroutine
Numerical
How many times a subroutine should be called so that the stack becomes full, Assume
that the stack address space ranges from 2000 to 1600 and each stack word consumes 4
bytes and machine is byte addressable.[Note: No parameter, return value, registers,
local variables are stored in the stack due to subroutine call]
Numerical
Given the following program fragment
Main Program First Subroutine SUB1 Second Subroutine SUB2
2000 ADD R1, R2 3000 MOV R1,R2 4000 SUB R6, R1
2004 XOR R3, R4 3004 ADD R5, R1 4008 XOR R1, R5
2008 CALL SUB1 3008 CALL SUB2 4012 RETURN
2012 SUB R4, R5 3012 RETURN

Initially the stack pointer SP contains 5000.


What are the content of PC, SP, and the top of the stack?
i) After the subroutine call instruction is executed in the main program?
ii) After the subroutine call instruction is executed in the subroutine SUB1?
iii) After the return from SUB2 subroutine?
Numerical
Numerical
The content of the top of the memory stack is 5000. The content of the stack
pointer SP is 3000. Assume you want to organize a nested subroutine calls on a
computer as follows:
The routine Main calls a subroutine SUB1 by executing a two-word call subroutine
instruction located in memory at address 1000 followed by the address field of 6000 at
location 1001.Again subroutine SUB1 calls another subroutine SUB2 by executing a
two-word call subroutine instruction located in memory at address 6050 followed by the
address field of 8000 at location 6051 . What are the content of PC, SP, and the top of
the stack?
i) After the subroutine call instruction is executed in the main routine?
ii) After the subroutine call instruction is executed in the subroutine SUB1?
iii) After the return from SUB2 subroutine?
Numerical
Given the following program fragment
Main Program First Subroutine SUB1 Second Subroutine SUB2
6000 8000 1st Inst
1000 CALL SUB1(6000) 6050 CALL SUB2(8000)
1002 Next Inst 6052 Next Inst

6080 Return 8080 Return


Initially the stack pointer SP contains 5000.
Basic Performance Equation

29
Numerical 2
Suppose a program (or a program task) takes 1 billion instructions to execute on a
processor running at 2 GHz. Suppose also that 50% of the instructions execute in 3 clock
cycles, 30% execute in 4 clock cycles, and 20% execute in 5 clock cycles. What is the
execution time for the program or task?

Solution
Total Number instructions=N=109

Value Frequency Product


3 0.5 1.5
4 0.3 1.2
5 0.2 1.0

S=CPI=3.7

R = 2GHz
So, T= (S x N)/R
= (3.7 x 109)/(2 x 109) sec= 1.85 sec
Basic Performance Equation
The basic performance equation, which is fundamental to measuring
computer performance, measures the CPU time, is as follows.
CPU Time To execute a program = T= time/program

Time required to execute a basic step = P = time/cycle


Average number of basic steps required to execute one machine instruction =
CPI = S = cycles/instruction

Number of instructions a program contains is N = instructions/program

Time/Program= Time/Cycle x Cycles/Instruction x Instructions/Program

CPU Time = P x CPI x N


T= P x S x N
T= (S x N)/R

Where R(Clock Rate)


Numerical
If a 8GHz computer takes 7 clock cycles for ALU instructions, 11 clock cycles for
branch instructions and 6 clock cycles for data transfer instructions. Then Find the total
time taken by the computer to execute the program that consists of 10 ALU instructions,
5 branch instructions and 5 data transfer instructions.

Solution
Total Number instructions=N=10+5+5=20

Total cycles=7*10+11*5+6*5=70+55+30=155

S=CPI=155/20
R = 8GHz

So, T= (S x N)/R
= (155/20) x 20)/(8 x 109) sec= 19.375 x10-9 sec = 19.375 nsec.
Numerical 2
A program is running on an 8MHz processor. Assume that 50% instructions perform an
ALU operation, 30% instructions perform memory operation and 20% instructions
perform Branching. Further assume that instructions performing an ALU operation take 4
clock cycles, instructions performing memory operation take 9 clock cycles and
instructions performing Branching take 7clock cycles. What is the total time taken by the
program?
Solution
Total Number instructions=N=100

Value Frequency Product


4 0.5 2.0
9 0.3 2.7
7 0.2 1.4

S=CPI=6.1

R = 8MHz
So, T= (S x N)/R
= (6.1 x 102)/(8 x 106) sec= 76.25 μ sec
Numerical 2
A program is running on an 8MHz processor. Assume that 50% instructions perform an
ALU operation, 30% instructions perform memory operation and 20% instructions
perform Branching. Further assume that instructions performing an ALU operation take 4
clock cycles, instructions performing memory operation take 9 clock cycles and
instructions performing Branching take 7clock cycles. What is the total time taken by the
program?
Solution
Total Number instructions=N=100

Value Frequency Product


4 0.5 2.0
9 0.3 2.7
7 0.2 1.4

S=CPI=6.1

R = 8MHz
So, T= (S x N)/R
= (6.1 x 102)/(8 x 106) sec= 76.25 μ sec
Big Endian and
Little Endian Representation

35
Big Endian and Little Endian Concept
Little and big endian are two ways of storing multibyte data-types ( int, float,
etc). For Single Byte no ordering is required.
In byte ordering, the "big end" byte is called the "high-order byte" or the
"most significant byte".
The term ‘endian’ as derived from ‘end’ may lead to confusion. The end
denotes which end of the number comes first rather than which part comes at
the end of the sequence of bytes.
The basic endian layout can be seen in the table below:

Endianness First Byte Last Byte


Big Most Significant Least Significant
Little Least Significant Most Significant
Big Endian and Little Endian Concept
Big Endian Byte Order Little Endian Byte Order
The most significant byte (the "big end") The least significant byte (the "little end")
of the data is placed at the byte with the of the data is placed at the byte with the
lowest address. The rest of the data is lowest address. The rest of the data is
placed in order in the next bytes in placed in order in the next bytes in
memory. memory.
Example: Motorola Computer/Machine Example: Intel 80x86 Computer/Machine
Big Endian and Little Endian Concept
Example: Represent the integer 0x01234567 (represented in hexa) in Big
Endian and Littel Endian machines.
Answer:

0x100 0x101 0x102 0x103


01 23 45 67
BIG ENDIAN

0x100 0x101 0x102 0x103


67 45 23 01
LITTLE ENDIAN
Big Endian and Little Endian Concept
Example: Represent the integer 0x01234567 (represented in hexa) in Big
Endian and Littel Endian machines.
Answer:

0x100 0x101 0x102 0x103


01 23 45 67
BIG ENDIAN

0x100 0x101 0x102 0x103


67 45 23 01
LITTLE ENDIAN
Examples On
Big Endian and Little Endian Representation

40
Big Endian and Little Endian Concept
Example: Represent the integer 14342 in Big Endian and Little Endian
machines.

Answer: 1. Convert the no into binary : (32bits)


14342= (0000 0000 0000 0000 0011 1000 0000 0110)2

Answer: 2. Write the hexadecimal equivalent no for the binary combination


obtained in the step no1.
14342= (0000 0000 0000 0000 0011 1000 0000 0110)2 = 00 00 38 06H

0x100 0x101 0x102 0x103


06 38 00 00
LITTLE ENDIAN

0x100 0x101 0x102 0x103


00 00 38 06
BIG ENDIAN
Big Endian and Little Endian Concept
Example: Represent the integer -14342 in Big Endian and Little Endian
machines.
Answer: 1. Convert the no into binary : (32bits)
14342= (0000 0000 0000 0000 0011 1000 0000 0110)2
Answer: 1. Then take the 2’s complement or the binary combination obtained in the
step no1.
-14342= (1111 1111 1111 1111 1100 0111 1111 1010)2
Answer: 2. Write the hexadecimal equivalent no for the binary combination obtained in
the step no 2.
-14342= (1111 1111 1111 1111 1100 0111 1111 1010)2 = FF FF C7 FAH
0x100 0x101 0x102 0x103
FA C7 FF FF
LITTLE ENDIAN

0x100 0x101 0x102 0x103


FF FF C7 FA
BIG ENDIAN
Big Endian and Little Endian Concept
Example: Represent the long integer a=14342 in Big Endian and Little Endian
machines.

Answer: 1. Convert the no into binary : (64bits)


14342=
(0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0011 1000 0000 0110)2
Answer: 2. Write the hexadecimal equivalent no for the binary combination
obtained in the step no1.
14342= 00 00 00 00 00 00 38 06H

0x100 0x101 0x102 0x103 0x104 0x105 0x106 0x107


06 38 00 00 00 00 00 00
LITTLE ENDIAN
0x100 0x101 0x10 0x103 0x104 0x105 0x106 0x107

00 00 00 00 00 00 38 06
BIG ENDIAN
GATE Question On
Big Endian and Little Endian Representation

44
GATE 2021
If the numerical value of a 2-byte unsigned integer on a little endian computer
is 255 more than that on a big endian computer, which of the following
choices represent(s) the unsigned integer on a little endian computer?
a.0x6665
0x6566
b. 0x0001 0x00FF
c. 0x4243 0x6665
d. 0x0100
So, option a is correct
Solution: Let’s take the option a.
Little Endian= 0x6665
So, the original number= 0x6566
So, the big endian= 0x6566 , little endian= 0x6665
Given , Little endian=255 + Big endian
255= 1111 1111 =0xFF
GATE 2021
If the numerical value of a 2-byte unsigned integer on a little endian computer
is 255 more than that on a big endian computer, which of the following
choices represent(s) the unsigned integer on a little endian computer?
a.0x6665
0x0100
b. 0x0001 0x00FF
c. 0x4243 0x01FF
d. 0x0100

Solution: Let’s take the option b. So, option B is incorrect


Little Endian= 0x0001
So, the original number= 0x0100
So, the big endian= 0x0100 , little endian= 0x0001
Given , Little endian=255 + Big endian
255= 1111 1111 =0xFF
GATE 2021
If the numerical value of a 2-byte unsigned integer on a little endian computer
is 255 more than that on a big endian computer, which of the following
choices represent(s) the unsigned integer on a little endian computer?
a.0x6665
0x4342
b. 0x0001 0x00FF
c. 0x4243 0x4441
d. 0x0100

Solution: Let’s take the option c. So, option C is incorrect


Little Endian= 0x4243
So, the original number= 0x4342
So, the big endian= 0x4342 , little endian= 0x4243
Given , Little endian=255 + Big endian
255= 1111 1111 =0xFF
GATE 2021
If the numerical value of a 2-byte unsigned integer on a little endian computer
is 255 more than that on a big endian computer, which of the following
choices represent(s) the unsigned integer on a little endian computer?
a.0x6665
0x0001
b. 0x0001 0x00FF
c. 0x4243 0x0100
d. 0x0100

Solution: Let’s take the option c. So, option D is correct


Little Endian= 0x0100
So, the original number= 0x0001
So, the big endian= 0x0001 , little endian= 0x0100
Given , Little endian=255 + Big endian
So, option Aand D both
255= 1111 1111 =0xFF
are correct.
GATE QUESTIONS ON INSTRUCTION
FORMATS
Bindu Agarwalla

49
GATE 2018
A processor has 16 integer registers (R0, R1, … , R15) and 64 floating point
registers (F0, F1, … , F63). It uses a 2-byte instruction format. There are four
categories of instructions: Type-1, Type-2, Type-3, and Type 4. Type-1
category consists of four instructions, each with 3 integer register operands
(3Rs). Type-2 category consists of eight instructions, each with 2 floating
point register operands (2Fs). Type-3 category consists of fourteen
instructions, each with one integer register operand and one floating point
register operand (1R+1F). Type-4 category consists of N instructions, each
with a floating point register operand (1F).
(A) 32
(B) 64
(C) 256
(D) 512
GATE 2018
# of integer registers= 16, i.e., 4(24) bits are required to represent a integer
register.
# of fp registers= 64, i.e., 6(26) bits are required to represent a fp register.

4 4 4 4
int reg int reg
Type-1

4 6 6
fp reg fp reg

Type-2

6 4 6 Type-3
int reg fp reg

10 6 Type - 4
fp reg
GATE 2018
No of Type-1 instructions is : 4 x 212

No of Type- 2 instructions is : 8 x 212

No of Type- 3 instructions is : 14 x 210

Total no of instructions is 216

No of Type- 4 instructions is : 216 - [ 22 x 212+ 23 x 212+14 x 210 ]


216 - [ 211 x ( 23 + 24+7) ]
= 216 - [ 211 x 31)]
=211(25-31)=211

Given, N x 26 = 211
N= 211/26 =25
N=32
GATE 2020
A processor has 64 registers and uses 16-bit instruction format. It has two types
of instructions: I-type and R-type. Each I-type instruction contains an opcode, a
register name, and a 4-bit immediate value. Each R-type instruction contains an
opcode and two register names. If there are 8 distinct I-type opcodes, then the
maximum number of distinct R-type opcodes is _____.
# of registers= 64, i.e., 6(26) bits are required to represent a register.

6 6 4 I-Type
Reg Imm

4 6 6 R - Type
Reg Reg
Let, N no of R-Type opcodes are possible
N x 212= 216- 8 x 26 x 24
N x 212= 216- 23 x 26 x 24
N = 24- 21 =14
Numerical 3
The content of register R1is 10110011. What will be the decimal value after execution
of following instruction. [Assume the number is represented in 2's complement format]
AShiftL #2, R1

0 1 0 1 1 0 0 1 1 R1

AShiftL#2, R1

1 0 1 1 0 0 1 1 0 R1

0 1 1 0 0 1 1 0 0 R1

Here, R1 contains -77 before the shift operation, and after the AshiftL 2 times,
it contains -11
Numerical 4
Execute the following instruction where Ro is of 8 bits and its content is
11001011.
i)Lshift L #2, Ro ii) Ashift R #1, Ro

0 1 1 0 0 1 0 1 1 R0

LshiftL#2, R0

1 1 0 0 1 0 1 1 0 R0

1 0 0 1 0 1 1 0 0 R0

Here, R0 contains -43 before the shift operation, and after the AshiftR 2 times,
it contains -11
Thank You

You might also like