SlideShare une entreprise Scribd logo
1  sur  46
Advanced Procedures
COMPUTER ORG. & ASSEMBLY LANGUAGE
Group Members:
Saira Irshad
M Sikandar Mustafa
Arslan Liaqat
Maryam Noreen
Overview
INVOKE, ADDR, PROC and PROTO
Stack Frames
Recursion
Creating Multimodule Programs
PROC, ADDR, INVOKE and PROTO
Directives
ADVANCE PROCEDURES
PROC Directive
The PROC directive declares a procedure with an optional list of named
parameters.
Syntax:
label PROC paramList
paramList is a list of parameters separated by commas. Each parameter
has the following syntax:
paramName : type
Type must either be one of the standard ASM types (BYTE, SBYTE,
WORD, etc.), or it can be a pointer to one of these types.
PROC Directive
Alternate format permits parameter list to be on one or more separate lines:
label PROC,
paramList
The parameters can be on the same line . . .
param-1:type-1, param-2:type-2, . . ., param-n:type-n
Or they can be on separate lines:
param-1:type-1,
param-2:type-2,
. . .,
param-n:type-n
PROC example
• The AddTwo procedure receives two integers and returns
their sum in EAX.
AddTwo PROC,
val1:DWORD, val2:DWORD
push ebp
mov ebp, esp
mov eax,val1
add eax,val2
leave
ret 8
AddTwo ENDP
INVOKE Directive
The INVOKE directive simplifies a procedure call by allowing you to pass
parameters to a procedure in a single statement.
Syntax:
INVOKE procedureName [, argumentList]
ArgumentList is an optional comma-delimited list of procedure arguments
Arguments can be:
immediate values and integer expressions
variable names
address and ADDR expressions
register names
INVOKE Example
.data
byteVal BYTE 10
wordVal WORD 1000h
.code
; direct operands:
INVOKE Sub1,byteVal,wordVal
; register name, integer expression:
INVOKE Sub3,eax,(10 * 20)
ADDR Operator
.data
myWord WORD ?
.code
INVOKE mySub,ADDR myWord
• Returns address of variable
• Simple example:
PROTO Directive
Creates a procedure prototype.(Function Signature)
Syntax:
◦ label PROTO paramList
Every procedure called by the INVOKE directive must have a prototype
PROTO example
Standard configuration: PROTO appears at top of the program listing,
INVOKE appears in the code segment, and the procedure implementation
occurs later in the program:
MySub PROTO ; procedure prototype
.code
INVOKE MySub ; procedure call
MySub PROC ; procedure implementation
.
.
MySub ENDP
PROTO example
Prototype for the ArraySum procedure, showing its
parameter list:
ArraySum PROTO,
ptrArray:PTR DWORD, ; points to the array
szArray:DWORD ; array size
ArraySum PROC
ptrArray:PTR DWORD, ; points to the array
szArray:DWORD ; array size
.
.
RET
ArraySum ENDP
Stack Frames
ADVANCE PROCEDURES
Stack Frames
A stack frame is a frame of data that gets pushed onto the stack. A stack
frame would represent a function call and its argument data.
Also known as an activation record
Data pushed on stack frame is:
Arguments of function
Return Address
Local Variables of function
Temporary data used by the compiler
How it Works
Stack Frames
How it Works
Whenever a function call is made a “Stack frame” is created
registers used for this process are:
ESP – Stack Pointer
Always points to the top of Stack
EBP – Base pointer Or Frame pointer(FP)
Always points to Top of Stack frame
Must be reallocated its value after exit
How it Works
Explicit Access to Stack Parameters
A procedure can explicitly access stack parameters using constant
offsets from EBP.
Example: [ebp + 8]
Return from Subroutine in Assembly
It transfer control back to the previous Procedure or caller function or
target Address
Syntax:
◦ RET
◦ RET n
◦ Where n is number of byte or storage add from Stack Pointer (ESP)
after Instruction pointer (EIP) is assigned a value to transfer control
Example: Pass by value
Assembly Code
.data
sum DWORD ?
.code
push 6 ; second argument
push 5 ; first argument
call AddTwo ; EAX = sum
mov sum,eax ; save the sum
00000006
00000005
return address
EBP, ESP
[EBP + 4]
[EBP + 8]
[EBP + 12]
EBP
C++ equivalent code
◦ Int sum = AddTwo( 5, 6) ;
Example(cont.)
AddTwo Procedure in Assembly
00000006
00000005
return address
EBP, ESP
[EBP + 4]
[EBP + 8]
[EBP + 12]
EBP
Example(cont.)
AddTwo Function in C++
Int AddTwo(int x , int y)
{
return x+y;
}
Example : Pass By Reference
The ArrayFill procedure fills an array with 0
The calling program passes the address of the array, along with a count of the
number of array elements:
.data
count = 100
array dw count DUP(?)
.code
push OFFSET array
push COUNT
call ArrayFill
offset(array)
count
EBP
[EBP + 8]
[EBP + 12]
return address
EBP
Pass By Reference:
ArrayFill PROC
push ebp
mov ebp,esp
pushad
mov esi,[ebp+12]
mov ecx,[ebp+8]
cmp ecx,0
jle L2
L1:
mov [esi],0
inc esi
loop L1
L2:
popad
pop ebp
ret 8
ArrayFill ENDP
offset(array)
count
EBP
[EBP + 8]
[EBP + 12]
return address
EBP
Leave Instruction
Assign the value of Base Pointer(EBP) in Stack Pointer(ESP)
Pop EBP from stack
leave is same as:
mov esp,ebp
pop ebp
Local Variables
A local variable is created, used, and destroyed within a single
Procedure
Two Methods in assembly: Reserve Memory in stack and using
LOCAL Directive
The LOCAL directive declares a list of local variables
◦ immediately follows the PROC directive
◦ each variable is assigned a type
LOCAL directive
Syntax:
◦ LOCAL varlist
Example:
MySub PROC
LOCAL v1:BYTE, v2:WORD, v3:DWORD
Local Variable
MySub PROC
push ebp
mov ebp,esp
sub esp,8
mov [ebp-4],123456h ; locA
mov [ebp-8],0 ; locB
.
.
To explicitly create local variables, subtract their total size from ESP.
The following example creates and initializes two 32-bit local variables
(we'll call them locA and locB):
[EBP+8] Parameters
[EBP+4 ] : Return Address
EBP
[EBP-4 ] : 123456
[EBP-8]: 0
ESP
LOCAL Example
BubbleSort PROC
LOCAL temp:DWORD, SwapFlag:DWORD
push ebp
mov ebp,esp
add esp,0FFFFFFF8h ; add -8 to ESP
. . .
mov esp,ebp
pop ebp
ret
BubbleSort ENDP
LOCAL Example
return address
EBP EBP
[EBP - 4]
ESP
temp
SwapFlag [EBP - 8]
Diagram of the stack frame for the BubbleSort
procedure:
Another Example
xyz proc
local v1:byte , v2:byte ,
v3:dword
push bp
mov bp,sp
sub sp,12
mov v1,21h
mov v2,43h
mov v3,44441111h
mov sp,bp
pop bp
ret
xyz endp
Recursion:
What is recursion?
Recursively Calculating a Sum
Calculating a Factorial
What is Recursion?
The process created when . . .
◦ A procedure calls itself repeatedly
◦ Procedure A calls procedure B, which in turn calls procedure A
Using a graph in which each node is a procedure and each edge is a procedure
call, recursion forms a cycle:
A
B
D
E
C
Example in C++ : recursion
Void recursive(int n)
{
n = n-1;
if(n != 0)
Recursive(n);
}
Recursively Calculating a Sum
The CalcSum procedure recursively calculates the sum of an
array of integers. Receives: ECX = count. Returns: EAX = sum
.code
mov ecx, 5
mov eax,0
call CalcSum
L1:
; any instructions
CalcSum PROC
cmp ecx,0 ; check counter value
jz L2 ; quit if zero
add eax,ecx ; otherwise, add to sum
dec ecx ; decrement counter
call CalcSum ; recursive call
L2: ret
CalcSum ENDP
Calculating a Factorial (1 of 3)
This function calculates the factorial of integer n. A new value
of n is saved in each stack frame:
int function factorial(int n)
{
if(n == 0)
return 1;
else
return n * factorial(n-1);
}
5! = 5 * 4!
4! = 4 * 3!
3! = 3 * 2!
2! = 2 * 1!
1! = 1 * 0!
0! = 1
(base case)
1 * 1 = 1
2 * 1 = 2
3 * 2 = 6
4 * 6 = 24
5 * 24 = 120
1 = 1
recursive calls backing up
As each call instance returns, the
product it returns is multiplied by the
previous value of n.
Calculating a Factorial (2 of 3)
Factorial PROC
push ebp
mov ebp,esp
mov eax,[ebp+8] ; get n
cmp eax,0 ; n < 0?
ja L1 ; yes: continue
mov eax,1 ; no: return 1
jmp L2
L1: dec eax
push eax ; Factorial(n-1)
call Factorial
; Instructions from this point on execute when each
; recursive call returns.
ReturnFact:
mov ebx,[ebp+8] ; get n
mul ebx ; eax = eax * ebx
L2: pop ebp ; return EAX
ret 4 ; clean up stack
Factorial ENDP
Calculating a Factorial (3 of 3)
Suppose we want to calculate
12!
This diagram shows the first
few stack frames created by
recursive calls to Factorial
Each recursive call uses 12
bytes of stack space.
12 n
n-1
ReturnMain
ebp0
11
ReturnFact
ebp1
10
ReturnFact
ebp2
9
ReturnFact
ebp3
n-2
n-3
(etc...)
Alert!
Using a corrupt recursive function can cause your
program damage
Stack Overflow will take place
It can also effect other data on the stack
Multimodule Programs
A multimodule program is a program whose source code has been
divided up into separate ASM files.
Each ASM file (module) is assembled into a separate OBJ file.
All OBJ files belonging to the same program are linked using the link
utility into a single EXE file.
◦ This process is called static linking
Advantages
Large programs are easier to write, maintain, and debug when divided
into separate source code modules.
• When changing a line of code, only its enclosing module needs to be
assembled again. Linking assembled modules requires little time.
• A module can be a container for logically related code and data (think
object-oriented here...)
• encapsulation: procedures and variables are automatically hidden in a
module unless you declare them public
Creating a Multimodule Program
Here are some basic steps to follow when creating a multimodule
program:
◦ Create the main module
◦ Create a separate source code module for each procedure or set of
related procedures
◦ Create an include file that contains procedure prototypes for external
procedures (ones that are called between modules)
◦ Use the INCLUDE directive to make your procedure prototypes
available to each module
Example: ArraySum Program
Let's review the ArraySum.
Summation
Program (main)
Clrscr PromptForIntegers ArraySum DisplaySum
WriteStringWriteString ReadInt WriteIntWriteInt
Each of the four white rectangles will become a module.
ANY QUESTION

Contenu connexe

Tendances (20)

Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsort
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix Notations
 
Context free grammar
Context free grammar Context free grammar
Context free grammar
 
Pattern matching
Pattern matchingPattern matching
Pattern matching
 
Heaps
HeapsHeaps
Heaps
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
Stack organization
Stack organizationStack organization
Stack organization
 
Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree method
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applications
 
Parsing
ParsingParsing
Parsing
 
Huffman coding
Huffman coding Huffman coding
Huffman coding
 
Deterministic Finite Automata
Deterministic Finite AutomataDeterministic Finite Automata
Deterministic Finite Automata
 
push down automata
push down automatapush down automata
push down automata
 
stack presentation
stack presentationstack presentation
stack presentation
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structure
 
Finite Automata
Finite AutomataFinite Automata
Finite Automata
 
Automata theory
Automata theoryAutomata theory
Automata theory
 
Regular Expression to Finite Automata
Regular Expression to Finite AutomataRegular Expression to Finite Automata
Regular Expression to Finite Automata
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Expression trees
Expression treesExpression trees
Expression trees
 

Similaire à Advanced procedures in assembly language Full chapter ppt

Reversing malware analysis training part4 assembly programming basics
Reversing malware analysis training part4 assembly programming basicsReversing malware analysis training part4 assembly programming basics
Reversing malware analysis training part4 assembly programming basicsCysinfo Cyber Security Community
 
Reversing & Malware Analysis Training Part 4 - Assembly Programming Basics
Reversing & Malware Analysis Training Part 4 - Assembly Programming BasicsReversing & Malware Analysis Training Part 4 - Assembly Programming Basics
Reversing & Malware Analysis Training Part 4 - Assembly Programming Basicssecurityxploded
 
Reversing & malware analysis training part 4 assembly programming basics
Reversing & malware analysis training part 4   assembly programming basics Reversing & malware analysis training part 4   assembly programming basics
Reversing & malware analysis training part 4 assembly programming basics Abdulrahman Bassam
 
Assembly language programming_fundamentals 8086
Assembly language programming_fundamentals 8086Assembly language programming_fundamentals 8086
Assembly language programming_fundamentals 8086Shehrevar Davierwala
 
Objectives 1. using indirect addressing 2. passing parameters.pdf
Objectives 1. using indirect addressing 2. passing parameters.pdfObjectives 1. using indirect addressing 2. passing parameters.pdf
Objectives 1. using indirect addressing 2. passing parameters.pdffcsondhiindia
 
Digital Signal Processing Lab Manual
Digital Signal Processing Lab Manual Digital Signal Processing Lab Manual
Digital Signal Processing Lab Manual Amairullah Khan Lodhi
 
lec15_x86procedure_4up.pdf
lec15_x86procedure_4up.pdflec15_x86procedure_4up.pdf
lec15_x86procedure_4up.pdfhasan58964
 
Declare Your Language: Virtual Machines & Code Generation
Declare Your Language: Virtual Machines & Code GenerationDeclare Your Language: Virtual Machines & Code Generation
Declare Your Language: Virtual Machines & Code GenerationEelco Visser
 
Lec 04 intro assembly
Lec 04 intro assemblyLec 04 intro assembly
Lec 04 intro assemblyAbdul Khan
 
Basic concept of MATLAB.ppt
Basic concept of MATLAB.pptBasic concept of MATLAB.ppt
Basic concept of MATLAB.pptaliraza2732
 

Similaire à Advanced procedures in assembly language Full chapter ppt (20)

[ASM]Lab6
[ASM]Lab6[ASM]Lab6
[ASM]Lab6
 
Advance ROP Attacks
Advance ROP AttacksAdvance ROP Attacks
Advance ROP Attacks
 
Reversing malware analysis training part4 assembly programming basics
Reversing malware analysis training part4 assembly programming basicsReversing malware analysis training part4 assembly programming basics
Reversing malware analysis training part4 assembly programming basics
 
Exploitation Crash Course
Exploitation Crash CourseExploitation Crash Course
Exploitation Crash Course
 
Reversing & Malware Analysis Training Part 4 - Assembly Programming Basics
Reversing & Malware Analysis Training Part 4 - Assembly Programming BasicsReversing & Malware Analysis Training Part 4 - Assembly Programming Basics
Reversing & Malware Analysis Training Part 4 - Assembly Programming Basics
 
Reversing & malware analysis training part 4 assembly programming basics
Reversing & malware analysis training part 4   assembly programming basics Reversing & malware analysis training part 4   assembly programming basics
Reversing & malware analysis training part 4 assembly programming basics
 
Al2ed chapter17
Al2ed chapter17Al2ed chapter17
Al2ed chapter17
 
Chapter 5 notes new
Chapter 5 notes newChapter 5 notes new
Chapter 5 notes new
 
Matlab-3.pptx
Matlab-3.pptxMatlab-3.pptx
Matlab-3.pptx
 
Assem -lect-6
Assem -lect-6Assem -lect-6
Assem -lect-6
 
Assembly language programming_fundamentals 8086
Assembly language programming_fundamentals 8086Assembly language programming_fundamentals 8086
Assembly language programming_fundamentals 8086
 
Objectives 1. using indirect addressing 2. passing parameters.pdf
Objectives 1. using indirect addressing 2. passing parameters.pdfObjectives 1. using indirect addressing 2. passing parameters.pdf
Objectives 1. using indirect addressing 2. passing parameters.pdf
 
cp05.pptx
cp05.pptxcp05.pptx
cp05.pptx
 
[ASM]Lab8
[ASM]Lab8[ASM]Lab8
[ASM]Lab8
 
Digital Signal Processing Lab Manual
Digital Signal Processing Lab Manual Digital Signal Processing Lab Manual
Digital Signal Processing Lab Manual
 
lec15_x86procedure_4up.pdf
lec15_x86procedure_4up.pdflec15_x86procedure_4up.pdf
lec15_x86procedure_4up.pdf
 
Chapter 6 notes
Chapter 6 notesChapter 6 notes
Chapter 6 notes
 
Declare Your Language: Virtual Machines & Code Generation
Declare Your Language: Virtual Machines & Code GenerationDeclare Your Language: Virtual Machines & Code Generation
Declare Your Language: Virtual Machines & Code Generation
 
Lec 04 intro assembly
Lec 04 intro assemblyLec 04 intro assembly
Lec 04 intro assembly
 
Basic concept of MATLAB.ppt
Basic concept of MATLAB.pptBasic concept of MATLAB.ppt
Basic concept of MATLAB.ppt
 

Plus de Muhammad Sikandar Mustafa

overview introduction to Software Engineering
overview introduction to Software Engineeringoverview introduction to Software Engineering
overview introduction to Software EngineeringMuhammad Sikandar Mustafa
 

Plus de Muhammad Sikandar Mustafa (20)

What is organizational behavior
What is organizational behaviorWhat is organizational behavior
What is organizational behavior
 
11. estimation-1
11. estimation-111. estimation-1
11. estimation-1
 
9. risk-management
9. risk-management9. risk-management
9. risk-management
 
8. project-management
8. project-management8. project-management
8. project-management
 
7. requirement-engineering
7. requirement-engineering7. requirement-engineering
7. requirement-engineering
 
6. software requirements
6. software requirements6. software requirements
6. software requirements
 
software process
software process software process
software process
 
software myths
software mythssoftware myths
software myths
 
software characteristics
software characteristicssoftware characteristics
software characteristics
 
overview introduction to Software Engineering
overview introduction to Software Engineeringoverview introduction to Software Engineering
overview introduction to Software Engineering
 
5. software process model
5. software process model5. software process model
5. software process model
 
Lesson05 relational languages sql
Lesson05 relational languages sqlLesson05 relational languages sql
Lesson05 relational languages sql
 
Lesson03 the relational model
Lesson03 the relational modelLesson03 the relational model
Lesson03 the relational model
 
Lesson02 database system architecture
Lesson02 database system architectureLesson02 database system architecture
Lesson02 database system architecture
 
Lesson01 Database introduction
Lesson01 Database introductionLesson01 Database introduction
Lesson01 Database introduction
 
Lesson00 intro to databases
Lesson00 intro to databasesLesson00 intro to databases
Lesson00 intro to databases
 
Lesson10 Database security
Lesson10 Database security Lesson10 Database security
Lesson10 Database security
 
Lesson08 tm recovery
Lesson08 tm recoveryLesson08 tm recovery
Lesson08 tm recovery
 
Lesson07 e r modelling
Lesson07 e r modellingLesson07 e r modelling
Lesson07 e r modelling
 
Lesson06 database design
Lesson06 database designLesson06 database design
Lesson06 database design
 

Dernier

Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 

Dernier (20)

Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 

Advanced procedures in assembly language Full chapter ppt

  • 1. Advanced Procedures COMPUTER ORG. & ASSEMBLY LANGUAGE
  • 2. Group Members: Saira Irshad M Sikandar Mustafa Arslan Liaqat Maryam Noreen
  • 3. Overview INVOKE, ADDR, PROC and PROTO Stack Frames Recursion Creating Multimodule Programs
  • 4. PROC, ADDR, INVOKE and PROTO Directives ADVANCE PROCEDURES
  • 5. PROC Directive The PROC directive declares a procedure with an optional list of named parameters. Syntax: label PROC paramList paramList is a list of parameters separated by commas. Each parameter has the following syntax: paramName : type Type must either be one of the standard ASM types (BYTE, SBYTE, WORD, etc.), or it can be a pointer to one of these types.
  • 6. PROC Directive Alternate format permits parameter list to be on one or more separate lines: label PROC, paramList The parameters can be on the same line . . . param-1:type-1, param-2:type-2, . . ., param-n:type-n Or they can be on separate lines: param-1:type-1, param-2:type-2, . . ., param-n:type-n
  • 7. PROC example • The AddTwo procedure receives two integers and returns their sum in EAX. AddTwo PROC, val1:DWORD, val2:DWORD push ebp mov ebp, esp mov eax,val1 add eax,val2 leave ret 8 AddTwo ENDP
  • 8. INVOKE Directive The INVOKE directive simplifies a procedure call by allowing you to pass parameters to a procedure in a single statement. Syntax: INVOKE procedureName [, argumentList] ArgumentList is an optional comma-delimited list of procedure arguments Arguments can be: immediate values and integer expressions variable names address and ADDR expressions register names
  • 9. INVOKE Example .data byteVal BYTE 10 wordVal WORD 1000h .code ; direct operands: INVOKE Sub1,byteVal,wordVal ; register name, integer expression: INVOKE Sub3,eax,(10 * 20)
  • 10. ADDR Operator .data myWord WORD ? .code INVOKE mySub,ADDR myWord • Returns address of variable • Simple example:
  • 11. PROTO Directive Creates a procedure prototype.(Function Signature) Syntax: ◦ label PROTO paramList Every procedure called by the INVOKE directive must have a prototype
  • 12. PROTO example Standard configuration: PROTO appears at top of the program listing, INVOKE appears in the code segment, and the procedure implementation occurs later in the program: MySub PROTO ; procedure prototype .code INVOKE MySub ; procedure call MySub PROC ; procedure implementation . . MySub ENDP
  • 13. PROTO example Prototype for the ArraySum procedure, showing its parameter list: ArraySum PROTO, ptrArray:PTR DWORD, ; points to the array szArray:DWORD ; array size ArraySum PROC ptrArray:PTR DWORD, ; points to the array szArray:DWORD ; array size . . RET ArraySum ENDP
  • 15. Stack Frames A stack frame is a frame of data that gets pushed onto the stack. A stack frame would represent a function call and its argument data. Also known as an activation record Data pushed on stack frame is: Arguments of function Return Address Local Variables of function Temporary data used by the compiler
  • 18. How it Works Whenever a function call is made a “Stack frame” is created registers used for this process are: ESP – Stack Pointer Always points to the top of Stack EBP – Base pointer Or Frame pointer(FP) Always points to Top of Stack frame Must be reallocated its value after exit
  • 20. Explicit Access to Stack Parameters A procedure can explicitly access stack parameters using constant offsets from EBP. Example: [ebp + 8]
  • 21. Return from Subroutine in Assembly It transfer control back to the previous Procedure or caller function or target Address Syntax: ◦ RET ◦ RET n ◦ Where n is number of byte or storage add from Stack Pointer (ESP) after Instruction pointer (EIP) is assigned a value to transfer control
  • 22. Example: Pass by value Assembly Code .data sum DWORD ? .code push 6 ; second argument push 5 ; first argument call AddTwo ; EAX = sum mov sum,eax ; save the sum 00000006 00000005 return address EBP, ESP [EBP + 4] [EBP + 8] [EBP + 12] EBP C++ equivalent code ◦ Int sum = AddTwo( 5, 6) ;
  • 23. Example(cont.) AddTwo Procedure in Assembly 00000006 00000005 return address EBP, ESP [EBP + 4] [EBP + 8] [EBP + 12] EBP
  • 24. Example(cont.) AddTwo Function in C++ Int AddTwo(int x , int y) { return x+y; }
  • 25. Example : Pass By Reference The ArrayFill procedure fills an array with 0 The calling program passes the address of the array, along with a count of the number of array elements: .data count = 100 array dw count DUP(?) .code push OFFSET array push COUNT call ArrayFill offset(array) count EBP [EBP + 8] [EBP + 12] return address EBP
  • 26. Pass By Reference: ArrayFill PROC push ebp mov ebp,esp pushad mov esi,[ebp+12] mov ecx,[ebp+8] cmp ecx,0 jle L2 L1: mov [esi],0 inc esi loop L1 L2: popad pop ebp ret 8 ArrayFill ENDP offset(array) count EBP [EBP + 8] [EBP + 12] return address EBP
  • 27. Leave Instruction Assign the value of Base Pointer(EBP) in Stack Pointer(ESP) Pop EBP from stack leave is same as: mov esp,ebp pop ebp
  • 28. Local Variables A local variable is created, used, and destroyed within a single Procedure Two Methods in assembly: Reserve Memory in stack and using LOCAL Directive The LOCAL directive declares a list of local variables ◦ immediately follows the PROC directive ◦ each variable is assigned a type
  • 29. LOCAL directive Syntax: ◦ LOCAL varlist Example: MySub PROC LOCAL v1:BYTE, v2:WORD, v3:DWORD
  • 30. Local Variable MySub PROC push ebp mov ebp,esp sub esp,8 mov [ebp-4],123456h ; locA mov [ebp-8],0 ; locB . . To explicitly create local variables, subtract their total size from ESP. The following example creates and initializes two 32-bit local variables (we'll call them locA and locB): [EBP+8] Parameters [EBP+4 ] : Return Address EBP [EBP-4 ] : 123456 [EBP-8]: 0 ESP
  • 31. LOCAL Example BubbleSort PROC LOCAL temp:DWORD, SwapFlag:DWORD push ebp mov ebp,esp add esp,0FFFFFFF8h ; add -8 to ESP . . . mov esp,ebp pop ebp ret BubbleSort ENDP
  • 32. LOCAL Example return address EBP EBP [EBP - 4] ESP temp SwapFlag [EBP - 8] Diagram of the stack frame for the BubbleSort procedure:
  • 33. Another Example xyz proc local v1:byte , v2:byte , v3:dword push bp mov bp,sp sub sp,12 mov v1,21h mov v2,43h mov v3,44441111h mov sp,bp pop bp ret xyz endp
  • 34. Recursion: What is recursion? Recursively Calculating a Sum Calculating a Factorial
  • 35. What is Recursion? The process created when . . . ◦ A procedure calls itself repeatedly ◦ Procedure A calls procedure B, which in turn calls procedure A Using a graph in which each node is a procedure and each edge is a procedure call, recursion forms a cycle: A B D E C
  • 36. Example in C++ : recursion Void recursive(int n) { n = n-1; if(n != 0) Recursive(n); }
  • 37. Recursively Calculating a Sum The CalcSum procedure recursively calculates the sum of an array of integers. Receives: ECX = count. Returns: EAX = sum .code mov ecx, 5 mov eax,0 call CalcSum L1: ; any instructions CalcSum PROC cmp ecx,0 ; check counter value jz L2 ; quit if zero add eax,ecx ; otherwise, add to sum dec ecx ; decrement counter call CalcSum ; recursive call L2: ret CalcSum ENDP
  • 38. Calculating a Factorial (1 of 3) This function calculates the factorial of integer n. A new value of n is saved in each stack frame: int function factorial(int n) { if(n == 0) return 1; else return n * factorial(n-1); } 5! = 5 * 4! 4! = 4 * 3! 3! = 3 * 2! 2! = 2 * 1! 1! = 1 * 0! 0! = 1 (base case) 1 * 1 = 1 2 * 1 = 2 3 * 2 = 6 4 * 6 = 24 5 * 24 = 120 1 = 1 recursive calls backing up As each call instance returns, the product it returns is multiplied by the previous value of n.
  • 39. Calculating a Factorial (2 of 3) Factorial PROC push ebp mov ebp,esp mov eax,[ebp+8] ; get n cmp eax,0 ; n < 0? ja L1 ; yes: continue mov eax,1 ; no: return 1 jmp L2 L1: dec eax push eax ; Factorial(n-1) call Factorial ; Instructions from this point on execute when each ; recursive call returns. ReturnFact: mov ebx,[ebp+8] ; get n mul ebx ; eax = eax * ebx L2: pop ebp ; return EAX ret 4 ; clean up stack Factorial ENDP
  • 40. Calculating a Factorial (3 of 3) Suppose we want to calculate 12! This diagram shows the first few stack frames created by recursive calls to Factorial Each recursive call uses 12 bytes of stack space. 12 n n-1 ReturnMain ebp0 11 ReturnFact ebp1 10 ReturnFact ebp2 9 ReturnFact ebp3 n-2 n-3 (etc...)
  • 41. Alert! Using a corrupt recursive function can cause your program damage Stack Overflow will take place It can also effect other data on the stack
  • 42. Multimodule Programs A multimodule program is a program whose source code has been divided up into separate ASM files. Each ASM file (module) is assembled into a separate OBJ file. All OBJ files belonging to the same program are linked using the link utility into a single EXE file. ◦ This process is called static linking
  • 43. Advantages Large programs are easier to write, maintain, and debug when divided into separate source code modules. • When changing a line of code, only its enclosing module needs to be assembled again. Linking assembled modules requires little time. • A module can be a container for logically related code and data (think object-oriented here...) • encapsulation: procedures and variables are automatically hidden in a module unless you declare them public
  • 44. Creating a Multimodule Program Here are some basic steps to follow when creating a multimodule program: ◦ Create the main module ◦ Create a separate source code module for each procedure or set of related procedures ◦ Create an include file that contains procedure prototypes for external procedures (ones that are called between modules) ◦ Use the INCLUDE directive to make your procedure prototypes available to each module
  • 45. Example: ArraySum Program Let's review the ArraySum. Summation Program (main) Clrscr PromptForIntegers ArraySum DisplaySum WriteStringWriteString ReadInt WriteIntWriteInt Each of the four white rectangles will become a module.