10 gdb commands
RUN: starts the execution of a program inside the GDB
BREAK<label> sets a breakpoint for example
Break start sets a breakpoint at the start of the program
CONTINUE: resumes the execution of the program after the breakpoint
PRINT<expression> : Prints value of expression or register
NEXT: Executes next line skipping function call
STEP: Executes the next line and enters called function
DISASSEMBLE: Displays assembly instructions for a program
x/<n>i <address>: examines instruction from memory
INFO REGISTERS: displays contents of cpu registers
QUIT: exits the gdb debugger</n></expression></label>
role of control flow
alters the flow of execution in a program so instead of executing sequentially the cpu can jump to another instruction, call a procedure or return from a procedure .
categories of control flow, conditional and unconditional
CATEGORIES
UNCONDITIONAL: transfers control to a different part of the program regardless of the flags. Used for loo[ping or skipping sections of code. Example JMP and CALL, RET
CONDITIONAL: transfers control to a different part of the program only if specific cpu flags are set or reset after an operation.Used for error correction, decision making and looping. Example JE/JZ
INT and how its used in syscalls
Used to trigger a software interrupt. When it executes it causes the CPU to pause the current program, save its state and jump to the interrupt handler to execute the interrupt. In system calls, the program places the system call number and its parameters in registers then executes an INT instruction. The interrupt transfers control to kernel which executes the requested service and returns to the program. The INT instruction provides an interface between assembly programs and the operating system, enabling system-level functionality such as input, output, and process control through software interrupts.
code explanation for INT
mov ah, 02h ; DOS function: print character
mov dl, ‘A’ ; character to print
int 21h ; call the DOS interrupt
CALL
used to call a procedure or function.
Stores return address on stack.
Transfers control to the target address.
Allows returning using RET
JMP
transfers control to a target address.
Doesnt store return address
DOESNT Allow return once executed
MACROS AND PROCEDURE
MACROS
Code is expanded inline — the macro’s code is copied every time it is used.
Faster execution because there is no CALL/RET overhead.
Consumes more memory since the code is repeated at each use.
Defined using %macro / %endmacro.
Best used for short, frequently repeated operations.
PROCEDURES
Only one copy of the code exists — accessed using CALL.
Slower because CALL and RET require stack operations.
Uses less memory since code is stored once.
Defined using a label and ends with ret.
Best for complex logic, reusable functions, and structured programs.
MACROS ADVANTAGES AND DISADVANTAGES
MACROS ADVANTAGES
Code reusability
Simplifies code
Efficiency since there is no call overhead-faster
MACROS DISADVANTAGES
Increased code size because of inline expansion which leads to large binaries
Limited flexibility. Less powerful than procedures for complex tasks
Parameter limitations. Cant handle dynamic data as flexibly as procedures.
CODE EXPLANATION FOR CALL
CALL PRINT
…
PRINT:
; do something
RET
CODE FOR JMP
JMP SKIP
FACTORIAL WITH LOOP
MVI B, 05H
MVI A, 01H
LOOP:
MUL B
DCR B
JNZ LOOP
FACTORIAL WITHOUT LOOP
MVI A,04H
MUL 03H
MUL 02H
MUL 01H
STATUS FLAGS 6
CARRY-set when an unsigned arithmetic operation produces a carry in its msb
Example
Mov al, 255
Add al, 1
Jc, carry-set
PARITY-set if the number of 1s in the lsb byte is even
Example
Mov al, 3
Test al, al
Jp parity flag
ZERO- set if the eult if 0
Example Mov eax, 5 Sub eax, 5 Jz zero-detected
AUXILARRY CARRY- set if there is a carry from d3 to d4
Example
Mov al, 9
Add al, 7
OVERFLOW-set when the signed arithmetic overflows the data type rang
Example
Mov al, 127
Add al, 1
Jo overflow-detectedSIGN FLAG-set if the result of an operation is negative
Example
Mov eax, -5
Add eax, 3
Js sign-set
CONTROL FLAG 3
TRAP- enable single step debugging
Example
Mov eax, 1
Int 3; trigger a breakpoint
DIRECTION FLAG
0 -process forward-from lower memory location
1-process backwards
INTERRUPT FLAG-interrupt from peripheral
Sti-enable interrupt
STACK AND CALLING CONVENTIONS
Stack is a region of memory used to manage function calls, store arguments, return addresses and local variables. It operates in a LIFO structure with an ESP register pointing to the top of the stack.
During function calls
1. Arguments are pushed onto the stack in right to left order
2. Call instruction pushes the return address onto the stack automatically and jumps to function .
3. Inside the function, arguments and variables are accessed relative to ESP
During function returns
RET instruction pops the return address from stack and jumps back to the caller
Depending on the calling convention, either the caller (cdel) or callee(stdcall) does the stack cleanup.
This ensures proper passing of parameters, returning to correct location and isolating local variables.
STACK AND CALLING CONVENTIONS CODE
push 05h
push 03h
call ADD_NUM
inside the function
ADD_NUM:
push ax ; save register
; do work
pop ax ; restore register
ret
SUBROUTINE QUESTION code
MVI B,05H ; number whose factorial we want (≤5)
CALL FACT ; call subroutine
HLT ; result now in A
;—————— FACTORIAL SUBROUTINE ——————
FACT: PUSH B ; save B on stack
MVI A,01H ; A = 1 (initial result)
LOOP: MOV C,B ; C = current multiplier
MUL: ADD A ; A = A + A (repeated addition as simple multiply)
DCR C
JNZ MUL ; loop until C becomes zero
DCR B ; B = B - 1
JNZ LOOP ; continue factorial loop until B = 0
POP B ; restore original B
RET ; return to main programsubroutine + stack explanation
The program loads the number into register B and calls the subroutine FACT to compute its factorial. At the beginning of the subroutine, register B is saved using PUSH B so that its value is preserved. The accumulator A is initialized to 1 and the factorial is computed by repeatedly multiplying the accumulator by the decremented values of B using nested loops. After the calculation, POP B restores the original value from the stack, and RET returns control to the main program. The final factorial result is stored in the accumulator A. This demonstrates the use of the stack for saving registers and the use of a subroutine for modularizing code.
difference between machine code and assembly code
Machine language is the lowest-level programming language, consisting of binary code (0s and 1s) that a computer’s Central Processing Unit (CPU) can directly understand and execute.
Assembly language is a low-level programming language that uses mnemonics (symbolic codes) and symbols to represent machine language instructions, making it more human-readable than raw binary.
advantages of assembly code over machine language
Readability
Error correction and ease of modification
addressing modes
REGISTER-the operand is stored in the cpu registers
For example:
MOV AX, BX; The contents of BX are copied into AX
IMMEDIATE- the operand is directly embedded in the instruction
For example :
MOV AX, 10; The immediate value 10 is moved directly into register AX.
DIRECT- The Operand is stored in a memory location whose address is given directly
For example:
MOV AL, [MSG]; Data from memory address with label ‘MSG’is moved into AL.
INDEXED- uses an index register plus optional displacement such as as di and si
For example:
MOV AL, [SI + 5]; The operand is located at the memory address (SI + 5).
INDIRECT-Uses a register as a pointer to memory.
For example:
MOV AL, [BX]; The contents of memory location pointed to by BX are moved into
AL.
IMPLIED-the operand is implicitly defined in the instruction itself.
BASE-INDEX-combine contents of base register bx and an index register si to calculate the effective address.For example, MOV AL, [BX + SI].
SCALED INDEX-multiplies the index register by a scaled factor before adding it to a base register. example, MOV EAX,
[EBX + ESI*4]
assembler link cycle
SOURCE FILE CREATION-sue text editor to write program in ascii-output, program.asm(source file)
ASSEMBLING- assembler reads source file and produces object code and an optional listing flag which is a human readable version.
LINKING-linker reads object files and checks to see if the program contains any calls to functions in a link library. Combines object files with any library the program calls.
EXECUTION(LOADING)- os loader loads the executable in memory and the CPU begins the program execution
DIRECTIVES
EQU-equate directive-assigns a constant value to a label
ORG-ORIGIN DIRECTIVE-sets starting address for the code or data in memory.
OPCODE-Instruction to be executed, MOV
OPERAND-data to be operated on.