Assembly Language Programming
Tracing programs written in the simplified ACSL assembly language.
Overview
Assembly language is a low-level programming language that provides a direct mapping to machine code instructions. It serves as an intermediate step between high-level programming languages and machine code (sequences of 1's and 0's).
High-Level Language → Assembly Language → Machine Language (Binary)
Importance in Modern Programming
- Helps programmers better understand compiler operations and constraints
- Essential for optimization when specific execution speed or space requirements must be met
- Provides direct control over hardware resources
ACSL Assembly Language
ACSL uses its own simplified assembly language to teach core concepts without the complexity of real-world assembly languages. While simplified, it maintains the fundamental concepts common to all assembly languages.
Key Concepts
Basic Program Structure
Every ACSL assembly program follows these key rules:
- Programs run from top to bottom, except for branch instructions (BG, BE, BL, BU)
- All operations use a special memory location called the "accumulator" (ACC)
- ACC always starts at 0
Program Line Structure
Each line has three parts:
LABEL OPCODE LOC
Understanding Each Part
- LABEL:
- Optional name for a line (like a bookmark)
- Must start with a letter (A-Z or a-z)
- Case-sensitive
- OPCODE:
- The instruction to execute
- Must be uppercase
- Reserved words - cannot be used as labels (in uppercase form)
- LOC:
- Can be either a label reference or a direct number
- When using direct numbers (like "123"), it's called "immediate data"
Instruction Reference Chart
| OPCODE | DESCRIPTION |
|---|---|
| LOAD* | Puts value from LOC into ACC |
| STORE | Saves ACC value to LOC |
| ADD* | Adds LOC to ACC and stores result in ACC (result ≤ 999,999) |
| SUB* | Subtracts LOC from ACC and stores result in ACC (result ≤ 999,999) |
| MULT* | Multiplies ACC by LOC and stores result in ACC (result ≤ 999,999) |
| DIV* | Divides ACC by LOC and stores whole number result in ACC |
| BG | Jump to LOC if ACC > 0 |
| BE | Jump to LOC if ACC = 0 |
| BL | Jump to LOC if ACC < 0 |
| BU | Jump to LOC unconditionally |
| READ | Input number into LOC |
| Output value in LOC | |
| DC | Define constant; assigns LOC to LABEL (requires LABEL) |
| END | Stop program |
* Instructions marked with asterisk can use immediate data
Examples
What is printed when the following program is executed?
| X | DC | 20 |
| LOAD | X | |
| DIV | =6 | |
| MULT | =5 | |
| SUB | X | |
| STORE | Y | |
| Y | ||
| END |
Trace the accumulator line by line:
X DC 20defines the constant X = 20. ACC is still 0.LOAD Xcopies 20 into ACC.DIV =6divides ACC by the immediate value 6. 20 ÷ 6 = 3.33..., and only the whole number is kept, so ACC = 3.MULT =5gives ACC = 3 × 5 = 15.SUB Xsubtracts X: 15 - 20 = -5. ACC = -5.STORE Ysaves -5 into Y.PRINT Youtputs -5.
Output: -5
Practice Problems
Problem 1 Junior
What is printed when the following program is executed?
| N | DC | 47 |
| LOAD | N | |
| DIV | =7 | |
| STORE | P | |
| MULT | =8 | |
| ADD | N | |
| DIV | =-30 | |
| ADD | P | |
| STORE | S | |
| S | ||
| END |
Solution
Let's solve this step by step:
- N is set to 47
- LOAD N puts the value of N (47) in ACC
- DIV =7 stores 6 in ACC (integer division of 47/7)
- STORE P saves 6 to the new variable P
- MULT =8 gives 48 in ACC (6*8)
- ADD N gives 95 in ACC (48+47)
- DIV =-30 gives -3 in ACC (95/-30)
- ADD P gives 3 in ACC (-3+6)
- STORE S saves 3 to S
- PRINT S outputs 3
Problem 2 Intermediate
What is output after this program is executed?
| A | DC | 36 |
| LOAD | A | |
| DIV | =8 | |
| STORE | B | |
| MULT | =10 | |
| STORE | C | |
| LOAD | B | |
| SUB | A | |
| STORE | D | |
| DIV | =9 | |
| MULT | =7 | |
| ADD | B | |
| STORE | E | |
| SUB | A | |
| DIV | =6 | |
| STORE | G | |
| G | ||
| END |
Solution
Let's solve this step by step:
- A is set to 36
- LOAD A puts 36 in ACC
- DIV =8 gives 4 (36/8 = 4.5, but we take integer part)
- STORE B saves 4 to B
- MULT =10 gives 40
- STORE C saves 40 to C
- LOAD B puts 4 in ACC
- SUB A gives -32 (4-36)
- STORE D saves -32 to D
- DIV =9 gives -3
- MULT =7 gives -21
- ADD B gives -17 (-21+4)
- STORE E saves -17 to E
- SUB A gives -53 (-17-36)
- DIV =6 gives -8
- STORE G saves -8 to G
- PRINT G outputs -8
Problem 3 Senior
What is the sum of the outputs of the following assembly language program after it is executed?
| A | DC | 4213 |
| B | DC | 16 |
| TOP | LOAD | A |
| DIV | B | |
| STORE | C | |
| BE | DOWN | |
| LOAD | C | |
| MULT | B | |
| STORE | E | |
| LOAD | A | |
| SUB | E | |
| STORE | F | |
| F | ||
| LOAD | C | |
| STORE | A | |
| BU | TOP | |
| DOWN | A | |
| END |
Solution
Let's solve this step by step:
- First iteration:
- A = 4213, B = 16
- 4213 ÷ 16 = 263 remainder 5
- C = 263
- E = 263 × 16 = 4208
- F = 4213 - 4208 = 5
- PRINT F outputs 5
- A becomes 263
- Second iteration:
- 263 ÷ 16 = 16 remainder 7
- C = 16
- E = 16 × 16 = 256
- F = 263 - 256 = 7
- PRINT F outputs 7
- A becomes 16
- Third iteration:
- 16 ÷ 16 = 1 remainder 0
- C = 1
- E = 1 × 16 = 16
- F = 16 - 16 = 0
- PRINT F outputs 0
- A becomes 1
- Fourth iteration:
- 1 ÷ 16 = 0 remainder 1
- C = 0
- BE DOWN executes (because C = 0)
- PRINT A outputs 1
- Sum of all outputs: 5 + 7 + 0 + 1 = 13