0% found this document useful (0 votes)
8 views6 pages

Lecture 19 45

The document covers various topics in assembly language, including procedures, stack management, macros, string instructions, interrupts, file handling, and mixing assembly with C/C++. It explains how to define and use procedures, pass parameters, manage the stack, and utilize macros for efficient code. Additionally, it details common interrupts and file functions in DOS, as well as techniques for integrating assembly code within C programs.

Uploaded by

ashamughal133
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

Lecture 19 45

The document covers various topics in assembly language, including procedures, stack management, macros, string instructions, interrupts, file handling, and mixing assembly with C/C++. It explains how to define and use procedures, pass parameters, manage the stack, and utilize macros for efficient code. Additionally, it details common interrupts and file functions in DOS, as well as techniques for integrating assembly code within C programs.

Uploaded by

ashamughal133
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Lecture 19–24: Procedures and Stack Management

➤ What is a Procedure?

• A procedure (like a function) is a named block of code that performs a specific task.

• Use CALL to jump to a procedure and RET to return.

• Saves the return address on the stack when called.

➤ Passing Parameters

• Parameters can be passed:

o Directly using registers

o Indirectly via memory

o By stack (most flexible)

• When using stack:

o PUSH parameters

o Inside procedure, access via BP (Base Pointer)

➤ Stack Usage

• Stack uses LIFO (Last In First Out)

• Instructions:

o PUSH: Adds data to stack

o POP: Removes data from stack

• BP is used to access parameters and local variables:

o [BP+4] = first pushed parameter (after return address)

➤ Local Variables

• Space is created on stack using SUB SP, n

• Accessed using [BP - offset]

• Must be cleaned using ADD SP, n before RET

➤ Saving/Restoring Registers
• Save important registers before procedure runs:

o PUSH AX … Do work … POP AX

➤ Nested Procedures

• A procedure can call another

• Each CALL pushes return address

• Proper stack management is crucial to avoid errors

Lecture 25–30: Macros and Conditional Assembly

➤ What is a Macro?

• A macro is a textual substitution, not an actual procedure call.

• No CALL/RET overhead; code is inserted inline.

• Defined using:

asm

CopyEdit

MyMacro MACRO

; code here

ENDM

➤ Macro vs Procedure

Feature Macro Procedure

Speed Faster Slower

Size Larger Smaller

Uses Stack? No Yes

Reusable? Less More

➤ Conditional Assembly

• Control which code gets assembled using:


o IF, ELSE, ENDIF

• Example:

asm

CopyEdit

IF CONST EQ 1

; Code for option 1

ELSE

; Code for option 2

ENDIF

➤ Looping in Macros

• Use:

o REPT n: repeat block n times

o IRP: loop over parameters

o IRPC: loop over characters in a string

Lecture 31–35: String Instructions

➤ String Instructions Basics

• Used to operate on arrays or strings.

• Registers:

o SI: Source Index

o DI: Destination Index

o CX: Counter

o DS, ES: Segment pointers

➤ Important Instructions

• MOVSB / MOVSW: Move byte/word

• LODSB / LODSW: Load from SI to AL/AX


• STOSB / STOSW: Store AL/AX to DI

• SCASB / SCASW: Compare AL/AX with DI

• CMPSB / CMPSW: Compare [SI] with [DI]

➤ REP Prefixes

• REP: Repeat instruction CX times

• REPE/REPZ: Repeat while equal / zero

• REPNE/REPNZ: Repeat while not equal / not zero

Lecture 36–39: Interrupts and BIOS/DOS Functions

➤ What is an Interrupt?

• A signal to CPU to stop current task and run another (usually I/O or system-level)

• In Assembly, we use INT instruction.

➤ Common Interrupts

• DOS Interrupt (INT 21h):

o AH=01h: Read character

o AH=09h: Display string

• BIOS Interrupts:

o INT 10h: Video

o INT 16h: Keyboard input

➤ Using INT 21h Example

asm

CopyEdit

MOV AH, 09h

MOV DX, OFFSET msg

INT 21h
Lecture 40–42: File Handling in Assembly

➤ DOS File Functions (INT 21h)

• Open File: AH=3Dh

• Read File: AH=3Fh

• Write File: AH=40h

• Close File: AH=3Eh

➤ Example File Read

asm

CopyEdit

MOV AH, 3Dh ; Open file

MOV DX, OFFSET filename

INT 21h

; File handle returned in AX

➤ File Pointer (Seek)

• AH = 42h, AL defines origin:

o 0: Beginning

o 1: Current

o 2: End

Lecture 43–45: Mixing Assembly with C/C++

➤ Inline Assembly in C

• Use asm {} or __asm to embed ASM code inside C.

• C can:

o Call ASM functions

o Pass values via registers or stack


➤ Calling ASM from C

• Define extern "C" to avoid name mangling

• Match calling convention

➤ Calling C from ASM

• Push parameters in reverse order

• Use proper function labels from C object file

• Clean the stack after call

Final Tips for Understanding

Topic Focus Area

Procedures CALL/RET, BP usage, stack

Macros Fast, inline, no stack use

String Ops REP family, SI/DI/CX

Interrupts INT 21h (DOS), INT 10h (BIOS)

File Handling INT 21h AH codes, handle management

C & ASM Parameter passing, naming conventions

You might also like