📖 Scenario: You are learning about how compilers translate programs into instructions that a computer can understand. To do this, the compiler uses a target machine model which describes the computer's features.Imagine you are designing a simple description of a computer's capabilities so a compiler can generate the right instructions.
🎯 Goal: Create a simple description of a target machine model using a dictionary. Then add configuration details, write the main logic to summarize the model, and finally complete the description with a summary statement.
📋 What You'll Learn
Create a dictionary called target_machine with keys: 'registers', 'memory_size', and 'instruction_set' with exact values
Add a variable called max_registers set to 8
Write code to create a list called supported_instructions containing only instructions with names longer than 3 characters
Add a final summary string called summary describing the target machine model
💡 Why This Matters
🌍 Real World
Compilers need to know the target machine's features to generate correct machine code that runs efficiently on that computer.
💼 Career
Understanding target machine models is important for compiler developers, systems programmers, and anyone working on low-level software optimization.
Progress0 / 4 steps
1
Create the target machine dictionary
Create a dictionary called target_machine with these exact entries: 'registers': 16, 'memory_size': 65536, and 'instruction_set': ['LOAD', 'STORE', 'ADD', 'SUB', 'JMP'].
Compiler Design
Hint
Use curly braces {} to create a dictionary. Keys are strings and values are numbers or lists.
2
Add max_registers configuration
Add a variable called max_registers and set it to 8.
Compiler Design
Hint
Just write max_registers = 8 on a new line.
3
Filter supported instructions
Create a list called supported_instructions that contains only the instructions from target_machine['instruction_set'] whose names have more than 3 characters.
Compiler Design
Hint
Use a list comprehension to filter instructions by length.
4
Add summary description
Add a string variable called summary that describes the target machine model as: 'Target machine has 16 registers, 65536 memory size, and supports 2 instructions longer than 3 characters.'
Compiler Design
Hint
Write the exact string inside single quotes and assign it to summary.
Practice
(1/5)
1. What does the target machine model primarily describe in compiler design?
easy
A. The hardware features and capabilities of the computer the compiler generates code for
B. The programming language syntax rules
C. The user interface design of the compiler
D. The network protocols used by the compiler
Solution
Step 1: Understand the role of target machine model
The target machine model defines the hardware features like instructions, registers, and memory layout that the compiler must consider.
Step 2: Differentiate from other compiler aspects
It is not about language syntax, UI, or network protocols, but about the machine the code will run on.
Final Answer:
The hardware features and capabilities of the computer the compiler generates code for -> Option A
Quick Check:
Target machine model = hardware features [OK]
Hint: Target machine model = hardware details compiler targets [OK]
Common Mistakes:
Confusing target machine model with programming language syntax
Thinking it describes compiler UI or network
Ignoring hardware specifics like registers and instructions
2. Which of the following is NOT typically part of a target machine model?
easy
A. Memory organization
B. Register set
C. Compiler error messages
D. Instruction set architecture
Solution
Step 1: Identify components of target machine model
It includes instruction set, registers, memory layout, and I/O methods.
Step 2: Recognize unrelated options
Compiler error messages relate to compiler diagnostics, not the machine model.
Final Answer:
Compiler error messages -> Option C
Quick Check:
Error messages ≠ machine model [OK]
Hint: Machine model = hardware parts, not compiler messages [OK]
Common Mistakes:
Including software diagnostics as machine features
Confusing compiler internals with hardware details
3. Consider a target machine model with 8 general-purpose registers and instructions that operate only on registers. What happens if a program tries to use more than 8 variables simultaneously?
medium
A. The compiler will generate code that uses memory to store extra variables
B. The program will crash at runtime
C. The compiler will ignore extra variables
D. The program will run faster
Solution
Step 1: Understand register limitation
The machine has only 8 registers, so only 8 variables can be held in registers at once.
Step 2: Compiler handling of extra variables
Extra variables are stored in memory (stack or heap) because registers are limited.
Final Answer:
The compiler will generate code that uses memory to store extra variables -> Option A
Quick Check:
More variables than registers -> use memory [OK]
Hint: More variables than registers? Use memory spill [OK]
Common Mistakes:
Assuming program crashes automatically
Thinking compiler ignores variables
Believing program runs faster with more variables
4. A compiler targeting a machine with 16-bit instructions tries to generate code using 32-bit instructions. What is the likely problem?
medium
A. The program will run twice as fast
B. The generated code will not run correctly due to instruction size mismatch
C. The compiler will automatically convert 32-bit to 16-bit instructions
D. The machine will ignore extra bits and run fine
Solution
Step 1: Check instruction size compatibility
The target machine supports only 16-bit instructions, so 32-bit instructions are invalid.
Step 2: Consequences of mismatch
Code with wrong instruction size will cause errors or incorrect execution on the machine.
Final Answer:
The generated code will not run correctly due to instruction size mismatch -> Option B
Quick Check:
Instruction size mismatch -> code fails [OK]
Hint: Instruction size must match machine specs [OK]
Common Mistakes:
Assuming machine auto-converts instructions
Thinking program runs faster with wrong size
Believing machine ignores extra bits
5. You need to optimize a compiler for a target machine model that has a limited number of registers but supports fast memory access. Which strategy best improves performance?
hard
A. Generate code without considering the target machine model
B. Ignore registers and use only memory for all variables
C. Use only registers and avoid memory completely
D. Use register allocation carefully and rely on memory for extra variables
Solution
Step 1: Analyze hardware constraints
The machine has few registers but fast memory access, so registers are limited but memory is efficient.
Step 2: Choose optimization strategy
Use registers for frequently accessed variables and spill others to memory to balance speed and capacity.
Final Answer:
Use register allocation carefully and rely on memory for extra variables -> Option D
Quick Check:
Balance registers and memory for best performance [OK]
Hint: Balance registers and memory use for optimization [OK]
Common Mistakes:
Ignoring registers completely
Using only registers beyond capacity
Not adapting code to machine features
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.