exam prep Flashcards

(35 cards)

1
Q

10 gdb commands

A

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>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

role of control flow

A

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 .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

categories of control flow, conditional and unconditional

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

INT and how its used in syscalls

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

code explanation for INT

A

mov ah, 02h ; DOS function: print character
mov dl, ‘A’ ; character to print
int 21h ; call the DOS interrupt

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

CALL

A

used to call a procedure or function.
Stores return address on stack.
Transfers control to the target address.
Allows returning using RET

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

JMP

A

transfers control to a target address.
Doesnt store return address
DOESNT Allow return once executed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

MACROS AND PROCEDURE

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

MACROS ADVANTAGES AND DISADVANTAGES

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

CODE EXPLANATION FOR CALL

A

CALL PRINT

PRINT:
; do something
RET

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

CODE FOR JMP

A

JMP SKIP

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

FACTORIAL WITH LOOP

A

MVI B, 05H
MVI A, 01H
LOOP:
MUL B
DCR B
JNZ LOOP

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

FACTORIAL WITHOUT LOOP

A

MVI A,04H
MUL 03H
MUL 02H
MUL 01H

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

STATUS FLAGS 6

A

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-detected

SIGN FLAG-set if the result of an operation is negative

Example
Mov eax, -5
Add eax, 3
Js sign-set

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

CONTROL FLAG 3

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

STACK AND CALLING CONVENTIONS

A

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.

17
Q

STACK AND CALLING CONVENTIONS CODE

A

push 05h
push 03h
call ADD_NUM

inside the function
ADD_NUM:
push ax ; save register
; do work
pop ax ; restore register
ret

18
Q

SUBROUTINE QUESTION code

A

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 program
19
Q

subroutine + stack explanation

A

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.

20
Q

difference between machine code and assembly code

A

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.

21
Q

advantages of assembly code over machine language

A

Readability
Error correction and ease of modification

22
Q

addressing modes

A

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]

23
Q

assembler link cycle

A

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

24
Q

DIRECTIVES

A

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.

25
Instruction Functions + Flag Effects
CMP:subtracts the source operand from destination operand without storing result. Updates zero flag(both values are equal), sign flag(sign of subtraction) carry(borrow from destination to source) parity and auxiliary carry RET: pops return address from stack and transfers control back to procedure. affects no flag JC JC checks the Carry flag and performs a jump to the specified address only if CF = 1. It does not change any flags . MOV (Move): MOV copies data from a source to a destination operand. It does not perform arithmetic, so no flags are affected;
26
Interrupt vectors and ISR
Interrupt vectors are fixed memory addresses that point to the start of an Interrupt Service Routine (ISR). Each interrupt type has a unique vector, so when an interrupt occurs, the CPU knows exactly where to jump. Execution of an ISR: How an ISR works: When an interrupt occurs, the CPU saves the current program location on the stack. It then jumps to the address in the interrupt vector. The ISR runs and performs the needed task. When done, the ISR uses a return-from-interrupt instruction (like RETI) to go back to the saved program location. This mechanism allows asynchronous events (like I/O) to be handled efficiently without disturbing the main program flow.
27
calling conventions
1. C declaration(cdecl) Parameters are pushed onto the stack in reverse order(right-left). The caller cleans up the stack after the cal 2. Standard Calling convention(stdcall) Parameters are pushed onto the stack in reverse order(right-left). The callee which is the function itself cleans up the stack using a RET n instruction.
28
29
General purpose registers
are used by the programmer to store temporary data, perform arithmetic or logical operations, and move data between memory and the CPU. AX, BX, CX, DX (or AL, AH, BL, BH for 8-bit parts).
30
special purpose registers
have a dedicated role in controlling or monitoring the CPU, managing memory, or handling program execution. SP (Stack Pointer), BP (Base Pointer), IP (Instruction Pointer), FLAGS register.
31
sections
1. section .data- Stores initialized data such as constants and messages 2. section .bss- Stores uninitialized data 3. section .text- Contains the executable instructions
32
instruction byte size
LDAX H → 1 DCX SP → 1 CALL NN → 3 ADI N → 2 RRC → 1 ANI N → 2
33
quotient
AL
34
REMAINDER
AH
35
b) Explain what would happen if you declare an uninitialized variable in the wrong section. Give a short code example
If an uninitialized variable is declared in .data instead of .bss, it unnecessarily occupies space in the program file, increasing its size. If declared in .text, it may cause assembly or runtime errors because .text is read-only code. Wrong code section .text count resb 1 Correct code section .bss count resb 1