Microcontroller Simulator Task 5 - Electronics Tutorials |
|
The stack is an area of memory used to store the return addresses of subroutine calls. The stack pointer is a register which points to the current stack position. Data is added and removed from the stack in a strict Last In First Out (LIFO) order. The stack pointer is adjusted to keep track of these operations. Imagine a tall pile of dinner plates. It is easy to add to the top of the pile. Also it's easy to remove from the top of the pile. Removing a middle or bottom plate is not a good idea. The plate pile is a stack and obeys the LIFO rule. When data is added to the stack this is called a PUSH. When data is removed from the stack it's called a POP. If you write bad code, the stack can grow so big that it "eats" your program. That would be a stack overflow. If you write your code correctly, each CALL has a matching RETurn. If you have more calls than returns, your stack will grow and eat your program. If you have more returns than calls, you will get a Stack Underflow. This might not destroy your program but something is going to break. The Task ... Open a browser window or tab containing the microcontroller simulator. Copy and paste this program into the code editing area. Press F8 to assemble the code. Click the TRAFFIC button on the highlighted "C" to connect the traffic Lights to PORTC. Step the program and watch SP and the Stack. Answer the reviseOmatic questions on this task.
; ===========================================
; TRAFFIC LIGHTS WITH TIME DELAY SUBROUTINE
; ===========================================
MOVW 0X00 ; 0: Output 1: Input
MOVWR TRISC ; PORTC 8 INPUTS
START:
MOVW 0X21 ; GARxxRAG - 00100001
MOVWR PORTC ; X X
MOVW 0X20 ; DELAY DURATION
CALL DELAY
MOVW 0X62 ; GARxxRAG - 01100010
MOVWR PORTC ; XX X
MOVW 0X05 ; DELAY DURATION
CALL DELAY
MOVW 0X84 ; GARxxRAG - 10000100
MOVWR PORTC ; X X
MOVW 0X20 ; DELAY DURATION
CALL DELAY
MOVW 0X46 ; GARxxRAG - 01000110
MOVWR PORTC ; X XX
MOVW 0X05 ; DELAY DURATION
CALL DELAY
JMP START
; ===========================================
; ===== TIME DELAY SUBROUTINE ===============
; ===========================================
DELAY:
SUBW 0X1 ; SUBTRACT ONE FROM W
JPZ DONE ; JUMP IF Z FLAG IS SET
JMP DELAY ; CARRY ON COUNTING
DONE:
RET ; SUBROUTINE RETURN
; ===========================================
|