Sie sind auf Seite 1von 29

STACK and

BE/LE:
More Info
Prepared by: Siti Fatimah
Nor Binti Ab Wahab

STACK FROM NOTES

The stack is a special memory buffer used as a holding


area for addresses and data

However, computer do not generally provide special


memory for stack use
the programmer sets aside one or more blocks of
regular memory for the stack

Each location on the stack is pointed to by the stack


pointer
the stack pointer holds the address of the last data
element to be added to (pushed on the stack)
the last value added to the stack is also the first one
to be removed (popped from the stack)
LIFO structure (Last In, First Out)

Prepared by: Siti Fatimah Nor Binti Ab Wahab

MORE INFO ON STACK


The

stack pointer is usually a register that


contains the top of the stack.

http://
www.cs.umd.edu/class/sum2003/cmsc311/Notes/M
ips/stack.html

Prepared by: Siti Fatimah Nor Binti Ab Wahab

MORE INFO ON STACK


A

stack pointer is a smallregisterthat stores


the address of the last program request in a
stack.

http://
whatis.techtarget.com/definition/stack-pointer

Prepared by: Siti Fatimah Nor Binti Ab Wahab

MORE INFO ON STACK


The

stack pointer stores the address of the most recent


entry that was pushed onto the stack.
To push a value onto the stack, the stack pointer is
incremented to point to the next physical memory
address, and the new value is copied to that address in
memory.
To pop a value from the stack, the value is copied from
the address of the stack pointer, and the stack pointer is
decremented, pointing it to the next available item in the
stack.

http://
stackoverflow.com/questions/1464035/what-is-a-stack-pointer
-used-for-in-microprocessors
Prepared by: Siti Fatimah Nor Binti Ab Wahab

Software Stack
An area in RAM for temporary storage of data and
registers contents.
Building this stack is an inherent part of the interrupt
signal. However, stack can also be built
independently of an interrupt request. In fact, stacks
are used just about any time a subroutine is called.
The software stack is almost unlimited in size and
can reside anywhere in memory.
This implementation causes the need for a special
register in the microprocessor called the stack
pointer register.

Prepared by: Siti Fatimah Nor Binti Ab Wahab

Hardware Stack
A

number of registers set aside within the


processor to serve as the stack location.
The advantages of hardware stack are rapid
access and therefore, speed.
But the size of the stack is limited by the
number of registers that can be provided.
Restrict the flexibility of a microprocessor.

Prepared by: Siti Fatimah Nor Binti Ab Wahab

Functions of stacks
1.
2.

3.
4.

When a subroutine is called.


Stack is an excellent method for storing the
return address and arguments for subroutine
calls.
When interrupt occurs.
When stack instruction occurs

Stack building

The first plate put on the stack is at the bottom.


The last plate off the stack is the last plate placed of the
stack.
Stack operate by LIFO (Last In First Out)
The last byte put on the stack will be the first byte
retrieved from the stack, when an interrupt is completed,
for instance.

Stack operation
1.

push

push the value(byte) into the stack

Example instruction : push AX


-push the contents of AX register to the stack.

2.

pop

take out the value(byte) from the stack

Example instruction: pop AX


take out the value from the stack and put into AX register.

11

Stack Example #1
Given the segment of the program and content of registers
(address and data are given in hexadecimal) below:
PUSH AX;
PUSH BX;
PUSH CX; (A)
POPBX;
POPAX;
PUSH DX;
POPCX;
(B)
AX = 1234, BX = 5678, CX= 9ABC, DX=DEFF
stack pointer register = 10FF
Prepared by: Siti Fatimah Nor Binti Ab Wahab

12

Stack Example #1
a)

Build a stack diagram and show the content of


SP when the program execute until (A)

b)

When the program continues, build another


stack until (B)

c)

Show the content of the register: AX, BX, and CX


after the execution of the instruction is finished.
[12 Marks]

Prepared by: Siti Fatimah Nor Binti Ab Wahab

13

Stack Example #1(a) answer


a)

Build a stack diagram and show the content


of SP when the program execute until (A)
PUSH
PUSH
PUSH

AX;
BX;
CX;

(A)

AX = 1234
BX = 5678
CX = 9ABC
DX = DEFF
stack pointer register = 10FF

SP 10F9

BC

10FA

9A

10FB

78

10FC

56

10FD

34

10FE

12

10FF

XX

Prepared by: Siti Fatimah Nor Binti Ab Wahab

14

Stack Example #1(b) answer


b)

When the program continues, build another


stack until (B)
POP
POP
PUSH
POP

BX;
AX;
DX;
CX;

DX = DEFF

SP 10FD

34

10FE

12

(A)
BX = 9ABC
AX = 5678
CX = DEFF

Prepared by: Siti Fatimah Nor Binti Ab Wahab

15

Stack Example #1(c) answer


c)

Show the content of the register: AX, BX, and CX


after the execution of the instruction is finished.
AX = 5678
BX = 9ABC
CX = DEFF

Prepared by: Siti Fatimah Nor Binti Ab Wahab

16

Stack Example #2
Given
AX
BX
CX
DX
PC

=
=
=
=
=

the following set of data in registers:

1234
0BA79
1111
2B34
1003

AX

BX

CX

DX

PC

Prepared by: Siti Fatimah Nor Binti Ab Wahab

17

Stack Example #2(a)


a) Build a stack following the sequence of AX, BX, DX, PC
and CX starting from address FFFA.
(5 marks)

Prepared by: Siti Fatimah Nor Binti Ab Wahab

18

Stack Example #2(b)


b) Continue building the stack when the
following instructions are encountered.
POP AX
POP CX
POP DX

Prepared by: Siti Fatimah Nor Binti Ab Wahab

19

Stack Example #2(a) answer


a) Build a stack
following the
sequence of AX,
BX, DX, PC and CX
starting from
address FFFA.
(5 marks)

SP FFF3

03

FFF4

10

FFF5

34

FFF6

2B

FFF7

79

FFF8

BA

FFF9

34

FFFA

12

Prepared by: Siti Fatimah Nor Binti Ab Wahab

20

Stack Example #2(b)


answer
SP FFF5

34

FFF6

2B

FFF7

79

SP FFF7

79

FFF8

BA

FFF8

BA

FFF9

34

FFF9

34

SP FFF9

34

FFFA

12

FFFA

12

FFFA

12

AX =
1003

CX =
2B34

DX =
BA79
Prepared by: Siti Fatimah Nor Binti Ab Wahab

21

Big Endian vs Little Endian

A system can order its bytes from left-to-right or


right-to-left. There are two types of bytes ordering:

Big endian

Bytes are numbered from left to right


Usually for Motorola family (6800)

Little endian

Bytes are numbered from right to left


Usually for Intel family (8086)
Prepared by: Siti Fatimah Nor Binti Ab Wahab

22

Little Endian

Prepared by: Siti Fatimah Nor Binti Ab Wahab

23

Big Endian

Prepared by: Siti Fatimah Nor Binti Ab Wahab

24

Prepared by: Siti Fatimah Nor Binti Ab Wahab

25

Example 1(a)
Given

The

the following contents in register AX.


Register

Content

AX

22FF

following instruction is executed:

MOV [8002], AX

Prepared by: Siti Fatimah Nor Binti Ab Wahab

26

Example 1(a) : Big Endian


Register AX

Memory Address
8002
LO
8003
HI

HI

LO

22

FF

Content
22
FF

Prepared by: Siti Fatimah Nor Binti Ab Wahab

27

Example 1(b) : Little Endian


Register AX
HI

LO

22

FF

Memory Address
8002
LO
8003
HI

Content
FF
22

Prepared by: Siti Fatimah Nor Binti Ab Wahab

28

Final Exam Sep2014

Prepared by: Siti Fatimah Nor Binti Ab Wahab

29

Final Exam Mac2014

Prepared by: Siti Fatimah Nor Binti Ab Wahab

Das könnte Ihnen auch gefallen