0% found this document useful (0 votes)
6 views32 pages

CC372_Spring_2025_Tutorial_08-Part-01

The document contains a series of exercises and solutions related to SIC/XE assembly language programming, focusing on defining functions and subroutines. Each exercise includes a problem statement, followed by a solution that provides the assembly code for the specified function or operation. The exercises cover various programming concepts such as arithmetic operations, parameter passing, and memory manipulation.
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)
6 views32 pages

CC372_Spring_2025_Tutorial_08-Part-01

The document contains a series of exercises and solutions related to SIC/XE assembly language programming, focusing on defining functions and subroutines. Each exercise includes a problem statement, followed by a solution that provides the assembly code for the specified function or operation. The exercises cover various programming concepts such as arithmetic operations, parameter passing, and memory manipulation.
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/ 32

Systems Programming

Tutorial Eight-Part-01: Sheet Four Solution

Lecture Instructor : Prof. Dr. Ahmed El Nahas


Tutorial Instructors Committee:
Eng. Ahmed Abdelhamid Waheed Abdelwahab
Eng. Nour Hamdy Eng. Shereen Mabrouk 1
Exercise(1)
1. a) Write a SIC/XE assembly language definition of the function
FUN(U, Q, R), that stores in U the value: 5Q3 + R.
b) Write coding of the call: FUN(UU, QQ, RR).

2
Solution-Subroutine

Label Mnemonic Operand


FUN LDA #5
MUL Q
MUL Q
MUL Q
ADD R
STA @U
RSUB
U RESW 1
Q RESW 1
R RESW 1

3
Solution-Call

Label Mnemonic Operand


LDA #UU
STA U
LDA QQ
STA Q
LDA RR
STA R
JSUB FUN
UU RESW 1
QQ RESW 1
RR RESW 1

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

Label Mnemonic Operand


FUNC LDA #10
MUL N
MUL N
SUB #5
STA @R
RSUB
N RESW 1
R RESW 1

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

Label Mnemonic Operand


Fun LDA #10
MUL Q
ADD #20
MUL Q
ADD #30
MUL Q
ADD #40
STA @P
RSUB
Q RESW 1
P RESW 1
12
Solution-Call

Label Mnemonic Operand


LDA AQ
STA Q
LDA #AP
STA P
JSUB FUN
AQ RESW 1
AP RESW 1

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>);.

Let it be 463 for example

14
Solution-Subroutine

Label Mnemonic Operand


FUNC LDA @P1
ADD C
STA @P1
LDA @P2
SUB C
STA @P2
RSUB
P1 RESW 1
P2 RESW 1
C RESW 1

15
Solution-Call

Label Mnemonic Operand


LDA #PA1
STA P1
LDA #PA2
STA P2
LDA #463
STA C
JSUB FUNC
PA1 RESW 1
PA2 RESW 1

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

Label Mnemonic Operand


Fun LDA #2
MUL P
ADD #3
MUL P
ADD #4
STA @Q
RSUB
P RESW 1
Q RESW 1

18
Solution-Call

Label Mnemonic Operand


LDA PPP
STA P
LDA #QQQ
STA Q
JSUB FUN
PPP RESW 1
QQQ RESW 1

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

Label Mnemonic Operand


CUBE LDA P
MUL P
MUL P
STA @Q
RSUB
P RESW 1
Q RESW 1

21
Solution-Call

Label Mnemonic Operand


LDA XXX
STA P
LDA #YYY
STA Q
JSUB CUBE
XXX RESW 1
YYY RESW 1

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)

Label Mnemonic Operand


SWAP LDA @P
STA TEMP
LDA @Q
STA @P
LDA TEMP
STA @P
RSUB
P RESW 1
Q RESW 1
TEMP RESW 1

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

Label Mnemonic Operand


LDA #U
STA P
LDA #W
STA Q
JSUB SWAP
U RESW 1
W RESW 1

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

Label Mnemonic Operand


FUN LDS @P
LDA @Q
STA @P
LDA @R
STA @Q
STS @R
RSUB
P RESW 1
Q RESW 1
R RESW 1

28
Solution(b)-Call

Label Mnemonic Operand


LDA #PP
STA P
LDA #QQ
STA Q
LDA #RR
STA R
JSUB FUN
PP RESW 1
QQ RESW 1
RR RESW 1

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

Label Mnemonic Operand


FUN LDA #2
MUL @P
ADD #4
MUL @P
ADD #6
MUL @P
STA @P
RSUB
P RESW 1

31
Solution-Call

Label Mnemonic Operand


LDA #PPP
STA P
JSUB FUN
PPP RESW 1

32

You might also like