0% found this document useful (0 votes)
56 views

Stack&Subroutine

Here are the steps to pass parameters using stack: 1. Push the parameters onto the stack in reverse order 2. Call the subroutine 3. Access the parameters from the stack using SP relative addressing inside the subroutine 4. Pop the parameters off the stack before returning from the subroutine This allows a subroutine to be called without disturbing the registers, and parameters of any type (register pairs, single registers, immediate values) can be passed. The calling routine and subroutine do not need any information about each other apart from the interface defined by the parameters passed on the stack.

Uploaded by

Raj shah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Stack&Subroutine

Here are the steps to pass parameters using stack: 1. Push the parameters onto the stack in reverse order 2. Call the subroutine 3. Access the parameters from the stack using SP relative addressing inside the subroutine 4. Pop the parameters off the stack before returning from the subroutine This allows a subroutine to be called without disturbing the registers, and parameters of any type (register pairs, single registers, immediate values) can be passed. The calling routine and subroutine do not need any information about each other apart from the interface defined by the parameters passed on the stack.

Uploaded by

Raj shah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 71

q 2CP05: COMPUTER ORGANIZATION AND ARCHITECTURE

CREDITS - 4 (LTP:3,0,1)

0
Stack

q It can be described as a set of memory locations in the R/W


memory specified by a programmer in a main program
q These memory locations are used to store binary information
temporarily during the execution of a program

1
stack

q The beginning of the stack is defined in the program by


using the instruction
q LXI SP, 16bit address
q Once the stack location is defined, storing of data bytes
begins at the memory address that is one less than the
address in the stack register
q E.g. LXI SP,2099H
q The storing of data bytes begins at 2098H and continues in
decreases memory addresses such as 2097H….
q Therefore as a general practice the stack is initialized at
the highest available memory location to prevent the
program from being destroyed by the stack information.
q The size of stack is limited only by the available memeory

2
q The stack is shared by the programmer and the
microprocessor
q The programmer can store and retrieve the contents of
register pair by using PUSH and POP instructions
q The microprocessor automatically stores the contents of PC
when a subroutine is called

3
4
One byte instruction, 3 machine cycles required and 12 Tstats
required

5
q PUSH Rp
q Opcode fetch 6 T state : fetch opcode and decrement SP
q Memory write :SP-> address bus, higher register is written,
SP=SP-1
q Memory write : SP-> address bus ,Lower register is written
q No flags are modified

6
7
POP

q POP rp
q 1 byte, 3 machine cycles and 10Tstates
q Opcode fetch 4 T state : fetch opcode
q Memory read :SP-> address bus, memory contain is copied to
lower register , SP=SP+1
q Memory read : SP-> address bus , memory contain is copied
to higher , SP=SP+1
No flags are modified

8
9
PUSH B
PUSH D
POP B
POP D

10
Accumulator is considered as higher register and
flag as lower register

11
12
13
14
15
Read the following program and answer the questions given below
1. LXI SP,0400H
2. LXI B, 2055H
3. LXI H,22FFH
4. LXI D,2090H
5. PUSH H
6. PUSH B
7. POP H
8. POP B
a. What is the memory location of the stack where the first byte will be
stored?
b. What is stored in memory location 03FEH when line 5 (PUSH H) is
executed?
c. After the execution of line 6 (PUSH B) what is the address in the stack
pointer register and what is stored in stack memory location 03FDH?
d. Specify the contents of HL after execution of POP H? Also specify value in
SP
e. Specify the contents of BC after execution of POP B? Also specify value in
SP 16
17
q Access stack without disturbing sp
q Lxi H,0000
q DAD SP
q Mov a,m
To access top of stack
XTHL : exchange H and L with top of stack without changing SP
1 byte instruction, 5 machine cycle and 16 T states

18
19
Subroutine

q Two instructions to implement subroutines


q CALL and RET
q CALL is used to redirect program execution to the
subroutine
q RET instruction is used to return the execution to the
calling routine

20
5 machine cycles and 18 T
states

21
22
Three machine cycles and 10T-states 23
24
5 machine cycles and 18 T-States 25
Three machine cycles and 10T-states 26
27
Read the following program and answer the questions
2000 LXI SP,2100H Delay : 2064 PUSH H
2003 LXI B,2000H 2065 PUSH B
2006 PUSH B 2066 LXI B,80FFH
2007 POP PSW LOOP: 2069 DCX B
2008 LXI H,200BH 206A MOV A,B
200B CALL 2064H 206B ORA C
200E OUT 01H 206C JNZ LOOP
2010 HLT 206F POP B
2070 RET
1. What is the status of the flags and the contents of the accumulator after the
execution of the POP instruction located at 2007H?
2. Specify the stack locations and their contents after the execution of the CALL
instruction
3. What are the contents of the stack pointer register and the program counter after the
execution of the CALL instruction?
4. Specify the memory location where the program returns after the subroutine
28
5. What is the ultimate outcome of the program?
29
q Separate odd and even number and count them

30
LXI D,2600H
MVI A,00
STAX D even: Mov a,m
INX D stax B
LXI B,2700H INX B
STAX B PUSH H
INX B LXI H,2700H
LXI H,2800H INR M
ag: INX H POP H
mov a, M Com: PUSH H
ANI 01 LXI H,2800H
JZ even dcr M
MOV a, M POP H
STAX D jnz ag
INX D hlt
PUSH H
LXI H,2600H
INR M
POP H
JMP Com 31
q BCD to binary
q 255=2X100+5X10+5
q 2X100=c8H
q 5X10=32H
q C8+32+5=FF

32
BCD to Binary

LXI SP, 27FFH Mov D,A


LXI H,2500H Mvi E,0Ah
Mov D,M CALL Multi
MOV A,D next1: ADD c
cpi 00 mov c,a
jz next MoV a,m
Mvi E,64H ANI 0fH
Call Multi ADD C
next: Mov C,A STA 2600H
inx H HLT
Multi: PUSH B
Mov A,M PUSH D
ANI 0F0H XRA A
jz next1 sum: ADD E
RRC DCR D
RRC JNZ sum
RRc POP D
RRC PoP B
RET
33
q to find the factorial of a number

34
LXI H, 2000H
MOV B, M MULTIPLY: MOV E, B
MVI A, 00H
MVI D, 01H LOOP: ADD D
FACTORIAL: CALL MULTIPLY DCR E
DCR B JNZ LOOP
MOV D, A
JNZ FACTORIAL RET
INX H
MOV M, D
HLT

35
Binary to BCd

q FF to 255
q FF/64=2 FF-64=9B-64=37 so 2 times
q 37/0A five times subtraction is possible so answer is 5
q Remainder is 5

36
Binary to BCD

LXI SP,27FFH RLC


LXI D, 2500H RLC
lxi H,2600H RLC
LDAX D RLC
MVI B,64H ORA B
CALL DIV MOV M,A
MOV M,C HLT
INX H DIV: MVI C,0ffh
MVI B,0AH NEXT: INR C
CALL DIV SUB B
MOV B,A
MOV A,C JNC NEXT
ADD B
RET

37
Binary to acii
q LXI SP,27FFH
MOV A,M
q LXI H,2500H ANI 0FH
q LXI D 2600H CALL ASCII
q MOV A,M STAXD
HLT
q ANI 0F0H
q RLC ASCII: CPI 0AH
q RLC JC X
ADD 07H
q RLC X: ADD 30h
q RLC RET
q CALL ASCII
q STAXD
q INXD

38
Home work

q Write a subroutine to convert ASCII to digit. Using this


subroutine convert two ASCII value to one byte

39
q Write a subroutine to find minimum number
and using this subroutine arrange given
numbers in ascending order
Lxi SP,4000H
LXI H, 2500H MIN: PUSH H
PUSH B
MVI C,05
MOV E,L
AGAIN: DCR C AG: INX H
JZ last CMP M
JC CONT
MOV A,M
MOV A,M
CALL MIN MOV E,L
MOV B,M CONT: DCR C
JNZ AG
MOV M,A
POP B
MOV D,H POP H
MOV A,B RET
STAX D
INX H
JMP AGAIN 40
q Parameter passing using stack

41
LXI SP,27FFH SUM: LXI H,0000
AGAIN: LDAX D
LXI D,2500H DAD SP
ADD B
MVI C,5 INX H
MOV B,A
PUSH D INX H
JNC X
DCX SP INX H
INR L
DCX SP INX H
X: INX D
CALL SUM MOV E,M
DCR C
POP B INX H
JNZ AGAIN
POP D MOV D,M
MOV C,L
HLT PUSH H
POP H
MVI L,00
DCX H
MVI B,00
DCX H
MOV M,C
DCX H
MOV M,B
RET

42
43
q Recursive routine to find factorial of number

44
fact: lxi h,0000 inx h
q Lxi d,0003 dad sp inx h
q Lxi sp,2700H inx h mov b, m
inx h call multi
q dcx sp
mov a,m term: lxi h,0000
q dcx sp cpi 01 dad sp
q push d jz term inx h
dcr a inx h
q call fact
mov c,a inx h
q pop d mvi b,00 inx h
q pop d dcx sp mov m,a
dcx sp inx h
q hlt
push b mvi m,00
call fact ret
pop b multi: xra a
pop b ag: add b
lxi h,0000 dcr c
dad sp jnz ag
ret
45
q Multiplication

46
If condition is false they take 2 machine cycle i.e 9 T states 47
If condition is false it take 1machine cycle and 6 Tstate 48
otherwise 3 machine cycle and 12 T-state
49
Counters and Time Delays

50
51
52
53
54
55
56
57
58
59
60
61
Time delay using register pair

62
63
64
Total=7+3567=3574

65
66
q Write a program to count continuously in hexadecimal from
FFH to 00H in a system with 0.5us clock period. Generate
delay of 1ms between each count and displays the number at
one of the output port

67
LXI SP,2700H
MVI B,00
Next: DCR B
MOV A,B
Out 00H
CALL Delay
Jmp next

Delay : MVI C,COUNT


LOOP: DCR C
JNZ LOOP
RET

68
TL=14*0.5usXcount
Outside loop=18+7+10+10+4+4+10=63
Total delay=63*0.5us+count*14*0.5us
Count=?

69
PCHL : load program counter with HL contents
q 1 byte , 1 machine cycle and 6 T-states
q It is equivalent to a 1 byte unconditional jump instruction
SPHL : copy HL registers to SP
q 1 bye, 1 machine cycle and 6 T-states

70

You might also like