0% found this document useful (0 votes)
15 views80 pages

CC372_Spring_2025_Tutorial_07-Part-02

The document provides solutions to various exercises in SIC/XE assembly language programming, including calculating dot products, performing arithmetic operations, and manipulating arrays. It contains code snippets and complete programs for each exercise, detailing the labels, mnemonics, and operands used. The exercises cover a range of programming concepts such as loops, conditionals, and data 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)
15 views80 pages

CC372_Spring_2025_Tutorial_07-Part-02

The document provides solutions to various exercises in SIC/XE assembly language programming, including calculating dot products, performing arithmetic operations, and manipulating arrays. It contains code snippets and complete programs for each exercise, detailing the labels, mnemonics, and operands used. The exercises cover a range of programming concepts such as loops, conditionals, and data 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/ 80

Systems Programming

Tutorial Seven-Part-02: Sheet Three 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. Given two lists XX and YY each of 5 elements, write a SIC/XE code
snippet to calculate the dot product of the two lists i.e. and store
the result in sum variable i.e. sum=sum of(XX[i] * YY[i]) for i =1,
2,…,5.
Hint: (Define list XX with elements {1, 3, 5, 7, 9} and list YY with
elements {2, 4, 6, 8, 10}. You can use WORD directive to define a
list)
XX WORD 1, 3, 5, 7, 9
YY WORD 2, 4, 6, 8, 10

2
Solution

Label Mnemonic Operand


LDA #0
STA SUM
LDT #15
LDS #3
LDX #0
LOOP LDA XX,X
MUL YY,X
ADD SUM
STA SUM
ADDR S,X

3
Solution(CONT’D)

Label Mnemonic Operand


COMPR X,T
JLT LOOP
XX WORD 1, 3, 5, 7, 9
YY WORD 2, 4, 6, 8, 10
SUM RESW 1

4
Solution(Complete Program)

Label Mnemonic Operand


PROG START 0
LDA #0
STA SUM
LDT #15
LDS #3
LDX #0
LOOP LDA XX,X
MUL YY,X
ADD SUM
STA SUM
ADDR S,X
5
Solution(Complete Program)(CONT’D)

Label Mnemonic Operand


COMPR X,T
JLT LOOP
XX WORD 1, 3, 5, 7, 9
YY WORD 2, 4, 6, 8, 10
SUM RESW 1
END

6
Exercise(2)
2. Assuming all variables are binary integers, write a SIC/XE assembly
language code snippets for each of the following pseudo code
snippets.

7
Exercise(2)(a)
a. Q:=1;
SUM:=0;
while Q ≤ 4 do
{
R:= Q*Q ;
SUM := SUM+R;
Q := Q+1
}

8
Solution
Label Mnemonic Operand
LDA #1
STA Q
LDA #0
STA SUM
WHILE LDA Q
COMP #4
JGT OUT
LDA Q
MUL Q
STA R
LDA SUM
ADD R
9
Solution(CONT’D)
Label Mnemonic Operand
STA SUM
LDA Q
ADD #1
STA Q
J WHILE
Q RESW 1
SUM RESW 1
R RESW 1

10
Solution(Complete Program)
Label Mnemonic Operand
PROG START 0
LDA #1
STA Q
LDA #0
STA SUM
WHILE LDA Q
COMP #4
JGT OUT
LDA Q
MUL Q
STA R
LDA SUM
ADD R 11
Solution(Complete Program)(CONT’D)
Label Mnemonic Operand
STA SUM
LDA Q
ADD #1
STA Q
J WHILE
Q RESW 1
SUM RESW 1
R RESW 1
END

12
Exercise(2)(b)
b. int U[8]=[1, 2, 3, 4, 5, 6, 7, 8];
int V[8];
for i=0 until 7
{
V[i] := U[i];
}

13
Solution
Label Mnemonic Operand
LDT #24
LDS #3
LDX #0
LOOP LDA U,X
STA V,X
ADDR S,X
COMPR X,T
JLT LOOP
U WORD 1, 2, 3, 4, 5, 6, 7, 8
V RESW 8

14
Solution(Complete Program)
Label Mnemonic Operand
PROG START 0
LDT #24
LDS #3
LDX #0
LOOP LDA U,X
STA V,X
ADDR S,X
COMPR X,T
JLT LOOP
U WORD 1, 2, 3, 4, 5, 6, 7, 8
V RESW 8
END 15
Exercises(2)(c)
c. Do Y:=2Y; Z=Z/3; while(Y < 1000)

16
Solution
Label Mnemonic Operand
DOWHILE LDA #2
MUL Y
STA Y
LDA Z
DIV #3
STA Z
LDA Y
COMP #1000
JLT DOWHILE
Y RESW 1
Z RESW 1

17
Solution(Complete Program)
Label Mnemonic Operand
PROG START 0
DOWHILE LDA #2
MUL Y
STA Y
LDA Z
DIV #3
STA Z
LDA Y
COMP #1000
JLT DOWHILE
Y RESW 1
Z RESW 1
END
18
Exercise(2)(d)
d. N := 4; Y := 0;
While (N ≤ 256) do begin Y := Y + N; N := 4 end;
Z := Y * Y;

19
Solution
Label Mnemonic Operand
PROG START 0
LDA #4
STA N
LDA #0
STA Y
WHILE LDA N
COMP #256
JGT OUT
LDA Y
ADD N
STA Y
LDA #4
STA N
20
Solution(CONT’D)
Label Mnemonic Operand
J WHILE
OUT LDA Y
MUL Y
STA Z
N RESW 1
Y RESW 1
Z RESW 1
END

21
Solution(Complete Program)
Label Mnemonic Operand
LDA #4
STA N
LDA #0
STA Y
WHILE LDA N
COMP #256
JGT OUT
LDA Y
ADD N
STA Y
LDA #4
STA N

22
Solution(Complete Program) (CONT’D)
Label Mnemonic Operand
J WHILE
OUT LDA Y
MUL Y
STA Z
N RESW 1
Y RESW 1
Z RESW 1

23
Exercise(2)(e)
e. while ( P *Q ≤ 200 ) do
begin P := P − 1 ; Q := Q + 3 ; R := R + P end ;
W := 5 * R ;

24
Solution
Label Mnemonic Operand
WHILE LDA P
MUL Q
COMP #200
JGT OUT
LDA P
SUB #1
STA P
LDA Q
ADD #3
STA Q
LDA R
ADD P
STA R
25
Solution(CONT’D)
Label Mnemonic Operand
J WHILE
OUT LDA #5
MUL R
STA W
P RESW 1
Q RESW 1
R RESW 1
W RESW 1

26
Solution(Complete Program)
Label Mnemonic Operand
PROG START 0
WHILE LDA P
MUL Q
COMP #200
JGT OUT
LDA P
SUB #1
STA P
LDA Q
ADD #3
STA Q
LDA R
ADD P
STA R 27
Solution(Complete Program)(CONT’D)
Label Mnemonic Operand
J WHILE
OUT LDA #5
MUL R
STA W
P RESW 1
Q RESW 1
R RESW 1
W RESW 1
END

28
Exercise(2)(f)

29
Solution
Label Mnemonic Operand
LDA LA
COMP AL
JEQ CASE1
COMP BL
JEQ CASE2
COMP CL
JEQ CASE3
COMP DL
JEQ CASE4
LDA EL
SUB #1
STA EL
J OUT
30
Solution(CONT’D)
Label Mnemonic Operand
CASE1 LDA AL
SUB #1
STA AL
J OUT
CASE2 LDA BL
SUB #1
STA BL
J OUT
CASE3 LDA CL
SUB #1
STA CL
J OUT 31
Solution(CONT’D)
Label Mnemonic Operand
CASE4 LDA DL
SUB #1
STA DL
OUT ……. ….
LA RESW 1
AL RESW 1
BL RESW 1
CL RESW 1
DL RESW 1

32
Solution(Complete Program)
Label Mnemonic Operand
PROG START 0
LDA LA
COMP AL
JEQ CASE1
COMP BL
JEQ CASE2
COMP CL
JEQ CASE3
COMP DL
JEQ CASE4
LDA EL
SUB #1
STA EL
J OUT 33
Solution(Complete Program)(CONT’D)
Label Mnemonic Operand
CASE1 LDA AL
SUB #1
STA AL
J OUT
CASE2 LDA BL
SUB #1
STA BL
J OUT
CASE3 LDA CL
SUB #1
STA CL
J OUT 34
Solution(Complete Program)(CONT’D)
Label Mnemonic Operand
CASE4 LDA DL
SUB #1
STA DL
OUT ……. ….
LA RESW 1
AL RESW 1
BL RESW 1
CL RESW 1
DL RESW 1
END

35
Exercise(2)(g)

36
Solution
Label Mnemonic Operand
WHILE LDA P
COMP Q
JLT OUT
LDA R
ADD P
STA R
LDA P
SUB #2
STA P
J WHILE
OUT LDA SS
ADD R

37
Solution(CONT’D)
Label Mnemonic Operand
STA SS
P RESW 1
Q RESW 1
R RESW 1
SS RESW 1

38
Solution(Complete Program)
Label Mnemonic Operand
PROG START 0
WHILE LDA P
COMP Q
JLT OUT
LDA R
ADD P
STA R
LDA P
SUB #2
STA P
J WHILE
OUT LDA SS
ADD R
39
Solution(Complete Program)(CONT’D)
Label Mnemonic Operand
STA SS
P RESW 1
Q RESW 1
R RESW 1
SS RESW 1
END

40
Exercise(3)
3. Assume that all variables are integers, and that elements of the
arrays W, U, and V of 50 elements each are stored in in consecutive
locations in memory starting at WBASE, UBASE, and VBASE
respectively. Write a SIC/XE assembly language program-segment to
compute:
W[i] := 15*U[i] + V[i]; for all values of i.

41
Solution

Label Mnemonic Operand


LDT #150
LDS #3
LDX #0
LOOP LDA #15
MUL UBASE,X
ADD VBASE,X
STA WBASE,X
ADDR S,X
COMPR X,T
JLT LOOP

42
Solution(CONT’D)

Label Mnemonic Operand


UBASE RESW 50
VBASE RESW 50
WBASE RESW 50

43
Solution(Complete Program)

Label Mnemonic Operand


PROG START 0
LDT #150
LDS #3
LDX #0
LOOP LDA #15
MUL UBASE,X
ADD VBASE,X
STA WBASE,X
ADDR S,X
COMPR X,T
JLT LOOP
44
Solution(Complete Program)(CONT’D)

Label Mnemonic Operand


UBASE RESW 50
VBASE RESW 50
WBASE RESW 50
END

45
Exercise(4)
4. There are 50 integers stored in consecutive locations starting at
DATA. Write an assembly program segment to replace each element
by double its value.

46
Solution
Label Mnemonic Operand
LDT #150
LDX #0
LDS #3
LOOP LDA #2
MUL DATA,X
STA DATA,X
ADDR S,X
COMPR X,T
JLT LOOP
DATA RESW 50

47
Solution(Complete Program)
Label Mnemonic Operand
PROG START 0
LDT #150
LDX #0
LDS #3
LOOP LDA #2
MUL DATA,X
STA DATA,X
ADDR S,X
COMPR X,T
JLT LOOP
DATA RESW 50
END 48
Exercise(5)
5. Given two string H and I, each of 10 characters. Write a SIC/XE
program to copy the contents of string H to I.

49
Solution
Label Mnemonic Operand
LDT #10
LDX #0
LDS #1
LOOP LDCH H,X
STCH I,X
ADDR S,X
COMPR X,T
JLT LOOP
H RESB 10
I RESB 10

50
Another Solution
Label Mnemonic Operand
LDT #10
LDX #0
LOOP LDCH H,X
STCH I,X
TIXR T
JLT LOOP
H RESB 10
I RESB 10

51
Solution(Complete Program)
Label Mnemonic Operand
PROG START 0
LDT #10
LDX #0
LDS #1
LOOP LDCH H,X
STCH I,X
ADDR S,X
COMPR X,T
JLT LOOP
H RESB 10
I RESB 10
END 52
Another Solution(Complete Program)
Label Mnemonic Operand
PROG START 0
LDT #10
LDX #0
LOOP LDCH H,X
STCH I,X
TIXR T
JLT LOOP
H RESB 10
I RESB 10
END

53
Exercise(6)
6. Suppose that Y is an array of 200 integers, write a sequence of
instructions to multiply each of the 200 elements by 3.

54
Solution
Label Mnemonic Operand
LDT #600
LDX #0
LDS #3
LOOP LDA #3
MUL Y,X
STA Y,X
ADDR S,X
COMPR X,T
JLT LOOP
Y RESW 200

55
Solution(Complete Program)
Label Mnemonic Operand
PROG START 0
LDT #600
LDX #0
LDS #3
LOOP LDA #3
MUL Y,X
STA Y,X
ADDR S,X
COMPR X,T
JLT LOOP
Y RESW 200
END 56
Exercise(7)
7. Given an integer array N of 100 elements. Write a SIC/XE code
snippet to execute the following (assume that i here represents
element number in one-based indexing):
Ni = Ni-1 + Ni+1 for i = 2, 3, 4, …..99.

57
Solution
Label Mnemonic Operand
LDT #297
LDX #3
LDS #3
LOOP LDA N-3,X
ADD N+3,X
STA N,X
ADDR S,X
COMPR X,T
JLT LOOP
N RESW 100

58
Solution(Complete Program)
Label Mnemonic Operand
PROG START 0
LDT #297
LDX #3
LDS #3
LOOP LDA N-3,X
ADD N+3,X
STA N,X
ADDR S,X
COMPR X,T
JLT LOOP
N RESW 100
END 59
Exercise(8)
8. Given two devices INDEV and OUTDEV specified by addresses F1
and 05, respectively (in hexadecimal). Write a SIC/XE code that
reads a character from INDEV and writes this character in OUTDEV
(Note that before writing/reading to/from a device, you need to
test whether the device is ready).

60
Solution
Label Mnemonic Operand
RLOOP TD INDEV
JEQ RLOOP
RD INDEV
WLOOP TD OUTDEV
JEQ WLOOP
WD OUTDEV
INDEV BYTE X’F1’
OUTDEV BYTE X’05’

61
Solution(Complete Program)
Label Mnemonic Operand
PROG START 0
RLOOP TD INDEV
JEQ RLOOP
RD INDEV
WLOOP TD OUTDEV
JEQ WLOOP
WD OUTDEV
INDEV BYTE X’F1’
OUTDEV BYTE X’05’
END

62
Exercise(9)
9. Suppose that DATA is an array of 100 integers, write code to set all
100 elements to double the value. That is; set DATA[i] := 2*DATA[i]
for i := 1, 2, . . . ,100

63
Solution
Label Mnemonic Operand
LDT #300
LDS #3
LDX #0
LOOP LDA DATA,X
MUL #2
STA DATA,X
ADDR S,X
COMPR X,T
JLT LOOP
DATA RESW 100

64
Solution(Complete Program)
Label Mnemonic Operand
PROG START 0
LDT #300
LDS #3
LDX #0
LOOP LDA DATA,X
MUL #2
STA DATA,X
ADDR S,X
COMPR X,T
JLT LOOP
DATA RESW 100
END
65
Exercise(10)
10. Suppose that DATA is an array of 150 integers, write a sequence of
instructions to store in NZERO the number of non-zero elements in
DATA.

66
Solution

Label Mnemonic Operand


LDA #0
STA NZERO
LDT #450
LDS #3
LDX #0
LOOP LDA DATA,X
COMP #0
JEQ NEXT
THEN LDA NZERO

67
Solution(CONT’D)

Label Mnemonic Operand


ADD #1
STA NZERO
NEXT ADDR S,X
COMPR X,T
JLT LOOP
NZERO RESW 1
DATA RESW 150

68
Solution(Complete Program)

Label Mnemonic Operand


PROG START 0
LDA #0
STA NZERO
LDT #450
LDS #3
LDX #0
LOOP LDA DATA,X
COMP #0
JEQ NEXT
THEN LDA NZERO

69
Solution(Complete Program)(CONT’D)

Label Mnemonic Operand


ADD #1
STA NZERO
NEXT ADDR S,X
COMPR X,T
JLT LOOP
NZERO RESW 1
DATA RESW 150
END

70
Exercise(11)
11. Write SIC/XE assembly language instructions corresponding to the
following code segment. Assume that all variables are binary
integers, and that the elements of the arrays Q and R (of 100
elements each) are allocated in consecutive words in memory
starting at QQ and RR respectively:
SUM := 0 ;
SUM := Q [i] x R [i] + SUM , for i := 1,2, .. , 100

71
Solution

Label Mnemonic Operand


LDA #0
STA SUM
LDT #300
LDS #3
LDX #0
LOOP LDA QQ,X
MUL RR,X
ADD SUM
STA SUM

72
Solution(CONT’D)

Label Mnemonic Operand


ADDR S,X
COMPR X,T
JLT LOOP
QQ RESW 100
RR RESW 100
SUM RESW 1

73
Solution(Complete Program)

Label Mnemonic Operand


PROG START 0
LDA #0
STA SUM
LDT #300
LDS #3
LDX #0
LOOP LDA QQ,X
MUL RR,X
ADD SUM
STA SUM

74
Solution(Complete Program)(CONT’D)

Label Mnemonic Operand


ADDR S,X
COMPR X,T
JLT LOOP
QQ RESW 100
RR RESW 100
SUM RESW 1
END

75
Exercise(12)
12. Given two arrays Q and P of 50 integers each, write assembly
language program segment to compute the assignment:
R = SUM of ( Q[i] * P[i] ) , for i = 1 to 50 .

76
Solution

Label Mnemonic Operand


LDA #0
STA SUM
LDT #150
LDS #3
LDX #0
LOOP LDA Q,X
MUL P,X
ADD SUM
STA SUM

77
Solution(CONT’D)

Label Mnemonic Operand


ADDR S,X
COMPR X,T
JLT LOOP
Q RESW 50
P RESW 50
SUM RESW 1

78
Solution(Complete Program)

Label Mnemonic Operand


PROG START 0
LDA #0
STA SUM
LDT #150
LDS #3
LDX #0
LOOP LDA Q,X
MUL P,X
ADD SUM
STA SUM

79
Solution(Complete Program)(CONT’D)

Label Mnemonic Operand


ADDR S,X
COMPR X,T
JLT LOOP
Q RESW 50
P RESW 50
SUM RESW 1
END

80

You might also like