Micro_computer_assignment_group1
Micro_computer_assignment_group1
1
Contents
Task 1.1.................................................................................................................................................... 4
Essential Components of an Instruction Set ............................................................................................ 4
How These Components Shape Assembly Language Programs? ............................................................ 5
Task 1.2.................................................................................................................................................... 6
Assembly Language Program .................................................................................................................. 6
Explanation of Each Instruction ............................................................................................................... 7
Task 2.1.................................................................................................................................................... 8
Investigating Addressing Modes in Assembly Language ......................................................................... 8
Significance in Efficient Programming ..................................................................................................... 8
Examples from Modern Processors......................................................................................................... 9
Task 2.2.................................................................................................................................................. 10
Assembly Language Program ................................................................................................................ 10
Explanation of Addressing Modes. ........................................................................................................ 11
Task 3.1.................................................................................................................................................. 12
Assembly Language Program ................................................................................................................ 12
Step-by-Step Explanation ...................................................................................................................... 13
Task 3.2.................................................................................................................................................. 15
Assembly Language Program ................................................................................................................ 15
Pseudocode. .......................................................................................................................................... 16
Flowchart............................................................................................................................................... 17
Task 4.1.................................................................................................................................................. 18
Assembly Language Program ................................................................................................................ 18
Documentation of the Loop Structure and Control............................................................................... 19
Task 4.2.................................................................................................................................................. 21
Assembly Language Program ................................................................................................................ 21
Explanation of Nested Loops. ................................................................................................................ 23
Task 5.1.................................................................................................................................................. 24
Methods for Data Transfer .................................................................................................................... 24
Relevant Instructions in Assembly Language. ....................................................................................... 25
Conclusion ............................................................................................................................................. 26
Task 5.2.................................................................................................................................................. 26
Assembly Language Program ................................................................................................................ 26
Explanation of Program Logic and Execution ........................................................................................ 27
Task 6.1.................................................................................................................................................. 29
Concept of Interrupts in Assembly Language. ....................................................................................... 29
2
Role and Importance in System Operation ........................................................................................... 29
Types of Interrupts. ............................................................................................................................... 30
Example of an Interrupt in Assembly Language. ................................................................................... 30
Explanation ............................................................................................................................................ 31
Task 6.2.................................................................................................................................................. 32
Assembly Language Program ................................................................................................................ 32
Task 7.1.................................................................................................................................................. 35
Project: Number Guessing Game in Assembly Language. ..................................................................... 36
Assembly Language Program ................................................................................................................ 36
Task 7.2.................................................................................................................................................. 41
Project: Guess the Number Game in Assembly Language..................................................................... 41
Program Logic. ....................................................................................................................................... 41
Assembly Language Progra.................................................................................................................... 42
Flowchart............................................................................................................................................... 46
Challenges Faced and Solutions. ........................................................................................................... 47
references.............................................................................................................................................. 48
3
Task 1.1:
Identify and describe the essential components of an instruction set. Discuss how these components
shape the structure and functionality of assembly language programs. Include examples from
modern processors.
- Description: The opcode specifies the operation to be performed by the processor. It's
essentially the command part of the instruction, like ADD for addition or MOV for data
transfer.
- Example: In the x86 architecture, `ADD eax, ebx` adds the values in registers `eax` and
`ebx`.
2. Operand(s):
- Description: Operands are the data items that the instruction manipulates. They can be
immediate values, registers, or memory addresses.
- Example: In `MOV eax, [ebx+4] `, `eax` and `[ebx+4] ` are operands, where `eax` is a
register and `[ebx+4] ` is a memory address.
3. Addressing Mode:
- Description: Addressing modes specify how the operand is accessed. Common modes
include immediate, register, direct, and indirect addressing.
- Example: `MOV eax, [ebx]` uses indirect addressing, where the memory address is
determined by the value in `ebx`.
4. Instruction Format:
4
- Example: In the RISC-V architecture, a typical instruction format might include 7 bits for
the opcode, 5 bits for each of the two source registers, and 5 bits for the destination register.
5. Instruction Length:
- Description: This is the size of the instruction, which can be fixed or variable. RISC
architectures generally use fixed-length instructions, while CISC architectures may have
variable-length instructions.
- Addressing Modes: These provide flexibility in how instructions access data, allowing for
more complex data structures and efficient use of memory. Indirect and indexed addressing
modes, for example, enable efficient access to array elements.
- Instruction Format and Length: These factors influence the complexity of the assembly
language. A simpler, fixed-format instruction set (like in RISC architectures) leads to more
predictable performance and easier optimization. Conversely, the more complex, variable-
length instructions in CISC architectures allow for more powerful and compact code.
5
Task 1.2:
Create an assembly language program that demonstrates at least five distinct types of instructions,
such as data transfer, arithmetic, logic, control, and comparison. Document and explain the purpose
of each instruction used.
section .bss
section .text
global _star
_start:
mov al, [num1] ; Move the value at address num1 (10) into register al
; Arithmetic Instruction
; Logic Instruction
and al, 0Fh ; Perform a bitwise AND on al with 0Fh (binary: 00001111)
; Comparison Instruction
; Control Instruction
je equal ; Jump to label 'equal' if the comparison was equal (zero flag set)
6
; Move the result to the reserved memory location
equal:
- Purpose: Move the value stored at memory address `num1` (which is 10) into the `al`
register.
- Explanation: This instruction transfers data from a memory location to a register. It's
essential for loading values into registers for further processing.
- Purpose: Add the immediate value 5 to the value in the `al` register.
- Purpose: Perform a bitwise AND operation on the contents of the `al` register with `0Fh`.
- Explanation: Logic instructions like AND are used to manipulate specific bits within a
byte or word. This operation is often used for masking certain bits.
7
- Purpose: Compare the value in the `al` register with the immediate value 15.
- Explanation: The CMP instruction sets the processor's flags based on the result of the
comparison. It's used for decision-making in programs.
- Purpose: Jump to the label `equal` if the zero flag is set (i.e., if `al` equals 15).
- Explanation: Control instructions like JUMP direct the flow of execution in a program.
This allows for conditional execution of code blocks based on previous comparisons or
calculations.
Task 2.1:
Investigate and explain the concept of addressing modes in assembly language. Highlight their
significance in efficient programming with examples.
- Memory Efficiency: Using appropriate addressing modes can minimize memory usage and
increase the efficiency of memory accesses. This is especially important in embedded
systems and applications with limited resources.
8
- Code Readability and Maintenance: Proper use of addressing modes can make the code
more readable and easier to maintain. For example, using indexed addressing for arrays
makes it clear that you are accessing array elements.
```assembly
```
2. Register Addressing:
```assembly
```
3. Direct Addressing:
```assembly
```
4. Indirect Addressing:
```assembly
```
5. Indexed Addressing:
```assembly
MOV AX, [BX + SI] ; Move value from address (BX + SI) to AX
```
9
6. Base-Indexed Addressing:
```assembly
```
Task 2.2:
Write an assembly program that incorporates and demonstrates direct, indirect, and immediate
addressing modes. Provide a detailed explanation for the functionality and usage of each mode
within your program
section .data
section .bss
section .text
global _start
_start:
mov bl, [num1] ; Move the value at address of num1 (10) into the BL register
mov ecx, num2 ; Load the address of num2 into the ECX register
mov dl, [ecx] ; Move the value at the address in ECX (20) into the DL register
10
; Perform an operation using the loaded values
mov [result], al ; Move the value in AL (35) into the result variable
```
- Functionality: Moves the immediate value `5` directly into the `AL` register.
- Usage: Immediate addressing is used when you need to work with a constant value. It is
fast because the value is embedded directly in the instruction.
- Example: Here, `5` is the immediate value, and it is moved into `AL`.
- Functionality: Moves the value stored at the memory address `num1` (which is `10`) into
the `BL` register.
- Example: The value `10` at the address `num1` is directly moved into `BL`.
11
3. Indirect Addressing Mode:
- Functionality: First, the address of `num2` is moved into the `ECX` register. Then, the
value at the address contained in `ECX` (which is `20`) is moved into the `DL` register.
- Example: The value `20` at the address `num2` is accessed indirectly through the `ECX`
register and moved into `DL`.
Task 3.1:
Develop an assembly program to compute the factorial of a given number using simple
arithmetic instructions. Include a step-by-step explanation of your code.
section .data
section .text
global _start
_start:
; Initialize result to 1
12
mov cx, 1 ; Move 1 to CX register
factorial_loop:
; Decrement BL
dec bl ; BL = BL - 1
```
Step-by-Step Explanation.
1. Data Section:
- `num db 5`: This defines a byte-sized variable `num` with the value 5. You can change
this value to compute the factorial of a different number.
2. Text Section:
13
- The program starts with `_start`, which is the entry point.
- `mov al, [num]`: Load the value of `num` (5) into the `AL` register.
- `mov bl, al`: Copy the value from `AL` to `BL`. `BL` will be used as a counter in the loop.
4. Initialize Result:
- `mov cx, 1`: Initialize the `CX` register to 1. This will hold the intermediate results of the
factorial calculation.
5. Factorial Loop:
- `mul cx`: Multiply `AL` (which holds the current number) by `CX`. The result is stored in
`AX`.
- `jg factorial_loop`: If `BL` is greater than 1, jump back to `factorial_loop` to continue the
calculation.
- `mov [result], cx`: Store the final factorial result from `CX` into the `result` variable.
- `mov eax, 1`: Prepare the system call number for exiting the program.
14
Task 3.2
: Create an assembly program that performs the logic operations: AND, OR, XOR, and NOT.
Accompany your program with a flowchart or pseudocode to clarify the logic behind each
operation.
section .data
section .bss
result_and resb 1
result_or resb 1
result_xor resb 1
result_not resb 1
section .text
global _start
_start:
mov [result_and], al
; Perform OR operation
or al, [val2]
mov [result_or], al
15
mov al, [val1]
mov [result_xor], al
not al
mov [result_not], al
; Exit program
```
Pseudocode.
```plaintext
4. Perform OR operation:
16
b. OR register A with val2
b. NOT register A
```
Flowchart.
Here's a flowchart to visualize the logic operations:
1. Start
17
5. Perform OR operation
- OR A with val2
- NOT A
Task 4.1
: Write an assembly program that calculates the sum of integers from 1 to NNN using a loop.
Document how the loop is structured and controlled within the program.
section .data
section .bss
section .text
global _start
_start:
18
; Initialize registers
calculate_sum:
mov [sum], bx
```
- Purpose: Initialize the `CX` register to 1. This register serves as the counter, starting from
1.
- Purpose: Initialize the `BX` register to 0. This register will be used to accumulate the sum.
19
- Instruction: `calculate_sum:`
- Purpose: This label marks the beginning of the loop. It is used as a reference point for the
loop control instructions.
3. Addition Operation:
- Purpose: Add the current value of `CX` (the counter) to `BX` (the accumulator). This
operation updates the sum by adding the current counter value.
4. Increment Counter:
- Purpose: Increment the `CX` register by 1. This updates the counter for the next iteration
of the loop.
5. Comparison:
- Purpose: Compare the current value of `CX` with `N`. This checks if the counter has
reached the upper limit.
6. Loop Control:
- Purpose: If `CX` is less than or equal to `N`, jump back to the `calculate_sum` label to
repeat the loop. This ensures that the loop continues as long as the counter is within the
desired range.
20
- Instruction: `mov [sum], bx`
- Purpose: After the loop completes, store the final sum from `BX` into the `sum` variable.
- Purpose: These instructions perform a system call to exit the program with exit code 0.
Task 4.2:
Design an assembly program with nested loops to generate a multiplication table for
numbers 1 to 10. Include comments to explain the logic of each nested loop.
section .data
section .bss
result resb 100 ; Reserve space for storing the multiplication table (10x10)
section .text
global _start
_start:
outer_loop:
21
; Inner loop: Iterate over columns (1 to 10)
inner_loop:
mul bx ; AX = row * 10
; Store result
inc dx ; DX = DX + 1
end_inner_loop:
inc cx ; CX = CX + 1
end_outer_loop:
22
; Exit the program
```
- Compare the counter with 11. The loop runs as long as `cx` is less than 11.
- If the counter is greater than or equal to 11, exit the outer loop.
2. Inner Loop:
- Compare the counter with 11. The loop runs as long as `dx` is less than 11.
23
- If the counter is greater than or equal to 11, exit the inner loop.
- Index Calculation: `mov bx, 10`, `mul bx`, `add ax, dx`, `dec ax`
- Calculate the zero-based index for storing the result in the `result` array.
- Store the result of the multiplication in the `result` array at the calculated index.
- System Call: `mov eax, 1`, `xor ebx, ebx`, `int 0x80`
Task 5.1:
Examine and explain the methods for data transfer between the CPU and memory in
assembly language. Provide examples of relevant instructions used.
24
- Description: This method allows peripherals to access the memory directly, bypassing the
CPU. It's used for high-speed data transfer, such as disk drives or network interfaces.
- Example: The CPU sets up the DMA controller to handle the data transfer and then goes
into a wait state or continues executing other instructions.
2. Programmed I/O:
- Description: In this method, the CPU is responsible for data transfer between memory and
peripherals. The CPU reads from or writes to memory one word at a time.
- Example: When transferring data from memory to an I/O device, the CPU executes
instructions to read data from memory and write it to the device.
3. Memory-Mapped I/O:
- Description: Peripherals are assigned specific memory addresses. The CPU uses standard
memory instructions to transfer data between memory and these addresses.
- Example: The CPU can read from or write to a peripheral by accessing its assigned
memory address.
- Description: The MOV instruction is used to transfer data between registers, from a
memory location to a register, from a register to a memory location, or between two memory
locations.
- Description: These instructions are used to transfer data between registers and the stack.
- Description: These string manipulation instructions are used for block data transfer.
25
- Description: These instructions are used to transfer data between an I/O port and the CPU.
Conclusion.
Data transfer between the CPU and memory is a crucial aspect of assembly language
programming. Various methods, such as direct memory access, programmed I/O, and
memory-mapped I/O, facilitate efficient data transfer.
Task 5.2
: Develop an assembly program that transfers a block of data from one memory location to
another. Include a detailed explanation of the program logic and its execution.
section .data
section .bss
section .text
global _start
_start:
mov esi, source ; ESI points to the start of the source data
mov edi, dest ; EDI points to the start of the destination data
transfer_loop:
26
mov al, [esi] ; Load byte from source into AL
```
- `source db 'Hello, world!'`: Defines the source data as the string "Hello, world!".
- `length equ $ - source`: Calculates the length of the source data, which is 13 bytes.
2. BSS Section:
- `dest resb 13`: Reserves 13 bytes for the destination data, where the source data will be
copied.
3. Text Section:
4. Initialization:
27
- `mov esi, source`: Load the address of the source data into the `ESI` register. `ESI` will
serve as the source pointer.
- `mov edi, dest`: Load the address of the destination data into the `EDI` register. `EDI` will
serve as the destination pointer.
- `mov ecx, length`: Load the length of the source data into the `ECX` register. `ECX` will
serve as the loop counter, holding the number of bytes to transfer.
5. Transfer Loop:
- `mov al, [esi]`: Load a byte from the source memory location pointed to by `ESI` into the
`AL` register.
- `mov [edi], al`: Store the byte from the `AL` register into the destination memory location
pointed to by `EDI`.
- `inc esi`: Increment the source pointer (`ESI`) to point to the next byte in the source data.
- `inc edi`: Increment the destination pointer (`EDI`) to point to the next byte in the
destination data.
- `jnz transfer_loop`: If the loop counter (`ECX`) is not zero, jump back to the
`transfer_loop` label to repeat the loop. This ensures that the loop continues until all bytes are
transferred.
- `mov eax, 1`: Prepare the system call number for exiting the program.
28
Task 6.1:
Research and describe the concept of interrupts in assembly language, emphasizing their role
and importance in system operations.
- Importance: They ensure timely processing, crucial in real-time systems where delays can
lead to system failures or data loss.
- Importance: This improves CPU efficiency by reducing idle times and allowing
multitasking.
3. Prioritization:
- Description: Interrupts can be prioritized. Critical tasks can preempt less important ones,
allowing the system to handle high-priority tasks immediately.
29
- Importance: This ensures that crucial tasks are handled first, enhancing system stability
and performance.
4. System Stability:
- Importance: This contributes to the overall reliability and robustness of the system.
Types of Interrupts.
1. Hardware Interrupts:
2. Software Interrupts:
3. Exceptions:
- Description: Generated by the CPU to signal error conditions or special instructions (e.g.,
division by zero, invalid opcode).
30
```assembly
section .data
section .text
global _start
_start:
; Exit program
```
Explanation.
1. Message Data Section:
31
- `msg db 'Hello, world!', 0xA`: Defines the message to be printed, ending with a newline
character.
2. Text Section:
- `mov eax, 4`: Move the system call number for `sys_write` into the `EAX` register.
- `mov ebx, 1`: Move the file descriptor for `stdout` (1) into the `EBX` register.
- `mov ecx, msg`: Move the pointer to the message into the `ECX` register.
- `mov edx, len`: Move the length of the message into the `EDX` register.
- `int 0x80`: Trigger the software interrupt to call the kernel and execute the `sys_write`
system call.
- `mov eax, 1`: Move the system call number for `sys_exit` into the `EAX` register.
- `xor ebx, ebx`: Clear the `EBX` register to set the exit code to 0.
- `int 0x80`: Trigger the software interrupt to call the kernel and exit the program.
Task 6.2:
Write an assembly program that implements a user-defined ISR to handle a keyboard
interrupt. Provide comprehensive comments and documentation to explain each part of your
code.
32
section .data
section .bss
section .text
global _start
_start:
int 0x21
mov ds, ax
int 0x21
hang:
33
jmp hang
new_isr:
in al, 0x60 ; Read the scan code from the keyboard controller
segment .data
segment .bss
key resb 1
segment .text
global _start
_start:
int 0x21
34
; Set the new ISR for interrupt 0x09
mov ds, ax
int 0x21
; Loop indefinitely
jmp $
new_isr:
pusha
in al, 0x60
mov [key], al
int 0x21
popa
```
Task 7.1:
Develop a complex assembly program that integrates concepts such as arithmetic operations,
loops, data transfer, and interrupts. Examples include a basic calculator, an LED control
system, or a simple game.
35
Project: Number Guessing Game in Assembly Language.
section .data
newline db 10, 0
seed dw 1234
section .bss
user_guess resb 1
target_num resb 1
section .text
global _start
_start:
out 0x70, al
call generate_random
mov [target_num], al
main_loop:
; Display prompt
36
mov ah, 0x09
int 0x21
call read_number
mov [user_guess], al
je correct_guess
ja too_high
jb too_low
too_high:
int 0x21
jmp main_loop
too_low:
int 0x21
37
jmp main_loop
correct_guess:
int 0x21
; Exit program
call exit_program
generate_random:
int 0x1A
mul dx
div dx
inc dx
mov al, dl
ret
read_number:
38
sub al, '0' ; Convert ASCII to numeric
ret
exit_program:
; Exit program
mov eax, 1
int 0x80
```
1. Data Section:
- `prompt db 'Guess the number (1-100): $'`: Prompt message for user input.
- `high_msg db 'Too high! Try again.$', 10`: Message for when the guess is too high.
- `low_msg db 'Too low! Try again.$', 10`: Message for when the guess is too low.
- `correct_msg db 'Congratulations! You guessed it!$'`: Message for when the guess is
correct.
2. BSS Section:
3. Text Section:
39
- Initialization:
- `mov ax, [seed]`, `out 0x70, al`: Initialize the seed for random number generation.
- Main Loop:
- Display Prompt:
- `mov ah, 0x09`, `lea dx, [prompt]`, `int 0x21`: Display the prompt message for user
input.
- `cmp al, [target_num]`: Compare the user's guess with the target number.
- `je correct_guess`: If the guess is equal to the target number, jump to `correct_guess`.
- `ja too_high`: If the guess is greater than the target number, jump to `too_high`.
- `jb too_low`: If the guess is less than the target number, jump to `too_low`.
- Feedback Messages:
- Too High:
- `mov ah, 0x09`, `lea dx, [high_msg]`, `int 0x21`: Display the too high message.
- Too Low:
- `mov ah, 0x09`, `lea dx, [low_msg]`, `int 0x21`: Display the too low message.
- Correct Guess:
40
- `mov ah, 0x09`, `lea dx, [correct_msg]`, `int 0x21`: Display the congratulations
message.
- Exit Program:
4. Subroutines:
- read_number: Reads a character from the keyboard and converts it from ASCII to
numeric.
Task 7.2
: Document your project in detail, including program logic, flowcharts, and a report on
challenges faced and how they were addressed.
Program Logic.
1. Initialization:
- Generate a random number between 1 and 100 (for simplicity, we'll use a hard-coded
number).
41
- Provide feedback (too high, too low, correct).
3. Game Loop:
- Repeat the process until the user guesses the correct number.
section .data
section .bss
section .text
global _start
_start:
; Display prompt
42
int 0x21
guess_loop:
call read_number
mov [guess], al
je correct_guess
jl too_low
too_high:
int 0x21
jmp guess_loop
too_low:
43
mov ah, 0x09
int 0x21
jmp guess_loop
correct_guess:
int 0x21
; Exit program
mov eax, 1
int 0x80
read_number:
ret
```
1. Data Section:
- `target db 50`: The target number for the game (hard-coded for simplicity).
44
- `prompt db 'Guess the number (1-100): $'`: Prompt message for the user's guess.
- `high_msg db 'Too high! Try again.$'`: Message for when the guess is too high.
- `low_msg db 'Too low! Try again.$'`: Message for when the guess is too low.
- `correct_msg db 'Congratulations! You guessed it!$'`: Message for when the guess is
correct.
2. Text Section:
- Initialization:
- `mov ah, 0x09`, `lea dx, [prompt]`, `int 0x21`: Display the prompt message for the user's
guess.
- Guess Loop:
- `cmp al, [target]`: Compare the user's guess with the target number.
- `je correct_guess`: If the guess is equal to the target, jump to the correct guess message.
- `jl too_low`: If the guess is less than the target, jump to the too low message.
- Too High:
- `mov ah, 0x09`, `lea dx, [high_msg]`, `int 0x21`: Display the too high message.
- `jmp guess_loop`: Jump back to the guess loop to prompt the user again.
- Too Low:
- `mov ah, 0x09`, `lea dx, [low_msg]`, `int 0x21`: Display the too low message.
- `jmp guess_loop`: Jump back to the guess loop to prompt the user again.
- Correct Guess:
45
- `mov ah, 0x09`, `lea dx, [correct_msg]`, `int 0x21`: Display the correct guess message.
- Exit Program:
- `mov eax, 1`, `xor ebx, ebx`, `int 0x80`: Exit the program using a system call.
3. Subroutines:
- read_number: Reads a character from the keyboard and converts it from ASCII to
numeric.
Flowchart.
Here's a flowchart to visualize the program logic:
```plaintext
+ +
| Start |
+ +
+ +
| Display Prompt |
+ +
+ +
| Read Guess |
46
+ +
+ +
| Target |
+ +
+ + +
| | |
lt eq gt
| | |
v v v
+ + + +
+ + |
| v
+ >+ +
| Exit Program |
+ +
```
47
- Challenge: Ensuring valid numeric input from the user.
- Solution: Reading single characters and converting them from ASCII to numeric values,
limiting the input to digits only.
- Challenge: Continuously prompting the user until the correct guess is made.
- Solution: Using a loop structure with conditional jumps (`je`, `jl`, `jmp`) to control the
flow based on the user's input and comparison results.
3. Feedback Display:
- Challenge: Providing real-time feedback to the user based on their guess (too high, too
low, correct).
- Solution: Displaying appropriate messages using BIOS interrupts and branching to the
correct parts of the program based on the comparison results.
references
Smith, S. (2020). Programming with 64-Bit ARM Assembly Language: Single Board
Computer Development for Raspberry Pi and Mobile Devices. Apress.
48