Download presentation
Presentation is loading. Please wait.
Published byAshlee Franklin Modified over 9 years ago
1
Stack Machine Stack Architecture –“ Zero-address” –Processor/stack + Memory –Memory: Data and Program –“Reverse Polish notation” programming style –Stack pointer SP, the content is, Next available –Instruction set Constant Push/pop Load and store Flow of control Fetch/execution loop
2
Stack Architecture An example of a machine code: (-36)*(17+24) Machine codeStack content Const 3636 Neg-36 Const 17-36, 17 Const 24-36, 17, 24 Add-36, 41 Mult-1476
3
Example: Access to an arbitrary location in main memory with load and store: 100 + 1 Machine codeStack content Const 100100 Const 120100, 120 Load100, Const 1100,, 1 Add100, +1 storeempty
4
Basic Operators Instruction Stack content before and after Sadd a , b (a+b) Ssub Smult Sdiv Srem Sand Ssext a -> a Slow a -> a Shalt stop the machine
5
Constant, load and store InstructionStack content before and after Sc b b Push constant b : + Sl a 4 Load: SP -4; temp 4 > 4 ; > ; SP + Ss b a 4 b b Store: SP + temp > ; SP -4; >
6
Example: ADD4 120, 140, 100 Sc4(100) | destination address Sc4(120) | adress of first operand Sl4 | push Sc4(140) | adress of second operand Sl4 | push Sadd4 | add + Ss4 | store at address 100
7
Example: sum four array elements START:sc4(TEMP)| address for result sc4(DATA) sl4| load 1st operand sc4(DATA+4) sl4| load 2nd operand sadd4| add two operands sc4(DATA+8) sl4| load 3rd operand sadd4| add operands sc4(DATA+12) sl4| load 4th operand sadd4| add operands ss4| store the sum in TEMP shalt DATA:LONG(1)LONG(2)LONG(3)LONG(4) TEMP:LONG(0)| address for result
8
Jump and compare Instruction InstructionStack content before and after Scmp a b CC Arithmetic compare Sjmp a 4 Unconditional jump SP -4; PC 4 > 4 ; SjcondCC, a 4 Conditional jump SP -4; temp 4 > 4 ; SP -1; cctemp 1 > 4 ; If cond matches 1 PC 4 4 ;
9
Stack Frames and Procedure Calls Frame pointer B Stack pointer SP scall(n 2 ), number of arguments: a call to subroutine srtn: return from subroutine Slla(n 2 ): load local address, (n 2 is the offset) Slaa(n 2 ): load argument address (n 2 is the offset) salloc(n 2 ): allocate space salloc(n 2 ), n<0: deallocate space
10
Example Fibonacci int fib(n){ if (n<2) return n; else return fib(n-1) + f(n-2); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.