0% found this document useful (0 votes)
11 views

Procedures LAb

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Procedures LAb

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Procedures in assembly

language
USING EMU8086
Objectives

1. Objective 1: Understand the concept of procedures in assembly

language.

2. Objective 2: Learn how to define, call, and use procedures.

3. Objective 3: Understand parameter passing and stack usage in

procedures.
Introduction to Procedures

•Definition: A procedure is a block of code that performs a specific task and


can be called from different parts of a program.
•Benefits:
• Code Reusability
• Modularity
• Easier Debugging
Basic Procedure Syntax

procedure_name proc
1. Procedure definition
; procedure body 2. Procedure call
ret 3. Procedure return

procedure_name endp
How CALL and RET Work

•CALL Instruction: call procedure_name


•Pushes the address of the next ; Equivalent to:
instruction onto the stack.
; push IP
•Jumps to the procedure's address.
; jump to procedure_name
•Example:
•RET Instruction:
•Pops the return address from
the stack.
ret
•Jumps back to the address
popped. ; Equivalent to:
•Example:
; pop IP

; jump to IP
Stack Operations for CALL and RET

•CALL:

•Stack before CALL: [SP] = Old SP


•After CALL: [SP-2] = Return Address
•RET:

•Stack before RET: [SP] = Return Address


•After RET: [SP+2] = Old SPT: [SP+2] = Old SP
Example

•Data segment defines a message.


•main procedure sets up the data segment and calls print_message.
•print_message displays the message and returns.
.model small
.stack 100h
.data
msg db 'Hello, World!$', 0
.code
main proc
mov ax, @data
mov ds, ax
call print_message
mov ah, 4Ch
int 21h
main endp

print_message proc
lea dx, msg
mov ah, 09h
int 21h
ret
print_message endp
Passing Parameters to Procedures

•By Register: Pass parameters using registers (e.g., AX, BX).

•By Stack: Pass parameters using the stack (PUSH/POP

instructions).
Example: Passing Parameters via Stack

•main pushes two numbers onto the stack and calls add_numbers.

•add_numbers accesses parameters via the stack frame and adds them.

•Stack is cleaned up before returning.leaned up before returning.


.model small add_numbers proc
.stack 100h push bp
.data
num1 dw 5
mov bp, sp
num2 dw 10
.code mov ax, [bp+6]
main proc add ax, [bp+4]
mov ax, @data
mov ds, ax
mov ax, num1
pop bp
push ax ret 4; Clean up the stack by 4 bytes (2
mov ax, num2 parameters of 2 bytes each)
push ax add_numbers endp
call add_numbers
add sp, 4
mov ah, 4Ch
end main
int 21h
main endp
Example: Nested Procedures

greet procedure calls print_hello and print_world


procedures.
.model small print_hello proc
.stack 100h
lea dx, msg1
.data
msg1 db 'Hello, ', 0 mov ah, 09h
msg2 db 'World!$', 0
int 21h
.code
main proc ret
mov ax, @data
print_hello endp
mov ds, ax
call greet print_world proc
mov ah, 4Ch
lea dx, msg2
int 21h
main endp mov ah, 09h
greet proc
int 21h
call print_hello
call print_world ret
ret
print_world endp
greet endp
end main
Lab exercise: String length calculation
.model small string_length proc
.stack 100h mov cx, 0
.data
length_loop:
str db 'Hello, World!', 0
cmp byte ptr [si], 0
len dw ?
.code je end_length
main proc inc si
mov ax, @data inc cx
mov ds, ax
jmp length_loop
lea si, str
end_length:
call string_length
mov len, cx ret
mov ah, 4Ch string_length endp
int 21h end main
main endp
.model small
.stack 100h
.data gcd proc
num1 dw 48 cmp bx, 0
num2 dw 18 je end_gcd
result dw ?
mov dx, 0 ; Clear DX before division
.code
main proc div bx ; Divide AX by BX, quotient in AX,
mov ax, @data remainder in DX
mov ds, ax mov ax, bx ; Move BX to AX
mov bx, dx ; Move DX (remainder) to BX
mov ax, num1
call gcd ; Recursively call gcd with new AX
mov bx, num2
call gcd and BX
mov result, ax end_gcd:
ret
mov ah, 4Ch gcd endp
int 21h
main endp
end main

You might also like