4.2 Assembly Language (3)
Resources |
Revision Questions |
Computer Science
Login to see all questions
Click on a question to view the answer
1.
Consider a simple assembly language instruction set with the following addressing modes: register addressing, direct addressing, and indirect addressing. Write assembly code to perform the following operations:
- Add the contents of register R1 to the contents of memory location 1000 and store the result in register R2.
- Load the value stored at memory location 2000 into register R3.
- Add the value stored at memory location 3000 to the value stored at memory location 2000 and store the result in register R4.
- Load the value at the address stored in register R5 into register R6.
Assume R1, R2, R3, R4, R5, and R6 are registers. Provide the assembly code for each operation, clearly indicating the addressing mode used.
Here's the assembly code for each operation:
ADD R2, 1000
(Direct addressing - 1000 is the memory address)
LOAD R3, 2000
(Direct addressing - 2000 is the memory address)
ADD R4, 3000
(Direct addressing - 3000 is the memory address)
ADD R4, R3
(Register addressing - R3 contains the value at 2000)
LOAD R6, [R5]
(Indirect addressing - R5 contains the memory address)
2.
The following assembly program calculates the sum of two numbers. The numbers are stored in memory locations 'num1' and 'num2'. Trace the execution, showing the register contents and memory contents at each step. Assume the program uses a single accumulator register (ACC) and a temporary register (TEMP). Assume the program starts with ACC = 0, TEMP = 0, num1 = 12, and num2 = 8.
LOAD num1, ACC ; Load the value from memory location num1 into ACC
LOAD num2, TEMP ; Load the value from memory location num2 into TEMP
ADD ACC, TEMP ; Add the value in TEMP to ACC
STORE ACC, RESULT ; Store the value in ACC to memory location RESULT
Execution Trace:
Initial State: ACC = 0, TEMP = 0, num1 = 12, num2 = 8, RESULT = (uninitialized)
LOAD num1, ACC: The value from memory location 'num1' (12) is loaded into the accumulator register (ACC).
- ACC = 12
- TEMP = 0
- num1 = 12
- num2 = 8
- RESULT = (uninitialized)
LOAD num2, TEMP: The value from memory location 'num2' (8) is loaded into the temporary register (TEMP).
- ACC = 12
- TEMP = 8
- num1 = 12
- num2 = 8
- RESULT = (uninitialized)
ADD ACC, TEMP: The value in the temporary register (8) is added to the value in the accumulator (12). The result (20) is stored back in the accumulator.
- ACC = 20
- TEMP = 8
- num1 = 12
- num2 = 8
- RESULT = (uninitialized)
STORE ACC, RESULT: The value in the accumulator register (20) is stored into the memory location labeled 'RESULT'.
- ACC = 20
- TEMP = 8
- num1 = 12
- num2 = 8
- RESULT = 20
Final State: ACC = 20, TEMP = 8, num1 = 12, num2 = 8, RESULT = 20
3.
Explain how the choice of addressing mode can impact the efficiency of a program. Discuss scenarios where one addressing mode would be preferred over another, considering factors such as speed, memory usage, and code size. Provide specific examples to illustrate your points.
The choice of addressing mode significantly impacts program efficiency. Speed: Register addressing is generally the fastest because it avoids memory access. Direct and indirect addressing involve memory accesses, which take more time. Memory Usage: Immediate addressing can lead to larger code sizes if large constants are used. Direct and indirect addressing require more bits to specify the memory address. Code Size: Immediate addressing can result in larger instructions. Indirect addressing can also increase code size due to the need for an extra register to store the address.
Scenarios:
- Immediate addressing is preferred when dealing with frequently used constants, such as mathematical constants (pi, e) or loop counters. This avoids repeated memory accesses. Example: Calculating the area of a circle where pi is a constant.
- Direct addressing is preferred when accessing data stored at fixed memory locations, such as variables in a small data structure. Example: Accessing elements in an array where the base address is known.
- Indirect addressing is preferred when accessing data stored in dynamic data structures like linked lists or tables where the memory address is not known at compile time. Example: Traversing a linked list where each node contains a pointer to the next node. Also useful for implementing function pointers.
- Register addressing is preferred when frequently accessing data that is already stored in a register. This avoids the overhead of memory access. Example: Performing calculations using variables that are stored in registers.
In summary, the optimal addressing mode depends on the specific task and the trade-offs between speed, memory usage, and code size. A good compiler will choose the most efficient addressing mode for each instruction.