Group 4 Microcomputer systems assignment (1)
Group 4 Microcomputer systems assignment (1)
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.
1. Instruction Format
The instruction format specifies command structure.
Generally includes opcode (operation code), operands, and addressing modes.
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.),
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.
6
Explanation of Assembly Code
7
The description is as follows.
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)
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.
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.
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.
mov result, al: Store the result in the memory location result.
7. Exit 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.
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.
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.
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.
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.
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.
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.
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:
Multiplies eax (row index i) and ebx (column index j), storing the result in edx.
Stores the result (dl, the lower byte of edx) at the calculated memory position in the table.
Compares ebx (column index) to 10. If ebx is less than 10, it jumps back to next_col to
continue the inner loop.
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:
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.
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.
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.
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:
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.
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.
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.
_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.
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.
3. Storing and printing of results: Save the results of each operation and print them to the
console.
37
Flowchart
Start
Initialize operands
and ebx)
Perform Addition
(result)
Perform Subtraction
(result)
38
Perform Multiplication
(mul ebx)
(result)
Perform Division
(div ebx)
(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