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

Group 4 Microcomputer systems assignment (1)

Group 4 Microcomputer systems assignment (1

Uploaded by

ezekiel nyamu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Group 4 Microcomputer systems assignment (1)

Group 4 Microcomputer systems assignment (1

Uploaded by

ezekiel nyamu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

THE UNITED AFRICAN UNIVERSITY OF TANZANIA

PROGRAM: BACHELOR IN COMPUTER ENGINEERING AND INFORMATION TECHNOLOGY.

COURSE NAME: MICRO COMPUTER SYSTEMS 1

COURSE INSTRUCTOR: MR NYAMU

COURSE CODE: CS214

TASK: GROUP ASSIGNMENT/ PRESENTATION

NAMES ID NUMBER
EMMANUEL MALAMU RUKOMWA 23011004
SALLY ROY FIGUEIREDO 23011023
THOMAS JUMA MATIKO 23011019
JONATHAN ANDREA KUNJU 23011011
HAMIDU ABUBAKAR 23011006

1
Contents
INTRODUCTION .....................................................................................................................3
Task 1: Instruction Set ..............................................................................................................3
Task 1.1 ...................................................................................................................................3
Task 1.2 ...................................................................................................................................6
Task 2: Addressing Modes ...................................................................................................... 10
Task 2.1 ................................................................................................................................. 10
Task 2.2 ................................................................................................................................. 11
Task 3: Simple Arithmetic and Logic Operations .................................................................. 15
Task 3.1 ................................................................................................................................. 15
Task 3.2 ................................................................................................................................. 17
Task 4: Loops........................................................................................................................... 22
Task 4.1 ................................................................................................................................. 22
Task 4.2 ................................................................................................................................. 23
Task 5: Data Transfer Between CPU and Memory ................................................................ 27
Task 5.1 ................................................................................................................................. 27
Task 5.2 ................................................................................................................................. 28
Task 6: Programming Interrupts and User-Defined Interrupt Service Routines (ISRs) ..... 31
Task 6.1 ................................................................................................................................. 31
Task 6.2 ................................................................................................................................. 32
Task 7 :Comprehensive Project .............................................................................................. 36
Task 7.1 ................................................................................................................................. 36
Task 7.2 ................................................................................................................................ 37
CONCLUSION ....................................................................................................................... 41
REFERENCES ........................................................................................................................ 42

2
INTRODUCTION
Assembly language is often called low level language which gives the most practical approach to
the working of a hardware system. It has a very thin relationship with high level programmed
source and machine code. The programming languages are such that one could write extremely
efficient hardware-specific programs. This assignment contains the core contents of assembly
languages which are instruction set, addressing mode, arithmetic operations, loops and interrupts.
Each of the exercises contains some aspect of assembly programming with further elaboration
and demonstration within it.

Task 1: Instruction Set


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.

Answer for Task 1.1


An instruction set is an assemblage of commands which could be executed by the
process. These commands are the basic primitive constructs for assembly language
programming. Below are the necessary components of an instruction set:

1. Instruction Format
The instruction format specifies command structure.
Generally includes opcode (operation code), operands, and addressing modes.

 Opcode or Operation Code


The term operation code more generally relates simply to an opcode that describes
part of an incoming unique machine language instruction, which instructs the
computer to perform some unique operation. It tells what action is to be performed by
the processor,

3
i.e., moving data, adding values, jumping to some other location in memory.
Purpose: Specifies what actions the processor will perform.
Examples:
MOV: Moves Data
ADD: Adds
CMP: Compares
JMP: Jumps

 Operands
The portion of a computer instruction that determines the specific object to be
manipulated or acted upon was a processor register, a memory address, a literal
constant, or a label[3][4]. Data or Values on which the operation acts.
Purpose: Define Input/Output for operation.
Examples:
Register: MOV AX, BX (Moves Value from BX to AX).
Memory: MOV BX, [5678h] (Loads the value from memory address 5678h into
register B)
Immediate MOV: AX, 15 (load the constant into AX).

 Addressing Modes
This refers to the methods adapted by a processor for locating the operand (data or
instruction) in memory or registers. They describe how the instructions specify the
source or destination of data from one memory or register to the other [1][3].
Purpose: To provide flexibility while accessing data.
Types:
Immediate: ADD AX, 100 (Add the immediate value 100 to AX)
Register: ADD SP, BP (Add the value in BP to SP)

4
Direct: SUB BX, [ABCDh] (Subtract the value from memory location ABCDh from
BX)
Indirect: MOV AX,[BX](Take memory address stored in BX).

Example: In x86 the instruction MOV EAX, 1 uses the MOV opcode to transfer.

2. Data Types
Specifies types of data that can be processed by the instruction: Integers, floating point,
characters, etc.
Example: Nowadays, they also have 32-bit or 64-bit integers and operations. Moreover,
the floating-point type is included.

3. Addressing Modes
It tells how a processor knows the location of its operands.
Example: Immediate, register, direct, indirect, indexed.
 Immediate addressing mode: In this mode, the operand is a constant value
embedded within the instruction itself. EXAMPLE: MOV AL, 12 (the value 12
goes directly into AL Register).
 Register addressing mode: The operand is almost in registers. EXAMPLE: MOV
AX, BX (whatever was in BX is transferred to AX).
 Direct addressing mode: The address of operand is explicitly given within the
instruction code. EXAMPLE: MOV AL, [1234h] (the content in 1234h memory
location is copied into the AL Register).
 Indirect addressing mode: The address of operand is contained within some
register. EXAMPLE: MOV AX, [BX] (Move the value from the memory address
stored in the register BX into the register AX)
 Indexed addressing mode: The address of the operand is obtained by adding to the
base address an offset. EXAMPLE: MOV AX, [SI + 5] moves whatever value is
contained in memory at the location [SI + 5] into register AX.
Example: ARM uses both immediate and register addressing for almost
everything.

5
4. Instruction Classifications
Generally, different types of instructions correspond to specific tasks:
Data Transfer Instructions (MOV, LOAD STORE, etc.),

Arithmetic Instructions (ADD, SUB, etc.),


Logic Instructions (AND, OR, XOR),
Control Flow Instructions (JMP CALL RET),
Comparison Instructions (CMP, TEST).

5. Registers
Internally storage places in processors used for operations. For example, x86 processors
have general-purpose registers (EAX, EBX), while ARM uses R0-R15.

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.

Answer for Task 1.2

6
Explanation of Assembly Code

7
The description is as follows.

Initialization: Initializes the data segment.

Data Transfer: Transfers the value of num1 into register AL.

Arithmetic: It adds num2 to AL.

Logic: It performs an AND operation into AL.

Comparison: It compares AL with num2.

Control: Jumps to greater when AL is greater than num2.

8
Store Result: AL is moved into result.

9
Task 2: Addressing Modes
Task 2.1
Investigate and explain the concept of addressing modes in assembly language. Highlight their
significance in efficient programming with examples.

The different modes of direct addressing’s how the operand of an instruction is selected in
assembly language. The flexibility and ease of accessing data thus allow one to program at
extremely high levels. Some of these addressing modes have provided the following
specifications along with examples:
 Immediate Addressing Mode:
The object operand is a constant value that is known to be directly embedded by the
instruction. This mode allows the instruction to operate on constants without needing to
access memory.
Example: MOV AX, 15 (Here 15 is obviously immediate operand)

 Direct Addressing Mode:


An address of the operand is supplied within the instruction itself as an explicit part.This
mode allows direct access to a specific memory location, conveying it from the address
as:
Example : MOV AX, [1234H] (Here 1234H is the immediate operand's address)

 Indirect Addressing Mode:


The address of the operand is often located in a register or memory location. This mode
allows the flexibility of accessing operands whose addresses can be changed dynamically.
Example: MOV AX, [BX] (In this one, the operand store its address in this BX register)

 Register Addressing Mode:


The operand is a register data. This mode provides the fastest access to operands since they
are directly available in the CPU registers
Example.: MOV AX, BX (In this sense, the operand lies in register BX)

10
 Indexed Addressing Mode:
Combine the base address and index for accessing an operand. This mode is useful for
accessing elements in arrays or other data structures.
Example: MOV AX, [BX+SI] (here, BX refers to a base and SI refers to index).

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.

11
Detailed Explanation:

1. Initialization:

 mov ax, @data: Load the data segment address into AX.

 mov ds, ax: Move the address from AX to DS to initialize the data segment.

12
2. Immediate Addressing Mode:

mov al, 5: The immediate value 5 is directly provided in the instruction. The value 5 is moved
into the AL register.

3. Direct Addressing Mode:

mov bl, [num1]: The address of num1 is explicitly specified in the instruction. The value
stored at memory location num1 is moved into the BL register.

4. Indirect Addressing Mode:

 lea si, [num2]: Load the effective address of num2 into the SI register.
 mov cl, [si]: The address of the operand is held in the SI register. The value stored
at the memory address pointed to by SI (i.e., value of num2) is moved into the CL
register.

5. Performing Operations:

 add al, bl: Add the value in BL (loaded using direct addressing) to AL (loaded
using immediate addressing).

13
 add al, cl: Add the value in CL (loaded using indirect addressing) to AL.

6. Storing the Result:

mov result, al: Store the result in the memory location result.

7. Exit Program:

 mov ax, 4C00h: Prepare to exit the program.


 int 21h: Interrupt to terminate the program.

14
Task 3: Simple Arithmetic and Logic Operations
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.

Here is the stepwise explanation of the code:


Data Segment:
 num db 7: The factorial will be calculated for 7, as num is set to 7.
 result db 1: Stores the final calculated factorial, initialized to 1.

15
Text Section:
 Load the Value of num: mov al, [num]: Loads the value of 7 into register AL. This holds
the current value for multiplication.

 Initializing the Counter: mov bl, al: Copies the value in AL (7) to BL, which serves as a
counter for the factorial loop.

 Initial Value: mov dl, 1: Initializes DL to 1, which will hold the current result of the
factorial computation.

Factorial Calculation Loop:


 New Value Multiplier: mul bl: Multiplies the current value in AX (initially 1)
by BL (starting at 7) and stores the result in AX.
 Decrement the Counter: dec bl: Decreases BL by 1 to move through the factorial
sequence (7 to 6, etc.).
 Check if Loop Continues: cmp bl, 0: Compares BL to 0.
 Continue Loop: jne _factorial_loop: If BL is not 0, the loop continues.

16
The program multiplies the current result by each integer from 7 down to 1, storing the
final factorial value in result.

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.

17
Explanation of the Code

Section.data

 operand1 and operand2 are initialized with the hexadecimal values 0x3C (60 in
decimal) and 0xA5 (165 in decimal), respectively.

 result is a variable initialized to 0x00, and it will be used to store the outcome of the
logical operations.

18
AND operation:

 mov al, [operand1]: Moves the value of operand1 (60 or 0x3C) into the AL
register.

 and al, [operand2]: Performs a bitwise AND between the value in AL


and operand2 (165 or 0xA5).

 Bitwise AND results in 0x3C & 0xA5 = 0x04 (in decimal, 60 AND 165 = 4).

 mov [result], al: The result (0x04) is stored in the result variable.

OR operation:

 mov al, [operand1]: Loads operand1 (60 or 0x3C) into the AL register.

 or al, [operand2]: Performs a bitwise OR between the value in AL


and operand2 (165 or 0xA5).

 Bitwise OR results in 0x3C | 0xA5 = 0xBF (in decimal, 60 OR 165 = 191).

 mov [result], al: The result (0xBF) is stored in the result variable.

19
XOR operation:

 mov al, [operand1]: Loads operand1 (60 or 0x3C) into the AL register.

 xor al, [operand2]: Performs a bitwise XOR between the value in AL


and operand2 (165 or 0xA5).

 Bitwise XOR results in 0x3C ^ 0xA5 = 0x99 (in decimal, 60 XOR 165 = 153).

 mov [result], al: The result (0x99) is stored in the result variable.

NOT operation:

 mov al, [operand1]: Loads operand1 (60 or 0x3C) into the AL register.

 not al: Performs a bitwise NOT operation on the value in AL, inverting all the
bits.

 The result is the 8-bit complement of 0x3C, which is 0xC3 (in decimal, NOT 60 =
195).

 mov [result], al: The result (0xC3) is stored in the result variable.

Exit

 mov eax, 1: The value 1 is loaded into the eax register, which corresponds to
the sys_exit system call in Linux. This is used to exit the program.

20
 int 0x80: This is the interrupt instruction that triggers the system call, effectively ending
the program's execution.

21
Task 4: Loops
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.

Explanation of the code:


Loop Structure and Control
1. Initialization: The program begins by initializing the counter register ECX with the value
N and setting the accumulator register EAX to 0.

2. Loop Start: The label loop_start marks the beginning of the loop.

22
3. Addition: Inside the loop, the ADD instruction adds the value of ECX to EAX.

4. Decrement and Jump: The LOOP instruction automatically decrements ECX and checks
if ECX is not zero. If ECX is not zero, it jumps back to loop_start.

5. End of Loop: When ECX reaches zero, the loop exits.

6. Store Result: The result in EAX is stored in the sum variable.

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.

23
Explanation of logic of each nested loop:

Data Section

This section defines constants for the table's width and height.

24
BSS Section

This section allocates space for the multiplication table. resb 100 reserves 100 bytes (10x10
table).

Text Section

Initialization:

 Sets ecx to 0, which will be used as the row index i.

Outer Loop (next_row):

 Copies the row index i to eax.

 Initializes ebx to 0, which will be used as the column index j.

Inner Loop (next_col):

 Multiplies eax (row index i) and ebx (column index j), storing the result in edx.

 Moves the base address of the table to esi.

 Stores the result (dl, the lower byte of edx) at the calculated memory position in the table.

 Increments the column index j.

 Compares ebx (column index) to 10. If ebx is less than 10, it jumps back to next_col to
continue the inner loop.

 Increments the row index i.

25
 Compares ecx (row index) to 10. If ecx is less than 10, it jumps back to next_row to
continue the outer loop.

Exit:

 Prepares to exit the program.

 eax is set to 60, which is the syscall number for exit.

 edi is set to 0, the exit code.

 Executes the syscall to terminate the program.

26
Task 5: Data Transfer Between CPU and Memory
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.

Different Methods of Data Transfer As Between CPU and Memory in Assembly


Language

Basically, the transfer between the CPU and memory is an elementary operation that goes
on in the assembly language, strictly necessary for executing instructions and
manipulating data. All CPU architectures use various methods and instructions in these
transfers.

Types of Data Transfer Instructions


 Register to Register Transfer
This involves moving data between registers within the CPU.
Example: The MOV instruction can be used as follows:

 Register to Memory Transfer


Data is transferred from a register to a specific memory location.
Example:

 Memory to Register Transfer


This operation retrieves data from a memory location into a register.
Example:

27
 Memory to Memory Transfer
Data is transferred directly between two memory locations.
Example:

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.

Explanation of the Code:

Data and BSS Sections:

 section .data is used to declare initialized data (in this case, the source string 'Hello,
World!').
 section .bss is used to declare uninitialized data (we define a buffer dest of the same
length as the source).

28
Text Section:

 Global _start:

 The _start label is the entry point of the program.

 Registers Setup:

 mov esi, src: Load the address of the source data into the ESI register.

 mov edi, dest: Load the address of the destination buffer into the EDI register.

 mov ecx, len: Load the length of the data into the ECX register.

 Data Transfer:

 rep movsb: This instruction moves a byte from the address pointed to by ESI to
the address pointed to by EDI, and then increments ESI and EDI and decrements
ECX. It repeats the operation until ECX is zero.

 Exit System Call:

 mov eax, 60: Load the syscall number for exit.

 xor edi, edi: Set the exit status to 0.

 syscall: Make the system call to exit the program.

29
This is a basic example that demonstrates the use of string instructions and the basic flow of an
assembly program. This specific code copies the string "Hello, World!" from one memory
location to another.

30
Task 6: Programming Interrupts and User-Defined Interrupt Service Routines (ISRs)
Task 6.1
Research and describe the concept of interrupts in assembly language, emphasizing their
role and importance in system operations.

Concept of Interrupts in Assembly Language


Interrupts are signals that prompt the CPU to halt its current tasks and address a specific
event. They enable efficient system operations by allowing quick interactions between
hardware and software.

Definition and Functionality:


An interrupt is a signal indicating the need for immediate CPU attention. When an
interrupt occurs, the CPU suspends its task, saves its state, and executes the Interrupt
Service Routine (ISR). After the ISR finishes, control returns to the interrupted task.

Types of Interrupts:
 Hardware Interrupts: Generated by external devices (e.g., keyboard, mouse) to signal the
CPU for processing.
 Software Interrupts: Triggered by programs to request services from the operating system
(e.g., system calls).
 Traps: Generated by the CPU in response to errors or specific instructions (e.g., division
by zero).

Importance of Interrupts:
 Efficiency: Interrupts allow prompt responses without continuous polling, enhancing
system performance.
 Multitasking: They enable task switching based on priority, facilitating multitasking.
 Resource Management: Interrupts help manage resources by notifying the CPU of device
status.

31
Handling Interrupts:
IRQ: A device sends an IRQ signal to the CPU.
Context Saving: The CPU saves its current state.
Executing ISR: The CPU identifies and executes the appropriate ISR.
Completion: After the ISR, the CPU restores its state and resumes the interrupted task.

In conclusion, interrupts are vital for efficient system operations, multitasking, and
resource management, making them essential in low-level programming and assembly
language.

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
33
Explanation of Code

Section .data: It will be the data segment containing the message to be printed when there is
keyboard interrupt.

Section .bss: It describes the uninitialized data section. This is not utilized in this example other
than saving it for future use in declaring any uninitialized variables.

Section .text: It defines the code section where the executable codes reside.

global _start: Entry point for the program.

_start: Main entry point of the program which sets the keyboard interrupt ISR with enabling and
disabling interrupts in stopping the processor so that the interrupt can be awaited.

isr_keyboard: This is the interrupt service routine associated specifically with the keyboard
interrupt. It saves general-purpose registers then sends End Of Interrupt (EOI) signal to the

34
Programmable Interrupt Controller (PIC), prints a string, restores and returns registers to the
interrupt.

print_string: Subroutine to do the actual screen printing of any string using interrupts in BIOS. It
outputs each character one by one until it reaches the string terminator.

35
Task 7 :Comprehensive Project
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.

36
This program performs simple arithmetic operations and prints the result. You can modify the
values of eax and ebx to perform different calculations. The print_result function uses a system
call to print the result.

Task 7.2
Document your project in detail, including program logic, flowcharts, and a report on challenges
faced and how they were addressed.

Project Documentation: Basic Calculator in Assembly Language

Program Logic

The basic calculator performs arithmetic operations (addition, subtraction, multiplication, and
division) on two predefined operands. The main components of the program are:

1. Initialization of operands: Load the values of the two operands into registers.

2. Performing operations: Execute addition, subtraction, multiplication, and division


operations.

3. Storing and printing of results: Save the results of each operation and print them to the
console.

4. Exiting the program: Termination of the program.

37
Flowchart

Below is a flowchart representing the program logic:

Start

Initialize operands

(load values into eax

and ebx)

Perform Addition

(add eax, ebx)

Store result in memory

(result)

Perform Subtraction

(sub eax, ebx)

Store result in memory

(result)

38
Perform Multiplication

(mul ebx)

Store result in memory

(result)

Perform Division

(div ebx)

Store result in memory

(result)

Print Results

Exit Program

39
Challenges and Solutions
1. Implementation of Arithmetic Operations:
Challenge: Implementations such as multiplication and division in assembly involve
precise register and data-size management.
Solution: Proper register handling is put in place; other registers that might interfere in any
calculations are cleared to avoid either overflow or corruption of data (for example edx is
cleared in division).

2. Printing Results:
Challenge: Printing the numerical results in assembly involves converting the numbers
into strings and handling system calls.
Solution: The int 0x80 system call prints it out, and a separate function with the purpose
of output (print_result) is being created since it makes the process easier.

3. Code Structure:
Challenge: Managing a cleaner and modular code structure in assembly can be a big
challenge because of the low-level nature of the language.
Solution: The program has been carved into different sections (data, bss, and text), and
functions for repetitive tasks, such as print_result, have also been created, making it easier
to understand and maintain.

40
CONCLUSION
Assembly language is a very important bridge between high-level programming languages and
underlying hardware, really offering practical and efficient means to hardware-specific
programming. With a focus on instruction sets, addressing modes, arithmetic operations, loops,
and interrupts, assembly programming allows users to really exercise precise control over
hardware resources. This assignment explored these bases to show how assembly language can
write effective and efficient programs specializing in certain hardware needs.

41
REFERENCES
1. What is a microprocessor? | IBM
2. x86 Assembly Language Programming Tutorial (TutorialsPoint)
3. Assembly Language Programming (GeeksforGeeks)
4. Assembly Language – Wikipedia
5. "The Art of Assembly Language" by Randall Hyde

42

You might also like