0% found this document useful (0 votes)
38 views25 pages

Review Last Lecture - Subroutine & Parameter Passing - Basic Input/Output

The document reviews the last lecture, covers subroutines and parameter passing, and discusses basic input/output. It provides an example of a small test question and answer related to subroutines. The document also discusses different methods of passing parameters including through registers and on the stack.

Uploaded by

Lathifah Sahda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views25 pages

Review Last Lecture - Subroutine & Parameter Passing - Basic Input/Output

The document reviews the last lecture, covers subroutines and parameter passing, and discusses basic input/output. It provides an example of a small test question and answer related to subroutines. The document also discusses different methods of passing parameters including through registers and on the stack.

Uploaded by

Lathifah Sahda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

LECTURE 4

• Review last lecture


• Subroutine & parameter passing
• Basic Input/Output

1
Small Test

• Prepare a piece of paper


• On the above right, write your
name, NRP and class.

2
15 minutes Instruction ADD 8(R1),(A)
and some of the data are
stored in memory as in the
796 picture. The instruction will be
800 ADD A, 4(R1) executed. It is known also that
804 ADD 8(R1), (A) the content of R1=1000 &
……... A=2000.
996 1000 1.What is the content of PC?
1000 500 Explain your answer (10
1004 3500 points)
1008 2000 2.Write the fetch and
…… execution cycles. (50 points)
2000 1000
3.Write the end result after
execution. (40 points)
2004 1050
3
Answer of the test question
1. The content of PC = 804, because the
instruction that will be executed is in
memory location 804

2. Fetch cycle: Execution cycle:

MAR  [PC] MAR  A


READ
Increment PC,
MAR  [MDR]
READ
READ
IR  [MDR] R2  [MDR]
MAR  8+[R1]
READ
MDR  [R2] + [MDR]
WRITE 4
END RESULT
PC 808

800 ADD A, 4(R1)


804 ADD 8(R1), (A)
……...
996 1000
1000 500
1004 3500
1008 2500
……

2000 1000
2004 1050
5
ASSEMBLY LANGUAGE
START 100 Load R1,N SUM EQU 200
104 Move R2,#NUM1 ORIGIN 204
108 Clear R0
N DATAWORD 100
LOOP 112 Add R0,(R2)
NUM1 RESERVE 400
116 Add R2,#4
ORIGIN 100
120 Decrement R1
START LOAD R1,N
124 Branch >0 LOOP
MOVE R2,#NUM1
128 Store R0,SUM
CLR R0
132
LOOP ADD R0,(R2)
…….
ADD R2,#4
SUM 200
DEC R1
N 204 100
BGTZ LOOP
NUM1 208
STORE R0,SUM
NUM2 212
RETURN
…….
END START
NUMn 604
6
PARAMETER PASSING
Calling Program
Load R2,N • R2, R3, and R4 are called
Move R4,#NUM1 passing parameters.
Call LISTADD • If a lot of parameters are
Store R3,SUM used, the possibility is run
out of parameters.
Subroutine • Other alternative: passing
LISTADD Clear R3 parameter is put into stack
LOOP Add R3,
(R4)+
Decrement R2
Branch>0 LOOP
Return
7
PARAMETER PASSING
Calling Program
Calling Program Load R2,N
Load R2,N Move R4,#NUM1
Move R4,#NUM1 Call LISTADD
Call LISTADD Store R3,SUM
Store R3,SUM Subroutine
Subroutine LISTADD Subtract SP, SP, #4
LISTADD Clear R3 Store R5, (SP)
LOOP Add R3, Clear R3
(R4)+ LOOP Load R5, (R4)
Decrement R2 Add R3, R3, R5
Branch>0 LOOP Add R4, R4, #4
Return Subtract R2, R2, #1
Branch if_[R2]>0 LOOP
Load R5, (SP)
Add SP, SP, #4
8
Return
PARAMETER PASSING
Move -(SP),#NUM1
Calling Program
Load -(SP),N
Load R2,N Call LISTADD
Move R4,#NUM1 Store 4(SP),SUM

Call LISTADD Add SP,#8



Store R3,SUM
LISTADD LoadMultiple -(SP),R1-R3
Load R1,16(SP)
Subroutine Load R2,20(SP)
LISTADD Clear R3 Clear R3
LOOP Add R3, LOOP Add R3,(R2)+
(R4)+ Decrement R1
Decrement R2 Branch>0 LOOP
Branch>0 LOOP Load 20(SP),R3
Return LoadMultiple R1-R3,(SP)+
Return 9
STACK FRAME (If Link_Reg is used)
Load -(SP),#NUM1 STACK
Load -(SP),N
Call LISTADD
Store 4(SP),SUM
Add SP,#8

LISTADD LoadMultiple -(SP),R1-R3
Load R1,12(SP) Level 3 => [R3]

Load R2,16(SP) [R2]


Clear R3 [R1]
LOOP Add R3,(R2)+ Level 2 => n
Decrement R1
NUM1
Branch>0 LOOP
Level 1 =>
Load 16(SP),R3
LoadMultiple R1-R3,(SP)+
Return Initial SP
STACK FRAME (If Push_Stack is used)
Load -(SP),#NUM1 STACK
Load -(SP),N
Call LISTADD
Store 4(SP),SUM
Add SP,#8

Level 3 => [R3]
LISTADD LoadMultiple -(SP),R1-R3
Load R1,16(SP) [R2]
Load R2,20(SP) [R1]
Clear R3 Level 2 => Return Address
LOOP Add R3,(R2)+ n
Decrement R1
NUM1
Branch>0 LOOP
Level 1 =>
Load 20(SP),R3
LoadMultiple R1-R3,(SP)+
Return Initial SP
What is this With Subroutine & parameter
passed through registers
program doing?
Calling program
Load R2, N
Load R2, N
Move R4, #NUM1
Clear R3
Call LISTADD
Move R4, #NUM1 Store R3, SUM
LOOP: Load R5, (R4) Subroutine
Add R3, R3, R5 LISTADD: Subtract SP, SP, #4
Add R4, R4, #4 Store R5, (SP)
Subtract R2, R2, #1 Clear R3
Branch_if_[R2]>0 LOOP LOOP: Load R5, (R4)
Add R3, R3, R5
Store R3, SUM
Add R4, R4, #4
Subtract R2, R2, #1
Branch_if_[R2]>0 LOOP
Load R5, (SP)
Add SP, SP, #4
12
Return
Parameters passed on the
Main Program stack
2000 Load R2,
PARAM2
2004 Subtract SP, SP,
#4 [R3] from SUB1
2008 Store R2, (SP) [R2] from SUB1
2012 Load R2, FP  [FP] from SUB1
PARAM1
2164
2016 Subtract SP, SP,
#4 PARAM3
2020 Store R2, (SP) [R5] from Main
2024 Call SUB1 [R4] from Main
2028 Load R2, (SP) [R3] from Main
2032 Store R2,
[R2] from Main
RESULT
2036 Add SP, SP, #8 FP  [FP] from Main
2040 next instruction 2028
PARAM1
First subroutine
PARAM2
2100SUB1: Subtract SP, SP, #24
2104 Store  Old TOS
LINK_reg,20(SP)
2108 Store FP, 16(SP)
What about this?
Main Program 2132 Load R2, 8(FP)
2000 Load R2, 2136 Load R3, 12(FP)
PARAM2
Load R4, PARAM3
2004 Subtract SP, SP,
#4 Subtract SP, SP,
2008 Store R2, (SP) #4
2012 Load R2, Store R4, (SP)
PARAM1 Call SUB2
2016 Subtract SP, SP, Load R4, (SP)
#4
Add SP, SP, #4
2020 Store R2, (SP)
2024 Call SUB1 Store R5, 8(FP)
2028 Load R2, (SP) Load R5, (SP)
2032 Store R2, Load R4, 4(SP)
RESULT Load R3, 8(SP)
2036 Add SP, SP, #8
Load R2, 12(SP)
2040 next instruction
First subroutine Load FP, 16(SP)
2100SUB1: Subtract SP, SP, #24 Load
2104 Store LINK_reg,20(SP)
LINK_reg,20(SP) Add SP, SP, #24
2108 Store FP, 16(SP) Return
Second subroutine
3000 SUB2: Subtract SP,SP,#12
3004 Store FP,8(SP)
Store R2,4(SP)
Store R3,(SP)
Add FP,SP,#8
Load R2,4(FP)
Store R3,4(FP)
Load R3,(SP)
Load R2,4(SP)
Load FP,8(SP)
Add SP,SP,#12
Return
BASIC INPUT/OUTPUT

16
INPUT/OUTPUT ORGANIZATION

Processor Memory

Interconecton

I/O device 1 …… I/O device n

17
I/O INTERFACE

Address Lines
Bus Data Lines
Control Lines

Address Control Data and


Decoder Circuit Status Registers

Move R0,DATAIN
I/O Device
Move DATAOUT,R0

18
KIN : Keyboard Input
KIE : Keyboard Interrupt Enable
KIRQ : Keyboard Interrupt Request

DOUT : Display output


DIE : Display Interrupt Enable
DIRQ : Display Interrupt Request
19
Basic Operation of Input,Output

In the beginning KIN=0 and DOUT=1


For the keyboard, KIN=0 means keyboard is ready. When data
is in, KIN is Set (KIN=1) and as long as KIN=1 keyboard can
not accept new data.
In the CPU side, KIN=0 means there is no data in the keyboard.
If KIN=1 the data will be transferred to register and KIN is Reset
(KIN=0).

How about the Display?


20
READWAIT Read the KIN flag
Branch to READWAIT if KIN=0
Transfer data from KBD_DATA to R5
READWAIT: LoadByte R4, KBD_STATUS
And R4, R4, #2
Branch_if_[R4]=0 READWAIT
LoadByte R5, KBD_DATA

WRITEWAIT Read the DOUT flag


Branch to WRITEWAIT if DOUT=0
Transfer data from R5 to DISP_DATA
WRITEWAIT: LoadByte R4, DISP_STATUS
And R4, R4, #4
Branch_if_[R4]=0 WRITEWAIT
StoreByte R5, DISP_DATA 21
Example I/O program (RISC)
Move R2, #LOC
MoveByte R3, #CR
READ: LoadByte R4, KBD_STATUS
And R4, R4, #2
Branch_if_[R4]=0 READ
LoadByte R5, KBD_DATA
StoreByte R5, (R2)
Add R2, R2, #1.
ECHO: LoadByte R4, DISP_STATUS
And R4, R4, #4
Branch_if_[R4]=0 ECHO
StoreByte R5, DISP_DATA
Branch_if_[R5] ≠ [R3] READ
22
Example I/O program (CISC)
Move R2, #LOC
READ: TestBit KBD_STATUS, #1
Branch=0 READ
MoveByte (R2), KBD_DATA
ECHO: TestBit DISP_STATUS, #2
Branch=0 ECHO
MoveByte DISP_DATA, (R2)
CompareByte (R2)+, #CR
Branch ≠ 0 READ

23
HOMEWORK

• Read Chapter 4 reference 1.


• Read Hamacher Chapter 3.
STACK FRAME
LoadMultiple -(SP),R1-R3

Load -(SP), R1 Level 3 => [R3]


[R2]
Load -(SP), R2
[R1]
Load -(SP), R3
Level 2 => n

NUM1
Level 1 =>

Initial SP 25

You might also like