CC372_Spring_2025_Tutorial_08-Part-01
CC372_Spring_2025_Tutorial_08-Part-01
2
Solution-Subroutine
3
Solution-Call
4
Exercise(2)
2. a) Write definition of subroutine fun(P, Q, R), that returns in R the
value:
𝑃 + 10 𝑄 + 20 𝑖𝑓 𝑃 ≤ 𝑄
R=൝
𝑄 2 + 15 𝑖𝑓 𝑃 > 𝑄
b) Write code for the call: fun(AP, AQ, AR)
5
Solution-Subroutine
Label Mnemonic Operand
FUN LDA P
COMP Q
JGT ELSE
LDA P
ADD #10
MUL Q
ADD #20
STA @R
J DONE
ELSE LDA Q
MUL Q
ADD #15 6
Solution-Subroutine
Label Mnemonic Operand
STA @R
DONE RSUB
P RESW 1
Q RESW 1
R RESW 1
7
Solution-Call
Label Mnemonic Operand
LDA AP
STA P
LDA AQ
STA Q
LDA #AR
STA R
JSUB FUN
AP RESW 1
AQ RESW 1
AR RESW 1
8
Exercise(3)
3. Write a sequence of assembly instructions equivalent to the
function func, having two parameters n, R:
void func(int n, int &R)
{𝑅 = 10 ∗ 𝑛 ∗ 𝑛 − 5;}
9
Solution
10
Exercise(4)
4. a) Write a SIC/XE assembly language definition of function
FUN(Q, P), that returns in P the value:
𝑃= 10𝑄 + 20 𝑄 + 30 𝑄 + 40
b) Write code for the call: FUN(AQ, AP).
11
Solution-Subroutine
13
Exercise(5)
5. a) Write a SIC/XE assembly language definition of function:
void func(int * p1, int * P2, int c) {
*p1 = *p1 + c;
*p2 = *p2 – c;
}
b)Write code for the call:
func(pa1, pa2, <lasy_three_digits_of_your_id>);.
14
Solution-Subroutine
15
Solution-Call
16
Exercise(6)
6. a) Write the definition of the function FUN (P,Q) , that stores in Q
the value ( ( 2 * P + 3 ) * P + 4 )
b) Write the code for the call : FUN (PPP,QQQ)
17
Solution-Subroutine
18
Solution-Call
19
Exercise(7)
7. a) Write definition of subroutine CUBE(P, Q), that stores in Q the
value P3. Make sure to include ALL of the necessary directives in
your code.
b) Write code for the call: CUBE(XXX, YYY).
20
Solution-Subroutine
21
Solution-Call
22
Exercise(8)
8. Write the definition of the function SWAP (P, Q), that swaps values
in P and Q. Include all necessary declarations. Also, write code for
the call: SWAP (U, W).
23
Solution-Subroutine (With Auxiliary Variable Temp)
24
Alternative Solution-Subroutine
(With Register S as Temp)
Label Mnemonic Operand
SWAP LDS @P
LDA @Q
STA @P
STS @Q
RSUB
P RESW 1
Q RESW 1
25
Solution-Call
26
Exercise(9)
9. a) Write definition of function FUN (P, Q, R), that circularly shifts its
parameters:
- Save contents of P in register S,
- Set P = Q ;
- Set Q = R ;
- Set R = contents of S
b) Write code for the call FUN (PP, QQ, RR)
Make sure to include ALL the necessary directives in your code.
27
Solution(a)-Subroutine
28
Solution(b)-Call
29
Exercise(10)
10. Write definition of function FUN (P), that replaces P by:
[(2 * P + 4) * P + 6] * P,
Also, write code for the call: FUN (PPP)
Make sure to include ALL the necessary directives in your code.
30
Solution-Subroutine
31
Solution-Call
32