Professional Documents
Culture Documents
Computer Organization and Assembly Language: Lecture 15 & 16 Stacks, Endianness Addressing Modes
Computer Organization and Assembly Language: Lecture 15 & 16 Stacks, Endianness Addressing Modes
Computer Organization and Assembly Language: Lecture 15 & 16 Stacks, Endianness Addressing Modes
Lecture 15 & 16
Stacks, Endianness
Addressing Modes
Course Instructor: Engr. Aisha Danish
Stacks
A stack is an ordered set of elements, only one of which
can be accessed at a time.
The point of access is called the top of the stack
The number of elements in the stack, or length of the
stack, is variable
The last element in the stack is the base of the stack
Items may only be added to or deleted from the top of the
stack
A stack is also known as a pushdown list or a last-in-
first-out (LIFO) list
Stacks
Stacks
A PUSH operation appends one new item to the top of
the stack
A POP operation removes the top item from the stack
Binary operators, which require two operands (e.g.,
multiply, divide, add, subtract), use the top two stack
items as operands, pop both items, and push the result
back onto the stack
Unary operations, which require only one operand
(e.g., logical NOT), use the item on the top of the stack
Stack Implementation
The stack is a useful structure to provide as part of a processor
implementation
One use, is to manage procedure calls and returns
Stacks may also be useful to the programmer
The implementation of a stack depends in part on its potential uses
If it is desired to make stack operations available to the programmer,
then the instruction set will include stack-oriented operations,
including PUSH, POP, and operations that use the top one or two
stack elements as operands
Because all of these operations refer to a unique location, namely the
top of the stack, the address of the operand or operands is implicit and
need not be included in the instruction. These are the zeroaddress
instructions
Stack Implementation
Implementation of a stack requires some set of locations to be used to store the stack elements
A contiguous block of locations is reserved in main memory (or virtual memory) for the stack
Most of the time, the block is partially filled with stack elements and the remainder is available
registers:
Stack pointer (SP)
Contains the address of the top of the stack.
If an item is appended to or deleted from the stack, the pointer is incremented or decremented
to contain the address of the new top of the stack.
Stack base
Contains the address of the bottom location in the reserved block. If an attempt is made to
POP when the stack is empty, an error is reported.
Stack limit
Contains the address of the other end of the reserved block. If an attempt is made to PUSH
when the block is fully utilized for the stack, an error is reported.
Stack Implementation
Stack Implementation
Stack implementations have two key attributes:
Ascending/descending
Ascending stack grows in the direction of ascending addresses, starting from a
low address and progressing to a higher address
SP is incremented when items are pushed
Decremented when items are pulled
A descending stack grows in the direction of descending addresses, starting
from a high address and progressing to a lower one
Most machines implement descending stacks as a default
Stack Implementation
Full/empty
A misleading terminology, because is does not refer to whether the
method), or the next free space on the stack (an empty method)
For the full method, when the stack is completely full, the SP
points to the upper limit of the stack. For the empty method, when
the stack is completely empty, the SP points to the base of the stack
The ARM architecture allows the system programmer to specify the
use of ascending or descending, empty or full stack operations
The x86 architecture uses a descending/empty convention
LITTLE- , BIG- ,AND BI-
ENDIAN
Byte Ordering
The concept of endianness was first discussed in the literature by Cohen
[COHE81]
Suppose we have the 32-bit hexadecimal value 12345678
It is stored in a 32-bit word in byte-addressable memory at byte location
184
The value consists of 4 bytes, with the least significant byte containing the
value 78
The most significant byte containing the value 12.
There are two obvious ways to store this value:
LITTLE- , BIG- ,AND BI-
ENDIAN
Big-Endian
The mapping that stores the most significant byte
Instruction
Opcode Operand
Direct Addressing
Address field contains address of operand
Effective address (EA) = address field (A)
e.g. ADD A
Add contents of cell A to accumulator
Look in memory at address A for operand
Single memory reference to access data
No additional calculations to work out effective
address
Limited address space
Direct Addressing Diagram
Instruction
Opcode Address A
Memory
Operand
Indirect Addressing (1)
Memory cell pointed to by address field contains
the address of (pointer to) the operand
EA = (A)
Look in A, find address (A) and look there for operand
e.g. ADD (A)
Add contents of cell pointed to by contents of A to
accumulator
Indirect Addressing (2)
Large address space
May be nested, multilevel, cascaded
e.g. EA = (((A)))
Draw the diagram yourself
Multiple memory accesses to find operand
Hence slower
Indirect Addressing Diagram
Instruction
Opcode Address A
Memory
Pointer to operand
Operand
Register Addressing (1)
Operand is held in register named in address filed
EA = R
Limited number of registers
Very small address field needed
Shorter instructions
Faster instruction fetch
Register Addressing (2)
No memory access
Very fast execution
Very limited address space
Multiple registers helps performance
Requires good assembly programming or compiler
writing
Register Addressing Diagram
Instruction
Operand
Register Indirect Addressing
EA = (R)
Operand is in memory cell pointed to by contents
of register R
Large address space
One fewer memory access than indirect addressing
Register Indirect Addressing Diagram
Instruction
Registers
Base-register addressing
Indexing
Displacement Addressing
EA = A + (R)
Address field hold two values
A = base value
R = register that holds displacement
or vice versa
Displacement Addressing Diagram
Instruction
Registers
EA = A + (R)
(R) <--- (R) + 1
Indexing
A = base
R = displacement
EA = A + R
Good for accessing arrays
EA = A + R
R++
Indexing
Postindex
If indexing is performed after the indirection, it is termed
postindexing:
EA = (A) + (R)
Preindex
If indexing is performed before the indirection, it is termed
preindexing:
EA = (A+(R))