Assembly Language Lab10
Assembly Language Lab10
Assembly Language Lab10
Eng. Alaa.I.Haniya
Assembly Language Fundamentals
Objective:
To learn more about procedures.
Area of the stack set aside for passed arguments, subroutine return address, local variables,
and saved registers.
RET Instruction:
Assembly Language Lab # 10
Syntax:
RET
RET n
Optional operand n causes n bytes to be added to the stack pointer after EIP (or IP)
is assigned a value.
1
.data
sum DWORD ?
.code
push 6 ; second argument
push 5 ; first argument
call AddTwo ; EAX = sum
mov sum,eax ; save the sum
AddTwo PROC
push ebp
mov ebp,esp ; base of stack frame
mov eax,[ebp + 12] ; second parameter
add eax,[ebp + 8] ; first parameter
pop ebp
ret
AddTwo ENDP
Local variables are created on the runtime stack, usually below the base pointer (EBP).
The LOCAL directive declares a list of local variables and immediately follows the PROC
directive, each variable is assigned a type.
Syntax: LOCAL varlist
Advanced Procedures
Syntax:
LOCAL var1:type1, var2:type2, . . .
Example:
MySub PROC
LOCAL var1:BYTE, var2:WORD, var3:DWORD Assembly Language Lab # 10
Part3: Recursion:
2
Lab work:
Excercise1:
Write an assembly recursive procedure that prints a hexadecimal number saved in memory on the screen.
Advanced Procedures
Assembly Language Lab # 10
3
Excercise2:
This function calculates the factorial of integer n ≥ 0. A new value of n is saved in each stack
frame: (use n=3, 5, 12)
Advanced Procedures
Assembly Language Lab # 10
4
Assembly Language Lab # 10 Advanced Procedures