tristone
tristone

Reputation: 125

Why make ISA be aware of the existence of "stack" concept?

For example, x86 ISA has some specific instructions to handle stack-related operations such as push and pop, but stack can be totally implemented in the software by some memory access operations like mov, even the latter has better performance: Why use Push/Pop instead of Mov to put a number in a register in shellcode?

In ARM, push/pop are just aliases for memory operations, for reference: Push and Pop in arm

Why do we need to make the ISA aware of the existence of a stack? why don't we just make the hardware forget the "stack" and just leave it to software implementations? This would have two advantages, as far as I can see:

  1. hardware design can be simplified,
  2. give more flexibility to the software.

Can an ISA be implemented without the stack concept, i.e, without the push, pop, %rsp, %rbp, and such things?

Upvotes: 1

Views: 168

Answers (1)

puppydrum64
puppydrum64

Reputation: 1688

Why do we need to make the ISA aware of the existence of a stack? why don't we just make the hardware forget the "stack" and just leave it to software implementations?

It's not required for a CPU to work, that's for sure. There are indeed CPUs that do this, MIPS is one of them. While there is a register known as $sp, there is no hardware-enforced difference between it and the other general-purpose registers. MIPS doesn't have push or pop instructions that alter $sp implicitly. To push a register onto the stack in MIPS Assembly one would have to do it manually:

addiu $sp,$sp,-4
sw $s0,0($sp)

So why have the hardware be aware of the stack? One benefit seems to be that, on most architectures that do have a "hardware stack," the pop operation tends to be faster than loading the destination register with a value in memory- and this is the case even if you don't count the act of loading the source register with the desired address. Some assembly programmers would abuse this by temporarily relocating the stack to an array (whose values were stored in the reverse order, of course) and using pop to write each entry to a different register. Despite having some overhead with needing to disable interrupts and save the stack pointer in memory somewhere else, under very specific circumstances you could copy from memory just a little bit faster than normal, depending on how many values need to be read, what addressing modes the CPU allows, etc.

Upvotes: 2

Related Questions