Contest Python: ACSL › Unit 15 — Assembly Language
Exercises: Unit 15 — Assembly Language
There are two kinds of exercises here, just as on an ACSL contest.
- Short-answer: work it out with pencil and paper and write one exact answer.
- The value in a location is a whole number, such as
-9. - For "what is printed?", write every printed number in order, on one line, separated by single spaces:
13 8 3. - Some questions give options labelled
A,B,C, …. Write the labels of every option that qualifies, in alphabetical order, separated by a comma and a space:A, C. If no option qualifies, writeNONE.
- The value in a location is a whole number, such as
- Programming: write a complete program that reads the input with
input()and prints exactly the output described. Save it as a.pyfile and run it from a terminal, typing the sample input or feeding it a file.
The rules are the ones in the lesson.
ACC starts at 0, every DC line sets its location before the program starts, and DIV keeps the whole-number part of ACC ÷ LOC, rounding toward zero (-7 DIV 2 is -3).
In every exercise, every number stays between −999,999 and 999,999, and no program divides by zero.
Assembly language is on the Intermediate and Senior papers only, so each exercise is marked Intermediate or Senior: the lowest division it belongs to. Do every exercise at or below your division. Exercises marked Challenge are extra practice for when you want more.
Exercise 1: Pocket Arithmetic
After this program runs, what number is in the location R?
X DC 7
Y DC -4
Z DC 5
R DC 0
LOAD X
ADD Y
MULT Z
SUB X
STORE R
END
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: a number
Exercise 2: Rounding Toward Zero
After this program runs, what number is in the location ANS?
P DC -17
Q DC 5
W DC 3
ANS DC 0
LOAD P
DIV Q
MULT W
ADD =4
DIV =2
STORE ANS
END
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: a number
Exercise 3: Immediate Data
What does this program print?
A DC 6
B DC 0
LOAD =10
SUB A
STORE B
MULT =-5
ADD B
STORE A
LOAD A
DIV B
STORE B
PRINT A
PRINT B
END
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: Every printed number in order, on one line, separated by single spaces, as in `13 8 3`.
Exercise 4: Straight-Line Translator
Write a Python program that does exactly what this assembly program does.
READ A
READ B
LOAD A
MULT A
SUB B
DIV =3
STORE C
PRINT C
LOAD B
SUB A
DIV =4
STORE D
PRINT D
END
The input is two whole numbers, A and then B, one per line.
Print the two numbers the assembly program prints, one per line, in the same order.
Remember that DIV rounds toward zero, which Python's // does not do for negative numbers.
Constraints
AandBare between −100 and 100.
Sample Input
4
9
Sample Output
2
1
With A = 3 and B = −7, the second number printed is -2: ACC is −10 before DIV =4, and −10 ÷ 4 = −2.5 rounds toward zero.
How this is checked: Checked by running your program on 8 test inputs (1 sample, 7 hidden) and comparing its output line by line.
Exercise 5: Which Way?
The input numbers are −7, 0 and 12, in that order. What does this program print?
K DC 3
LOOP READ N
LOAD N
BL NEG
BE ZERO
MULT =2
BU OUT
NEG MULT =-3
BU OUT
ZERO LOAD =100
OUT STORE R
PRINT R
LOAD K
SUB =1
STORE K
BG LOOP
END
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: Every printed number in order, on one line, separated by single spaces, as in `13 8 3`.
Exercise 6: Odd Squares
After this program runs, what number is in the location S?
I DC 1
S DC 0
TOP LOAD I
MULT I
ADD S
STORE S
LOAD I
ADD =2
STORE I
SUB =9
BL TOP
END
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: a number
Exercise 7: Countdown
What does this program print?
N DC 20
LOOP PRINT N
LOAD N
SUB =6
STORE N
BG LOOP
PRINT N
END
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: Every printed number in order, on one line, separated by single spaces, as in `13 8 3`.
Exercise 8: What Does It Print?
This program reads two whole numbers, X and then Y.
READ X
READ Y
LOAD X
SUB Y
BG POS
LOAD Y
SUB X
POS STORE D
PRINT D
END
Which of these describe what the program prints, for every pair of whole numbers X and Y between −1000 and 1000? Choose every one that does.
- A. the larger of
XandY - B. the distance between
XandYon the number line, |X−Y| - C.
X−Y - D. the larger of the two numbers
X−YandY−X - E.
0wheneverXis less thanY
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: The labels of every option that qualifies, in alphabetical order, separated by a comma and a space, as in `A, C`; `NONE` if no option qualifies.
Exercise 9: Read Until Zero
The input numbers are 8, −15, 3, −7, 0 and 9, in that order. What does this program print?
T DC 0
C DC 0
LOOP READ X
LOAD X
BE DONE
ADD T
STORE T
LOAD C
ADD =1
STORE C
BU LOOP
DONE LOAD T
DIV C
STORE A
PRINT C
PRINT A
END
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: Every printed number in order, on one line, separated by single spaces, as in `13 8 3`.
Exercise 10: Digit Loop Translator
Write a Python program that does exactly what this assembly program does.
S DC 0
READ N
LOOP LOAD N
DIV =10
STORE Q
MULT =10
STORE T
LOAD N
SUB T
ADD S
STORE S
LOAD Q
STORE N
BG LOOP
PRINT S
END
The input is one whole number N.
Print the number the assembly program prints.
Translate the program line by line; do not guess what it "means". Your program must print exactly what the assembly program prints for every allowed input, negative numbers included.
Constraints
Nis between −999,999 and 999,999.
Sample Input
1234
Sample Output
10
For N = 0 the program prints 0.
How this is checked: Checked by running your program on 8 test inputs (1 sample, 7 hidden) and comparing its output line by line.
Exercise 11: Accumulator Calculator
Write a program that runs a short assembly program that uses only immediate data, and prints ACC after every line.
The first line of input holds a whole number n.
Each of the next n lines holds one instruction: an opcode (LOAD, ADD, SUB, MULT or DIV), then one or more spaces, then = and a whole number, such as MULT =-6.
There are no labels.
ACC starts at 0.
After each instruction, print the value of ACC on its own line.
Constraints
nis between 1 and 20.- Every number in the input, and every value of
ACC, is between −999,999 and 999,999. - No
DIVhas=0.
Sample Input
5
LOAD =7
MULT =-6
ADD =3
DIV =8
SUB =10
Sample Output
7
-42
-39
-4
-14
-39 DIV 8 is -4, because −39 ÷ 8 = −4.875 rounds toward zero.
If the first instruction is DIV =3, the program prints 0, since ACC starts at 0.
How this is checked: Checked by running your program on 6 test inputs (1 sample, 5 hidden) and comparing its output line by line.
Exercise 12: Subtract and Swap
What does this program print?
A DC 84
B DC 36
C DC 0
LOOP LOAD C
ADD =1
STORE C
LOAD A
SUB B
BE DONE
BL SWAP
STORE A
BU LOOP
SWAP LOAD B
SUB A
STORE B
BU LOOP
DONE PRINT A
PRINT C
END
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: Every printed number in order, on one line, separated by single spaces, as in `13 8 3`.
Exercise 13: What Does It Compute?
This program reads one whole number N.
READ N
LOAD =1
STORE P
LOOP LOAD N
BE DONE
SUB =1
STORE N
LOAD P
ADD P
STORE P
BU LOOP
DONE PRINT P
END
Which of these describe what the program prints, for every whole number N from 0 to 19? Choose every one that does.
- A. 2 ×
N - B. 2 to the power
N(2 multiplied by itselfNtimes; it is 1 whenN= 0) - C.
N×N - D. the number you get by starting at 1 and doubling it
Ntimes - E.
N+ 1
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: The labels of every option that qualifies, in alphabetical order, separated by a comma and a space, as in `A, C`; `NONE` if no option qualifies.
Exercise 14: Halving
The input number is 22. What does this program print?
READ N
LOOP LOAD N
DIV =2
STORE Q
MULT =2
STORE T
LOAD N
SUB T
STORE D
PRINT D
LOAD Q
STORE N
BG LOOP
END
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: Every printed number in order, on one line, separated by single spaces, as in `13 8 3`.
Exercise 15: Loop Inside a Loop
What does this program print?
I DC 0
S DC 0
OUTER LOAD I
ADD =1
STORE I
SUB =5
BE DONE
LOAD I
STORE J
INNER LOAD S
ADD J
STORE S
LOAD J
SUB =1
STORE J
BG INNER
BU OUTER
DONE PRINT S
END
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: a number
Exercise 16: Halve or Triple Translator
Write a Python program that does exactly what this assembly program does.
C DC 0
READ N
LOOP LOAD N
SUB =1
BE DONE
LOAD N
DIV =2
MULT =2
SUB N
BE EVEN
LOAD N
MULT =3
ADD =1
BU NEXT
EVEN LOAD N
DIV =2
NEXT STORE N
LOAD C
ADD =1
STORE C
BU LOOP
DONE PRINT C
END
The input is one whole number N.
Print the number the assembly program prints.
Constraints
Nis between 1 and 1000. (For these inputs, every number the assembly program makes stays below 999,999.)
Sample Input
6
Sample Output
8
For N = 1 the program prints 0: ACC is 0 after SUB =1, so BE DONE jumps straight away.
How this is checked: Checked by running your program on 7 test inputs (1 sample, 6 hidden) and comparing its output line by line.
Exercise 17: Assembly Interpreter
Extend the interpreter of Lesson 3 so that it runs every ACSL opcode: DC, LOAD, STORE, ADD, SUB, MULT, DIV, BE, BG, BL, BU, READ, PRINT and END, with immediate data =value for LOAD, ADD, SUB, MULT and DIV.
The input is:
- one line with a whole number
n, the number of program lines - the
nlines of the program, in the column layout of the lesson (words separated by one or more spaces; a line with no label starts with spaces) - one line with a whole number
m, the number of input numbers mlines, each holding one input number, which the program'sREADlines read in order
Run the program from its first line until it reaches END, and print each number a PRINT line prints, on its own line.
The rules are the lesson's:
ACCstarts at 0.- A line's first word is its label exactly when it is not an opcode.
- Every
DCline sets its location before the program starts, even if it comes afterEND; when the program reaches aDCline, it just moves on. - A
STOREorREADinto a label with noDCline makes that location. DIVkeeps the whole-number part ofACC÷LOC, rounding toward zero.
Constraints
nis between 2 and 60, andmis between 0 and 20.- Labels start with a letter, go on with letters and digits, and are never opcodes. Each label is on at most one line, and every branch goes to a label that exists.
- A location is always set (by
DC,STOREorREAD) before it is read, and the program never reads more input numbers than there are. - Every value stays between −999,999 and 999,999, no
DIVdivides by zero, and the program reachesENDwithin 10,000 lines run. - The program prints at least one number.
Sample Input. This is ACSL's N! program with a PRINT added, run with the input 5.
11
READ X
LOAD X
TOP SUB =1
BE DONE
STORE A
MULT X
STORE X
LOAD A
BU TOP
DONE PRINT X
END
1
5
Sample Output
120
How this is checked: Checked by running your program on 11 test inputs (1 sample, 10 hidden) and comparing its output line by line.
Exercise 18: Trial by Division
What does this program print?
N DC 9
LIM DC 30
NEXT LOAD N
ADD =1
STORE N
SUB LIM
BG DONE
LOAD =2
STORE D
TRY LOAD D
MULT D
SUB N
BG PRIME
LOAD N
DIV D
MULT D
SUB N
BE NEXT
LOAD D
ADD =1
STORE D
BU TRY
PRIME PRINT N
BU NEXT
DONE END
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: Every printed number in order, on one line, separated by single spaces, as in `13 8 3`.