Week # 09
COAL
What is function in assembly language?
In assembly language, a function is a block of code that performs a specific task and can be
called multiple times from different parts of a program. Functions are also known as
procedures or subroutines.
A function in assembly language typically has the following characteristics:
Label: A function is identified by a label, which is a unique name assigned to the
function.
Code block: A function consists of a block of assembly language instructions that
perform a specific task.
Input parameters: Functions can accept input parameters, which are values passed to
the function when it is called.
Return value: Functions can return a value to the calling program.
Local variables: Functions can use local variables, which are variables that are
defined within the function and are not accessible outside the function.
How to create a function in assembly language?
Step 1: Name Your Function
Give your function a unique name so it can be easily identified.
Step 2: Decide What Goes In (Parameters)
Figure out what inputs your function needs to work with. These can be passed through
specific locations, like registers or memory.
Step 3: Write the Instructions
Write the actual code in assembly language. This is where the function does its job—like
using the inputs, performing calculations, and storing results.
Step 4: Exit the Function
Use a special instruction (like ret) to finish the function and go back to the main program.
If your function needs to send back a result, put it in the correct location (like a register)
before exiting.
Step 5: Add Comments (Optional)
Write notes in your code to explain what your function does, what inputs it needs, and
what it outputs. This makes your code easier to understand and update later.
add_numbers: ; Function to add two numbers
add eax, ebx ; Input parameters: eax (number 1), ebx (number 2)
ret ; Return value: eax (result)
Declaring a function:
Declaring a function means defining its name, the inputs it takes, and what it does.
Example: Declaring a Function in x86 Assembly Language
add_numbers: ; Declare a function to add two numbers
add eax, ebx ; Function code goes here
ret
Calling a Function
Calling a function in assembly language involves using the call instruction followed by the
label of the function to be called.
Example: Calling a Function in x86 Assembly Language
assembly
; Call the add_numbers function
mov eax, 5
mov ebx, 3
call add_numbers
; eax now contains the result (8)
Write a program in assembly language that call a function and add two numbers.
section .data ; Data section where we define the numbers
num1 db 5 ; First number (5)
num2 db 10 ; Second number (10)
section .text
global _start
_start: ; Load the addresses of the two numbers into registers
mov al, [num1] ; Load num1 into AL register
mov bl, [num2] ; Load num2 into BL register
; Call the add function
call add_numbers
; Print the result (store it into a register for output)
mov ebx, eax ; Move the result into ebx for exit
mov eax, 1 ; Syscall number for exit
int 0x80 ; Interrupt to exit
; Function to add two numbers (parameters in AL and BL)
add_numbers:
add al, bl ; Add the two numbers (AL + BL)
mov eax, al ; Move the result from AL to EAX (return value)
ret ; Return to the caller
How to control variables in function in assembly language?
Controlling variables in a function in assembly language involves managing memory
explicitly and using registers, the stack, or static memory locations.
1. Use of Registers
Registers are the fastest way to store and manipulate variables. Common registers used for
variables in x86 assembly include eax, ebx, ecx, and edx.
2. Using the Stack
The stack is a region of memory where you can temporarily store variables. You "push"
values onto the stack and "pop" them off when needed.
3. Using Local Variables in the Stack Frame
If you have multiple variables, you can create a stack frame using ebp (base pointer) and esp
(stack pointer). This method is common in function-based programs.
Analysis of Computer Registers and Program Status Word (PSW)
Types of Registers
1. General-Purpose Registers (GPRs)
o Purpose: Store intermediate results, data, or temporary values during
execution.
o Examples (x86):
EAX: EAX: EAX: EAX:
Accumulators Accumulators Accumulators Accumulators
register for register for register for register for
arithmetic and arithmetic and arithmetic and arithmetic and
logic operations. logic operations. logic operations. logic operations.
2. Special-Purpose Registers These are designed for specific tasks:
o Instruction Pointer (IP) / Program Counter (PC):
Holds the address of the next instruction to execute.
o Stack Pointer (SP):
Points to the top of the stack in memory.
o Base Pointer (BP):
Used to reference variables or parameters in the stack.
o Index Registers:
Used for addressing arrays or string operations (SI, DI in x86).
o Flags Register:
Contains individual bits representing the state of the processor.
3. Segment Registers (x86 architecture)
o Define memory segments for code, data, and [Link]: CS (Code
Segment), DS (Data Segment), SS (Stack Segment).
4. Floating-Point Registers
o Hold floating-point numbers for mathematical operations.
5. Control Registers
o Store system-level settings like memory management, protection, and
debugging.
Program Status Word (PSW)
The Program Status Word (PSW) is a special-purpose register that contains information
about the current state of the CPU and controls its operation. It is often part of the CPU’s
flags register.
Structure of the PSW
The structure and contents of the PSW depend on the CPU architecture, but it typically
includes the following fields:
1. Condition Flags: These flags are set or cleared based on the results of operations:
o Zero Flag (Z): Set if the result of an operation is zero.
o Carry Flag (C): Set if an operation results in a carry out or borrow.
o Sign Flag (S): Indicates the sign of the result (1 for negative, 0 for positive).
o Overflow Flag (O): Indicates if an arithmetic operation caused an overflow.
o Parity Flag (P): Indicates if the number of 1-bits in the result is even or odd.
2. Control Flags: These influence the behavior of the CPU:
o Interrupt Enable (I): Enables or disables hardware interrupts.
o Direction Flag (D): Controls the direction of string operations
(increment/decrement).
o Trap Flag (T): Enables single step debugging.
3. Execution Mode Flags:
o Specify the CPU operating mode (e.g., User Mode, Kernel Mode).
o Used for privilege levels in protected mode.