0% found this document useful (0 votes)
23 views

GROUP (1)

GROUP (1)

Uploaded by

ezekiel nyamu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

GROUP (1)

GROUP (1)

Uploaded by

ezekiel nyamu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

THE UNITED AFRICAN UNIVERSITY OF TANZANIA

BACHELOR OF SCIENCE IN COMPUTER ENGINEERING AND


INFORMATION TECHNOLOGY
COURSE CODE : CS214
SUBJECT : MICRO COMPUTER SYSTEM
LECTURER : MR.NYAMU
ASSIGNMENT : GROUP ASSIGNMENT
DEAD LINE : 11ST DEC 2024

STUDENTS NAME REGISTRATION NUMBERS


CHARITY JOHN 23011027
CALVIN MREMA 23011029
MICHAEL MWELA 23011028
TIMOTHEO CHILAGI 23011026
1. Task 1.1: Essential Components of an Instruction Set
An instruction set is the collection of instructions a processor can execute. These
instructions form the building blocks of assembly language programs. Here are the essential
components and their impact on the structure and functionality of assembly language:
Components of an Instruction Set
Opcode (Operation Code):
o Specifies the operation to be performed.

o Examples:

 Intel x86: ADD, SUB, MUL, MOV.


 ARM: LDR (Load Register), STR (Store Register).
o Impact: Determines the functionality of the instruction and influences
program design.
Operands:
o Data values or addresses on which the operation is performed.

o Types:

 Register-based (e.g., AX in x86).


 Memory-based (e.g., [1234H]).
 Immediate values (e.g., 5).
o Impact: Flexibility in operand types enhances the range of operations.

Instruction Format:
o Defines how the instruction is encoded.

o Intel x86:

 ADD AX, BX (adds values in BX to AX).


o Impact: Compact encoding allows efficient program execution.

Addressing Modes:
o Determines how operands are accessed (e.g., direct, indirect, immediate).

o Example: MOV AX, [1234H] (direct addressing).


o Impact: Enhances program efficiency by enabling different ways to access
data.
Instruction Length:
o Number of bytes in an instruction (e.g., 1-byte, 2-byte instructions).

o Impact: Longer instructions can handle more complex operations but may
reduce execution speed.
Control Flow:
o Instructions like JMP, CALL, and RET manage program flow.

o Impact: Enables the creation of loops, functions, and conditional logic.

Example from Modern Processors


 Intel x86:
o Opcode: ADD for addition.

o Operand: AX, BX to add contents of BX to AX.

o Addressing Modes: Supports direct (MOV AX, [1234H]), indirect (MOV AX,
[BX]), and immediate (MOV AX, 5).
 ARM:
o Opcode: LDR (load data to a register).

o Operand: R0, [R1] (load from memory address in R1 to R0).


Task 1.2: Assembly Program with Five Instruction Types
This example demonstrates data transfer, arithmetic, logic, control, and comparison
instructions:

Explanation of Each Instruction


Data Transfer (MOV):
o Purpose: Transfers data between memory and registers.

o Example: mov al, [num1] loads num1 into AL.

Arithmetic (ADD):
o Purpose: Performs addition or subtraction.

o Example: add al, bl adds num2 to num1.

Logic (AND):
o Purpose: Performs bitwise operations (AND, OR, XOR, NOT).

o Example: and al, 0x0F masks the upper nibble of AL.

Comparison (CMP):
o Purpose: Compares two values and sets flags (e.g., Zero Flag, Sign Flag).

o Example: cmp al, 15 compares AL with 15.

Control (JMP/JE):
o Purpose: Alters the flow of execution.

o Example: je equal_label jumps to equal_label if AL == 15


Task 2.1: Concept of Addressing Modes
Definition
Addressing modes specify how the operands of an instruction are accessed. They determine
whether the operand is a constant, in a register, or in memory, and how to locate it if it's in
memory.
Significance :
Flexibility: Enable various ways of accessing data, improving program adaptability.
Efficiency: Allow compact code by optimizing data access.
Performance: Reduce unnecessary instructions or memory accesses.
Readability: Help organize code to be more understandable.

Common Addressing Modes


Immediate Addressing :
o The operand is specified directly within the instruction.

o Example: MOV AL, 5 (Load the constant 5 into register AL).

Direct Addressing:
o The instruction specifies the memory address where the operand is located.

o Example: MOV AX, [1234H] (Load data from memory address 1234H into
AX).
Indirect Addressing:
o The address of the operand is stored in a register.

o Example: MOV AX, [BX] (Load data from the memory address pointed to by
BX).
Register Addressing:
o The operand is in a register.

o Example: MOV AL, BL (Move data from BL to AL).

Indexed Addressing:
o Combines a base address with an offset.

o Example: MOV AL, [BX+SI] (Load data from an address calculated by


adding BX and SI).
Base-Plus-Offset Addressing:
o Adds a constant offset to a base address.

o Example: MOV AX, [BP+4] (Load data from an address offset 4 bytes from
BP).

Task 2.2: Assembly Program Demonstrating Addressing Modes


Below is an assembly program demonstrating direct, indirect, and immediate addressing
modes.
Explanation of Addressing Modes in the Code
Immediate Addressing:
o Example: mov al, 5
o The constant value 5 is loaded directly into the AL register.
o Use Case: Useful for small constants in calculations.
Direct Addressing:
o Example: mov al, [num1]
o The value stored at the memory address of num1 (10) is loaded into AL.
o Use Case: Efficient for accessing global or static data.
Indirect Addressing:
o Example: lea si, [num1] and mov al, [si]
o The address of num1 is loaded into the SI register, and then the value at that
address is accessed.
o Use Case: Ideal for traversing arrays or dynamic memory.

Program Output
The program:
1. Adds 5 and 3 using immediate addressing.
2. Adds num1 (10) and num2 (20) using direct addressing.
3. Repeats the addition using indirect addressing.
4. Stores the results in result and temp.
1 Task 3.1: Factorial Program
Problem Statement
Write an assembly program to compute the factorial of a given number (e.g., n = 5) using
simple arithmetic instructions.

Step-by-Step Explanation
i. Initialize Data:
o num: The number for which the factorial is to be calculated (e.g., 5).
o result: A variable to store the result.
ii. Load the Number:
o mov cl, [num]: Load the value of num into the CL register.
o mov ax, 1: Initialize AX to 1, as the factorial computation starts with 1.
iii. Factorial Loop:
o Multiply: mul cl multiplies the current value of AX with CL.
o Decrement: dec cl decrements CL by 1.
o Check Condition: jnz factorial_loop jumps back to the loop if CL is not zero.
iv. Store Result:
o mov [result], ax saves the factorial result in memory.
v. End Program:
o mov ah, 0x4C and int 0x21 terminate the program.

Flowchart

Initialize AX = 1, CL = num

Multiply AX = AX * CL

Decrement CL

Is CL == 0?

Yes No

End
Task 3.2: Logic Operations
Problem Statement
Create an assembly program to demonstrate AND, OR, XOR, and NOT operations.

1. Step-by-Step Explanation
Operands:
o operand1: Binary value 01011010.
o operand2: Binary value 10100101.
AND Operation:
o and al, [operand2]: Performs bitwise AND of operand1 and operand2.
OR Operation:
o or al, [operand2]: Performs bitwise OR of operand1 and operand2.
XOR Operation:
o xor al, [operand2]: Performs bitwise XOR of operand1 and operand2.
NOT Operation:
o not al: Inverts all bits of operand1

Flowchart for Logic Operations

Load operand1

Perform AND

Perform OR

Perform XOR

Perform NOT

Store Results

End
Task 4.1: Sum of Integers from 1 to N
Loops are essential in assembly programming for performing repetitive tasks. This section
demonstrates programs for summing integers and generating multiplication tables using
loops.
Problem Statement
Write an assembly program to calculate the sum of integers from 1 to N (e.g., N = 10) using a
loop.

Explanation
Initialize Registers:
o CX acts as the counter, initialized to N.
o AX serves as the accumulator to store the running sum.
Loop Structure:
o Add: add ax, cx adds the current counter value to the accumulator.
o Decrement: dec cx reduces the counter by 1.
o Jump: jnz sum_loop repeats the loop until CX = 0.
Store Result:
o The final value in AX is stored in sum.

Task 4.2: Multiplication Table


Problem Statement :
Write an assembly program with nested loops to generate a multiplication table for numbers 1
to 10.
Assembly Code
Explanation
Outer Loop (Rows):
o Starts with CL = 1 and increments up to 10.
o Each iteration represents a new row in the multiplication table.
Inner Loop (Columns):
o Starts with DL = 1 for each row.
o Multiplies AL (row number) by DL (column number).
o Increments DL until it reaches the table limit (10).
Result:
o The multiplication result is stored in AX.
o Modify the code to store or print the results
Task 5.1: Methods for Data Transfer Between CPU and Memory
Data transfer is a fundamental operation in assembly language where data is moved between
the CPU and memory. Various instructions and techniques allow efficient data transfer,
depending on the use case.
Methods of Data Transfer
Direct Memory Access (DMA):
o Transfers data directly between memory and peripherals without CPU
intervention.
o Efficient for bulk transfers but requires specialized hardware.
Register-Memory Transfer:
o Data is transferred between CPU registers and memory.
o Example: MOV AX, [1234H] loads data from memory into AX.
Memory-Memory Transfer:
o Data is transferred directly between two memory locations.
o Example: Implemented indirectly using registers.
I/O-Mapped Transfer:
o Uses I/O ports for data transfer.
o Example: IN and OUT instructions.

Relevant Instructions for Data Transfer


MOV:
o Transfers data between registers, memory, and immediate values.
o Example: MOV AX, [1234H].
PUSH/POP:
o Transfers data to/from the stack.
o Example: PUSH AX pushes AX onto the stack.
LODS/STOS:
o Used for string operations:
 LODS: Load string element into AL.
 STOS: Store AL into the destination.
REP MOVSB/MOVSW:
o Transfers blocks of data.
o Example: REP MOVSB copies a block of bytes from source to destination.

Significance
 Efficiency: Optimized instructions for bulk transfers reduce execution time.
 Flexibility: Multiple addressing modes enhance usability.
 Performance: Efficient data movement minimizes CPU overhead.
Task 5.2: Assembly Program for Block Data Transfer
Problem Statement
Write an assembly program to transfer a block of data from one memory location (source) to
another (destination).

Explanation of Program Logic


i. Initialization:
o ESI points to the source memory block.
o EDI points to the dest memory block.
o ECX is the counter initialized to the number of bytes to transfer.
ii. Data Transfer:
o LODSB:
 Loads a byte from [ESI] into AL.
 Increments ESI to point to the next byte.
o STOSB:
 Stores the byte from AL into [EDI].
 Increments EDI to point to the next destination byte.
o LOOP:
 Decrements ECX and jumps to transfer_loop if ECX != 0.
iii. Result:
o After the loop, the data from source is copied to dest.

Memory Layout After Execution


 Before:
o source: "HELLO, WORLD!"
o dest: Undefined/empty.
 After:
o source: "HELLO, WORLD!"
o dest: "HELLO, WORLD!"

Task 6.1: Concept of Interrupts in Assembly Language


Definition
Interrupts are signals to the processor that temporarily halt the current execution flow to
handle an event or a condition. Once the event is handled, the processor resumes its original
task. Interrupts can be hardware-based or software-based.
Types of Interrupts
I. Hardware Interrupts:
o Generated by external hardware devices (e.g., keyboard, mouse, timer).
o Example: The keyboard generates an interrupt when a key is pressed, which
can be handled to read the key press.
II. Software Interrupts:
o Triggered by a program instruction (e.g., int 0x80 in x86 architecture).
o Example: System calls in operating systems, such as int 0x80 for Linux
system calls.
Role and Importance
 Responsive Systems: Interrupts allow programs to respond to external events in real-
time, making systems more responsive.
 Multitasking: Enables CPU to switch between tasks efficiently, contributing to
multitasking capabilities.
 Resource Management: Helps manage hardware resources, ensuring devices work
correctly without constant CPU polling.
 Critical Operations: Vital for operations requiring immediate attention, such as
handling input/output (I/O) operations.

Task 6.2: Assembly Program for a User-Defined ISR


 Problem Statement
Write an assembly program that implements a user-defined interrupt service routine (ISR) to
handle a keyboard interrupt.
 Explanation of Interrupts and ISRs
 Interrupts: Triggered by hardware or software to signal the CPU to pause the current
program and execute an ISR.
 ISR: A piece of code that handles the interrupt, processes the event, and returns
control to the original program.

Program to Handle a Keyboard Interrupt


This example program sets up an ISR to handle keyboard input interrupts using the x86
architecture.
Explanation of Code Components
i. Setting Up the Interrupt Vector:
o The interrupt vector table entry for IRQ1 (keyboard interrupt) is set up using
the int 0x21 service.
o The address of the ISR is loaded into DX, and ah is set to 0x21 for interrupt
vector setup.
ii. Main Program:
o The hlt instruction halts the CPU, waiting for an interrupt.
o jmp main_loop ensures the program runs indefinitely until interrupted.
iii. ISR Routine:
o pusha saves all general-purpose registers to protect their values.
o mov al, [0x60] reads the key code from the keyboard buffer (0x60 is the
keyboard data port).
o int 0x10 is used to print the character to the screen (BIOS teletype function).
o popa restores register values before returning.
o iret ends the ISR and returns control to the interrupted program.
 How It Works
 When a key is pressed, the CPU receives an interrupt signal (IRQ1).
 The CPU pauses the current process and jumps to the ISR defined at the interrupt
vector for keyboard input.
 The ISR reads the key code, prints the character, restores the state, and returns control
using iret.

Task 7.1: Assembly Program for a Simple Calculator


The program will:
 Use keyboard interrupts to read user input.
 Use arithmetic operations to perform calculations.
 Implement loops to handle continuous input.
 Use data transfer to move data between registers and memory.
Program Logic and Explanation
i. Initial Prompt:
o The program prompts the user to enter an operation (+, -, *, /) using sys_write.
ii. Reading Input:
o The read_input function reads a single character from the keyboard and stores
it in memory.
iii. Arithmetic Operations:
o The program reads two numbers and determines which operation to perform
based on user input:
 Addition (+): Adds the two numbers.
 Subtraction (-): Subtracts the second number from the first.
 Multiplication (*): Multiplies the two numbers.
 Division (/): Divides the first number by the second.
iv. Data Transfer:
o The program uses mov to transfer data between registers and memory
locations.
v. Printing Results:
o The print_result function displays the result to the console.
Task 7.2: Documentation and Challenges
Documentation of Program Logic
 The program uses interrupts (int 0x80) for system calls, such as sys_write and
sys_read, to display text and read input.
 Registers (e.g., AX, BL, BH) are used for arithmetic operations and data transfer.
 The program loops back to the start after each operation to allow continuous use.
Challenges and Solutions
i. Challenge: Handling non-numeric input or invalid operations.
o Solution: Implemented a check to exit the program if the input is not
recognized.
ii. Challenge: ASCII to integer conversion.
o Solution: Used sub al, '0' to convert input characters to integers.
iii. Challenge: Division by zero.
o Solution: Added error handling to manage attempts to divide by zero (not
shown in this simplified version).
References
1. Stallings, W. (2010). Computer Organization and Architecture: Designing for
Performance. Pearson.
2. Irvine, K. R. (2013). Assembly Language for x86 Processors. Pearson.
3. Patterson, D. A., & Hennessy, J. L. (2013). Computer Organization and Design.
Elsevier.

You might also like