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, write NONE.
  • Programming: write a complete program that reads the input with input() and prints exactly the output described. Save it as a .py file 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

  • Division: Intermediate

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

For parents and teachers: report a problem

Exercise 2: Rounding Toward Zero

  • Division: Intermediate

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

For parents and teachers: report a problem

Exercise 3: Immediate Data

  • Division: Intermediate

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`.

For parents and teachers: report a problem

Exercise 4: Straight-Line Translator

  • Division: Intermediate

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

  • A and B are 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.

For parents and teachers: report a problem

Exercise 5: Which Way?

  • Division: Intermediate

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`.

For parents and teachers: report a problem

Exercise 6: Odd Squares

  • Division: Intermediate

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

For parents and teachers: report a problem

Exercise 7: Countdown

  • Division: Intermediate

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`.

For parents and teachers: report a problem

Exercise 8: What Does It Print?

  • Division: Intermediate

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 X and Y
  • B. the distance between X and Y on the number line, |X − Y|
  • C. X − Y
  • D. the larger of the two numbers X − Y and Y − X
  • E. 0 whenever X is less than Y

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.

For parents and teachers: report a problem

Exercise 9: Read Until Zero

  • Division: Intermediate

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`.

For parents and teachers: report a problem

Exercise 10: Digit Loop Translator

  • Division: Intermediate

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

  • N is 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.

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Exercise 11: Accumulator Calculator

  • Division: Intermediate

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

  • n is between 1 and 20.
  • Every number in the input, and every value of ACC, is between −999,999 and 999,999.
  • No DIV has =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.

For parents and teachers: report a problem

Exercise 12: Subtract and Swap

  • Division: Senior

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`.

For parents and teachers: report a problem

Exercise 13: What Does It Compute?

  • Division: Senior

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 itself N times; it is 1 when N = 0)
  • C. N × N
  • D. the number you get by starting at 1 and doubling it N times
  • 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.

For parents and teachers: report a problem

Exercise 14: Halving

  • Division: Senior

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`.

For parents and teachers: report a problem

Exercise 15: Loop Inside a Loop

  • Division: Senior

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

For parents and teachers: report a problem

Exercise 16: Halve or Triple Translator

  • Division: Senior

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

  • N is 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.

For parents and teachers: report a problem

Exercise 17: Assembly Interpreter

  • Challenge
  • Division: Senior

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:

  1. one line with a whole number n, the number of program lines
  2. the n lines 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)
  3. one line with a whole number m, the number of input numbers
  4. m lines, each holding one input number, which the program's READ lines 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:

  • ACC starts at 0.
  • A line's first word is its label exactly when it is not an opcode.
  • Every DC line sets its location before the program starts, even if it comes after END; when the program reaches a DC line, it just moves on.
  • A STORE or READ into a label with no DC line makes that location.
  • DIV keeps the whole-number part of ACC ÷ LOC, rounding toward zero.

Constraints

  • n is between 2 and 60, and m is 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, STORE or READ) 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 DIV divides by zero, and the program reaches END within 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.

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Exercise 18: Trial by Division

  • Challenge
  • Division: Senior

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`.

For parents and teachers: report a problem

Back to Unit 15 — Assembly Language