stack new edit

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

Upload: maiyonis

Post on 12-Sep-2015

236 views

Category:

Documents


6 download

DESCRIPTION

CSC159

TRANSCRIPT

STACK MORE INFO

STACK and BE/LE:More InfoPrepared by: Siti Fatimah Nor Binti Ab WahabSTACK FROM NOTESThe 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 Wahab2MORE INFO ON STACKThe stack pointer is usually a register that contains the top of the stack.http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Mips/stack.html3Prepared by: Siti Fatimah Nor Binti Ab WahabMORE INFO ON STACKA stack pointer is a smallregisterthat stores the address of the last program request in a stack.http://whatis.techtarget.com/definition/stack-pointer

4Prepared by: Siti Fatimah Nor Binti Ab WahabMORE INFO ON STACKThe 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

5Prepared by: Siti Fatimah Nor Binti Ab WahabSoftware StackAn 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 Wahab6Hardware StackA 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 Wahab7Functions of stacksWhen 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 occursStack 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 operation1.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 AXtake out the value from the stack and put into AX register.

Stack Example #1Given the segment of the program and content of registers (address and data are given in hexadecimal) below:PUSHAX;PUSHBX; PUSHCX; (A)POPBX;POPAX;PUSHDX; POPCX; (B)

AX = 1234, BX = 5678, CX= 9ABC, DX=DEFF stack pointer register = 10FF11Prepared by: Siti Fatimah Nor Binti Ab WahabStack Example #1Build a stack diagram and show the content of SP when the program execute until (A)

When the program continues, build another stack until (B)

Show the content of the register: AX, BX, and CX after the execution of the instruction is finished.[12 Marks]12Prepared by: Siti Fatimah Nor Binti Ab WahabStack Example #1(a) answerSP10F9BC10FA9A10FB7810FC5610FD3410FE1210FFXXPUSHAX;PUSHBX; PUSHCX; (A)

AX = 1234 BX = 5678 CX = 9ABCDX = DEFFstack pointer register = 10FFBuild a stack diagram and show the content of SP when the program execute until (A)13Prepared by: Siti Fatimah Nor Binti Ab WahabStack Example #1(b) answerSP10FD3410FE12BX = 9ABCAX = 5678CX = DEFFPOPBX;POPAX; PUSHDX;POP CX; (A)

DX = DEFFWhen the program continues, build another stack until (B)14Prepared by: Siti Fatimah Nor Binti Ab WahabStack Example #1(c) answerShow the content of the register: AX, BX, and CX after the execution of the instruction is finished.AX = 5678BX = 9ABCCX = DEFF15Prepared by: Siti Fatimah Nor Binti Ab WahabStack Example #2Given the following set of data in registers:AX = 1234BX = 0BA79CX = 1111DX = 2B34PC = 1003XXXAXBXCXDXPC0001101116Prepared by: Siti Fatimah Nor Binti Ab WahabStack Example #2(a) Build a stack following the sequence of AX, BX, DX, PC and CX starting from address FFFA.(5 marks)

17Prepared by: Siti Fatimah Nor Binti Ab Wahab17Stack Example #2(b)Continue building the stack when the following instructions are encountered.POP AXPOP CXPOP DX

18Prepared by: Siti Fatimah Nor Binti Ab WahabStack Example #2(a) answerSPFFF303FFF410FFF534FFF62BFFF779FFF8BAFFF934FFFA12Build a stack following the sequence of AX, BX, DX, PC and CX starting from address FFFA.(5 marks)19Prepared by: Siti Fatimah Nor Binti Ab WahabStack Example #2(b) answerSPFFF534FFF62BFFF779FFF8BAFFF934FFFA12AX = 1003CX = 2B34DX = BA79SPFFF934FFFA12SPFFF779FFF8BAFFF934FFFA1220Prepared by: Siti Fatimah Nor Binti Ab WahabBig Endian vs Little EndianA system can order its bytes from left-to-right or right-to-left. There are two types of bytes ordering:

Big endianBytes are numbered from left to rightUsually for Motorola family (6800)

Little endianBytes are numbered from right to leftUsually for Intel family (8086)

21Prepared by: Siti Fatimah Nor Binti Ab WahabLittle Endian

Prepared by: Siti Fatimah Nor Binti Ab Wahab22Big Endian

Prepared by: Siti Fatimah Nor Binti Ab Wahab23

Prepared by: Siti Fatimah Nor Binti Ab Wahab24Example 1(a)Given the following contents in register AX.

The following instruction is executed:MOV [8002], AXRegisterContentAX22FF25Prepared by: Siti Fatimah Nor Binti Ab WahabExample 1(a) : Big EndianRegister AXHILO22FFMemory AddressContent8002LO228003HIFF26Prepared by: Siti Fatimah Nor Binti Ab WahabExample 1(b) : Little EndianRegister AXHILO22FFMemory AddressContent8002LOFF8003HI2227Prepared by: Siti Fatimah Nor Binti Ab WahabFinal Exam Sep2014Prepared by: Siti Fatimah Nor Binti Ab Wahab28

Final Exam Mac2014

Prepared by: Siti Fatimah Nor Binti Ab Wahab29