Anda di halaman 1dari 1

EED 3018 LABORATORY

EXPERIMENT 7:

STACK and SUBROUTINES

Preliminary Work
Read pages 12-14 (Stack and Subroutines section) in the Laboratory programming guide.
Exercise : Write a subroutine to be named READSW (this is the label of the first line of
the subroutine) as defined below.

READSW

subroutine name: READSW


passing input parameter: in register C
passing output parameter: in register C
registers changed: the accumulator (dont save it onto stack )
READSW reads port A (I/O address=00H) and checks the position of a switch
at the LSB of the port: register C is
incremented if the LSB is logic 1, or
decremented if the LSB is logic 0.

Laboratory Work
Create a PIO (paralel input/output device for Z80) with two ports, Port A and Port B in
the simulator using virtual machine: ViewVirtual Machine PIO
Set Port A in input mode (click the button on the right to select Mode: Input) and Port
B in output mode. Note that Port B already comes in output mode when the PIO
window opens. Check the Set box in Address Decoder section as shown in the figure.
This configuration sets I/O addresses 00H for Port A and 01H for Port B, respectively.
Connect an input line (switches) to Port A and a seven segment display (SSD) to Port B
(see the figure).
The task is to design an up/down counter together with its entire program code. The
switch connected to to the LSB of Port A of the PIO controls the direction of counting.
If logic 0 is input by the switch, down-counter mode is selected, otherwise, up-counter
is selected.
Write the main program (on the back of this page) as follows:
initialize SP to 0074H and clear register C. Then, in an infinite loop repeat: call
subroutine READSW, logically AND register C by 7 to restrict maximum value, and
finally call subroutine DISP (given below) which displays a digit in register C on SSD.
(a) Test the up/down counter by changing the switch position (press Set button after
every change of the switch in Line Input window) and observing digits on the SSD.
Execute your program at the maximm speed in the simulator.
(b) Then, run the main program in single step mode, and fill in the table when stackrelated (that access the stack) instructions are being run.. You can also refer to
Assembly Output window during your single-step execution for understanding of
program flow.

Set Ready
Delay to 0

Switch

Answer the questions below after filling the table:


Q1. What size of stack in bytes (minimum) are needed to properly run the entire
program?
Q2. Why are the PUSH and POP instructions are used in subroutine DISP?
access to
instruction
stack
before
1st time CALL READSW

PC
after

next instruction
to be run

DISP

top of stack: SP
before
after

2nd time

RET
3rd time CALL DISP
4th time PUSH BC
5th time POP BC
6th time RET

SSEG

PUSH BC
LD H,0
LD L,C
LD BC,SSEG
ADD HL,BC
LD A,(HL)
OUT (1),A
POP BC
RET
DB 77H ; 0 *** look up table
DB 12H ; 1 *** for SSD
DB 6BH ; 2
DB 5BH ; 3
DB 1EH ; 4
DB 5DH ; 5
DB 7DH ; 6
DB 13H ; 7

Anda mungkin juga menyukai