SlideShare a Scribd company logo
1 of 11
Download to read offline
1
Systems Design & Programming CMPE 310Arithmetic, Logic and Control Instructions
Intel Assembly
Arithmetic Operations:
Addition
Subtraction
Multiplication
Division
Comparison
Negation
Increment
Decrement
Logic Operations:
AND
OR
XOR
NOT
shift
rotate
compare (test)
2
Systems Design & Programming CMPE 310Arithmetic, Logic and Control Instructions
Arithmetic Operations
Addition, Increment, Add-with-carry and Exchange-and-add:
Contents of the rightmost 8 bits of the FLAGS register can change (+ Overflow) for
arithmetic and logic instructions.
Flags include:
Z (result zero)
C (carry out)
A (half carry out)
S (result positive)
P (result has even parity)
O (overflow occurred)
add al, [ARRAY + esi]
;Adds 1 to any reg/mem except seginc byte [edi]
;Adds registers + Carry flag.adc ecx, ebx
;ecx=ecx+ebx, ebx=original ecx.xadd ecx, ebx
;Used for adding 64 bit nums.
3
Systems Design & Programming CMPE 310Arithmetic, Logic and Control Instructions
Arithmetic Operations
Subtraction, Decrement and Subtract-with-borrow:
Comparison:
Changes only the flag bits.
Often followed with a conditional branch:
sub eax, ebx
dec edi
;Subs registers - Carry flag.sbb ecx, ebx
;eax=eax-ebx
cmp al, 10H
jae LABEL1
;Jump if equal or below.jbe LABEL2
;if ecx==eax, eax=edx else eax=ecxcmpxchg ecx, edx
;Jump if equal or above.
4
Systems Design & Programming CMPE 310Arithmetic, Logic and Control Instructions
Arithmetic Operations
Multiplication and Division:
imul/idiv: Signed integer multiplication/division.
mul/div: Unsigned.
al always holds the multiplicand (or ax or eax).
Result is placed in ax (or dx and ax or edx or eax).
C and O bits are cleared if most significant 8 bits of the 16-bit product are zero
(result of an 8-bit multiplication is an 8-bit result).
Division by zero and overflow generate errors.
Overflow occurs when a small number divides a large dividend.
mul bl
imul bx
;Special, cx=dx*12H (signed only)imul cx, dx, 12H
;edx|eax=eax*ecxmul ecx
;ax=al*bl (unsigned)
;dx|ax=ax*bx (signed)
div cl
;dx|ax=(dx|ax)/cxidiv cx
;ah|al=ax/cl, unsigned quotient
; in al, remainder in ah
5
Systems Design & Programming CMPE 310Arithmetic, Logic and Control Instructions
Logic Operations
Allow bits to be set, cleared and complemented.
Commonly used to control I/O devices.
Logic operations always clear the carry and overflow flags.
AND: 0 AND anything is 0.
Commonly used with a MASK to clear bits:
OR: 1 OR anything is 1.
Commonly used with a MASK to set bits:
XXXX XXXX Operand
0000 1111 Mask
0000 XXXX Result
and al, bl ;al=al AND bl
XXXX XXXX Operand
0000 1111 Mask
XXXX 1111 Result
or eax, 10 ;eax=eax OR 0000000AH
6
Systems Design & Programming CMPE 310Arithmetic, Logic and Control Instructions
Logic Operations
XOR: Truth table: 0110.
Commonly used with a MASK to complement bits:
TEST: Operates like the AND but doesn't effect the destination.
Sets the Z flag to the complement of the bit being tested:
BT: Test the bit, BTC: Tests and complements...
NOT (logical one's complement)
NEG (arithmetic two's complement - sign of number inverted)
XXXX XXXX Operand
0000 1111 Mask
XXXX XXXX Result
xor ah, ch ;ah=ah XOR ch
test al, 4
jz LABEL ;Jump to LABEL if bit 2 is zero.
;Tests bit 2 in al -- 00000100
not ebx
neg TEMP
7
Systems Design & Programming CMPE 310Arithmetic, Logic and Control Instructions
Logic Operations
Shift: Logical shifts insert 0, arithmetic right shifts insert sign bit.
Double precision shifts (80386 and up):
Rotate: Rotates bits from one end to the other or through the carry flag.
Commonly used to operate on numbers wider than 32-bits:
shl eax, 1
sar esi, cl
;eax is logically shifted left 1 bit pos.
;esi is arithmetically shifted right
shdr eax, ebx, 12 ;eax shifted right by 12 and filled
;from the left with the right
shdl ax, bx, 14
;12 bits of ebx.
rol si, 14
rcr bl, cl
;si rotated left by 14 places.
;bl rotated right cl places through carry.
shl ax, 1 ;Original 48-bit number in dx, bx and ax.
;Shift ax left 1 binary place.
rcl bx, 1 ;Rotate carry bit from previous shl into
;low order bit of bx.
rcl dx, 1 ;Rotate carry bit from previous rcl in dx.
8
Systems Design & Programming CMPE 310Arithmetic, Logic and Control Instructions
Bit/String Scan
Bit Scan Instruction (80386 and up):
Scan through an operand searching for a 1 bit.
Zero flag is set if a 1 bit is found, position of bit is saved in destination register.
String Scan Instructions:
scasb/w/d compares the al/ax/eax register with a byte block of memory and sets the
flags.
Often used with repe and repne
cmpsb/w/d compares 2 sections of memory data.
bsl ebx, eax
bsr bl, cl
;eax scanned from the left.
;cl scanned from the right.
9
Systems Design & Programming CMPE 310Arithmetic, Logic and Control Instructions
Program Control Instructions
Conditional and Unconditional Jumps, Calls, Returns, Interrupts
Unconditional Jumps
Short jump: PC-relative using two bytes (+127/-128 bytes).
(PC-relative: constant added to eip).
Near jump:
Within segment (max of +/- 2G).
Far jump:
Four bytes give the offset and two bytes give a new segment address.
The segment value refers to a descriptor in protected mode.
jmp short NEXT
NEXT: add ax, bx
;short keyword is optional.
jmp near eax ;Jump to address given by eax.
jmp [eax] ;Jump to address given by [ax].
jmp far LABEL ;Jump to address given by LABEL.
10
Systems Design & Programming CMPE 310Arithmetic, Logic and Control Instructions
Flow-of-Control Instructions
Conditional Jumps:
Test flag bits S, Z, C, P and O.
For unsigned numbers:
For signed numbers
For either signed or unsigned:
Test cx instead of flags:
ja ;Jump if above (Z=0 and C=0)
;Jump if below or equal (Z=1 or C=1)jbe
;Jump if >= (S=O)jge
;Jump if < (S<>O)jl
;Jump if != (Z=0)jne
;Jump if ==; or jump if zero (Z=1)je or jz
;Jump if carry set (C=1)jc
;Jump if cx==0jcxz
;Jump if ecx==0jecxz
11
Systems Design & Programming CMPE 310Arithmetic, Logic and Control Instructions
Flow-of-Control Instructions
Conditional Set instructions:
Set a byte to either 01H or 00H, depending on the outcome of condition under test.
LOOP Instruction:
Combination of decrement ecx and jnz conditional jump.
Decrement ecx
If ecx != 0, jump to label
else fall through.
Example
;Set al=1 if >than (test Z==0 AND S==0)setg al
;else set al to 0
;Jump if ecx != 0loop LABEL
;Jump if (Z = 1 AND ecx != 0)loope
;Jump if (Z = 0 AND ecx != 0)loopne

More Related Content

What's hot

Lec12 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Ad...
Lec12 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Ad...Lec12 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Ad...
Lec12 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Ad...Hsien-Hsin Sean Lee, Ph.D.
 
Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2Iffat Anjum
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsAfaq Mansoor Khan
 
Infix prefix postfix
Infix prefix postfixInfix prefix postfix
Infix prefix postfixSelf-Employed
 
Expression evaluation
Expression evaluationExpression evaluation
Expression evaluationJeeSa Sultana
 
Infix to-postfix examples
Infix to-postfix examplesInfix to-postfix examples
Infix to-postfix examplesmua99
 
Assembly language-lab9
Assembly language-lab9Assembly language-lab9
Assembly language-lab9AjEcuacion
 
assembly language programming organization of IBM PC chapter 9 part-2(decimal...
assembly language programming organization of IBM PC chapter 9 part-2(decimal...assembly language programming organization of IBM PC chapter 9 part-2(decimal...
assembly language programming organization of IBM PC chapter 9 part-2(decimal...Bilal Amjad
 
Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)Ahmed Khateeb
 
Memory efficient pytorch
Memory efficient pytorchMemory efficient pytorch
Memory efficient pytorchHyungjoo Cho
 
Verilog VHDL code Parallel adder
Verilog VHDL code Parallel adder Verilog VHDL code Parallel adder
Verilog VHDL code Parallel adder Bharti Airtel Ltd.
 
Lec9 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Com...
Lec9 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Com...Lec9 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Com...
Lec9 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Com...Hsien-Hsin Sean Lee, Ph.D.
 

What's hot (19)

Lec12 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Ad...
Lec12 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Ad...Lec12 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Ad...
Lec12 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Ad...
 
Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2
 
Prefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix NotationsPrefix, Infix and Post-fix Notations
Prefix, Infix and Post-fix Notations
 
Infix prefix postfix
Infix prefix postfixInfix prefix postfix
Infix prefix postfix
 
Expression evaluation
Expression evaluationExpression evaluation
Expression evaluation
 
Infix to-postfix examples
Infix to-postfix examplesInfix to-postfix examples
Infix to-postfix examples
 
Assembly language-lab9
Assembly language-lab9Assembly language-lab9
Assembly language-lab9
 
Data structures
Data structures Data structures
Data structures
 
VERILOG CODE FOR Adder
VERILOG CODE FOR AdderVERILOG CODE FOR Adder
VERILOG CODE FOR Adder
 
assembly language programming organization of IBM PC chapter 9 part-2(decimal...
assembly language programming organization of IBM PC chapter 9 part-2(decimal...assembly language programming organization of IBM PC chapter 9 part-2(decimal...
assembly language programming organization of IBM PC chapter 9 part-2(decimal...
 
Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)
 
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
2.2 stack applications Infix to Postfix & Evaluation of Post Fix2.2 stack applications Infix to Postfix & Evaluation of Post Fix
2.2 stack applications Infix to Postfix & Evaluation of Post Fix
 
Memory efficient pytorch
Memory efficient pytorchMemory efficient pytorch
Memory efficient pytorch
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Context free grammar
Context free grammar Context free grammar
Context free grammar
 
Verilog VHDL code Parallel adder
Verilog VHDL code Parallel adder Verilog VHDL code Parallel adder
Verilog VHDL code Parallel adder
 
stack
stackstack
stack
 
Lec9 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Com...
Lec9 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Com...Lec9 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Com...
Lec9 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Com...
 
Topdown parsing
Topdown parsingTopdown parsing
Topdown parsing
 

Similar to Lab lect03 arith_control

EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5PRADEEP
 
Intel codetable
Intel codetableIntel codetable
Intel codetableJ R7
 
Instruction 4.pptx
Instruction 4.pptxInstruction 4.pptx
Instruction 4.pptxHebaEng
 
system software 16 marks
system software 16 markssystem software 16 marks
system software 16 marksvvcetit
 
Computer Organization And Architecture lab manual
Computer Organization And Architecture lab manualComputer Organization And Architecture lab manual
Computer Organization And Architecture lab manualNitesh Dubey
 
VHDL Packages, Coding Styles for Arithmetic Operations and VHDL-200x Additions
VHDL Packages, Coding Styles for Arithmetic Operations and VHDL-200x AdditionsVHDL Packages, Coding Styles for Arithmetic Operations and VHDL-200x Additions
VHDL Packages, Coding Styles for Arithmetic Operations and VHDL-200x AdditionsAmal Khailtash
 
Introduction of 8086 micro processor .
Introduction of 8086 micro processor .Introduction of 8086 micro processor .
Introduction of 8086 micro processor .Siraj Ahmed
 
N_Asm Assembly numbers (sol)
N_Asm Assembly numbers (sol)N_Asm Assembly numbers (sol)
N_Asm Assembly numbers (sol)Selomon birhane
 
Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 8086Vijay Kumar
 
Instructionsetof8086 180224060745(3)
Instructionsetof8086 180224060745(3)Instructionsetof8086 180224060745(3)
Instructionsetof8086 180224060745(3)AmitPaliwal20
 
Assembly language programs 2
Assembly language programs 2Assembly language programs 2
Assembly language programs 2HarshitParkar6677
 

Similar to Lab lect03 arith_control (20)

Al2ed chapter7
Al2ed chapter7Al2ed chapter7
Al2ed chapter7
 
EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5
 
Intel codetable
Intel codetableIntel codetable
Intel codetable
 
[ASM]Lab4
[ASM]Lab4[ASM]Lab4
[ASM]Lab4
 
Instruction 4.pptx
Instruction 4.pptxInstruction 4.pptx
Instruction 4.pptx
 
Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 8086
 
system software 16 marks
system software 16 markssystem software 16 marks
system software 16 marks
 
Lec06
Lec06Lec06
Lec06
 
Chapt 06
Chapt 06Chapt 06
Chapt 06
 
Chapt 06
Chapt 06Chapt 06
Chapt 06
 
X86 operation types
X86 operation typesX86 operation types
X86 operation types
 
Computer Organization And Architecture lab manual
Computer Organization And Architecture lab manualComputer Organization And Architecture lab manual
Computer Organization And Architecture lab manual
 
Assembly language programs
Assembly language programsAssembly language programs
Assembly language programs
 
VHDL Packages, Coding Styles for Arithmetic Operations and VHDL-200x Additions
VHDL Packages, Coding Styles for Arithmetic Operations and VHDL-200x AdditionsVHDL Packages, Coding Styles for Arithmetic Operations and VHDL-200x Additions
VHDL Packages, Coding Styles for Arithmetic Operations and VHDL-200x Additions
 
Introduction of 8086 micro processor .
Introduction of 8086 micro processor .Introduction of 8086 micro processor .
Introduction of 8086 micro processor .
 
[ASM]Lab7
[ASM]Lab7[ASM]Lab7
[ASM]Lab7
 
N_Asm Assembly numbers (sol)
N_Asm Assembly numbers (sol)N_Asm Assembly numbers (sol)
N_Asm Assembly numbers (sol)
 
Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 8086
 
Instructionsetof8086 180224060745(3)
Instructionsetof8086 180224060745(3)Instructionsetof8086 180224060745(3)
Instructionsetof8086 180224060745(3)
 
Assembly language programs 2
Assembly language programs 2Assembly language programs 2
Assembly language programs 2
 

Recently uploaded

Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsPrecisely
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 

Recently uploaded (20)

Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Unlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power SystemsUnlocking the Potential of the Cloud for IBM Power Systems
Unlocking the Potential of the Cloud for IBM Power Systems
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 

Lab lect03 arith_control

  • 1. 1 Systems Design & Programming CMPE 310Arithmetic, Logic and Control Instructions Intel Assembly Arithmetic Operations: Addition Subtraction Multiplication Division Comparison Negation Increment Decrement Logic Operations: AND OR XOR NOT shift rotate compare (test)
  • 2. 2 Systems Design & Programming CMPE 310Arithmetic, Logic and Control Instructions Arithmetic Operations Addition, Increment, Add-with-carry and Exchange-and-add: Contents of the rightmost 8 bits of the FLAGS register can change (+ Overflow) for arithmetic and logic instructions. Flags include: Z (result zero) C (carry out) A (half carry out) S (result positive) P (result has even parity) O (overflow occurred) add al, [ARRAY + esi] ;Adds 1 to any reg/mem except seginc byte [edi] ;Adds registers + Carry flag.adc ecx, ebx ;ecx=ecx+ebx, ebx=original ecx.xadd ecx, ebx ;Used for adding 64 bit nums.
  • 3. 3 Systems Design & Programming CMPE 310Arithmetic, Logic and Control Instructions Arithmetic Operations Subtraction, Decrement and Subtract-with-borrow: Comparison: Changes only the flag bits. Often followed with a conditional branch: sub eax, ebx dec edi ;Subs registers - Carry flag.sbb ecx, ebx ;eax=eax-ebx cmp al, 10H jae LABEL1 ;Jump if equal or below.jbe LABEL2 ;if ecx==eax, eax=edx else eax=ecxcmpxchg ecx, edx ;Jump if equal or above.
  • 4. 4 Systems Design & Programming CMPE 310Arithmetic, Logic and Control Instructions Arithmetic Operations Multiplication and Division: imul/idiv: Signed integer multiplication/division. mul/div: Unsigned. al always holds the multiplicand (or ax or eax). Result is placed in ax (or dx and ax or edx or eax). C and O bits are cleared if most significant 8 bits of the 16-bit product are zero (result of an 8-bit multiplication is an 8-bit result). Division by zero and overflow generate errors. Overflow occurs when a small number divides a large dividend. mul bl imul bx ;Special, cx=dx*12H (signed only)imul cx, dx, 12H ;edx|eax=eax*ecxmul ecx ;ax=al*bl (unsigned) ;dx|ax=ax*bx (signed) div cl ;dx|ax=(dx|ax)/cxidiv cx ;ah|al=ax/cl, unsigned quotient ; in al, remainder in ah
  • 5. 5 Systems Design & Programming CMPE 310Arithmetic, Logic and Control Instructions Logic Operations Allow bits to be set, cleared and complemented. Commonly used to control I/O devices. Logic operations always clear the carry and overflow flags. AND: 0 AND anything is 0. Commonly used with a MASK to clear bits: OR: 1 OR anything is 1. Commonly used with a MASK to set bits: XXXX XXXX Operand 0000 1111 Mask 0000 XXXX Result and al, bl ;al=al AND bl XXXX XXXX Operand 0000 1111 Mask XXXX 1111 Result or eax, 10 ;eax=eax OR 0000000AH
  • 6. 6 Systems Design & Programming CMPE 310Arithmetic, Logic and Control Instructions Logic Operations XOR: Truth table: 0110. Commonly used with a MASK to complement bits: TEST: Operates like the AND but doesn't effect the destination. Sets the Z flag to the complement of the bit being tested: BT: Test the bit, BTC: Tests and complements... NOT (logical one's complement) NEG (arithmetic two's complement - sign of number inverted) XXXX XXXX Operand 0000 1111 Mask XXXX XXXX Result xor ah, ch ;ah=ah XOR ch test al, 4 jz LABEL ;Jump to LABEL if bit 2 is zero. ;Tests bit 2 in al -- 00000100 not ebx neg TEMP
  • 7. 7 Systems Design & Programming CMPE 310Arithmetic, Logic and Control Instructions Logic Operations Shift: Logical shifts insert 0, arithmetic right shifts insert sign bit. Double precision shifts (80386 and up): Rotate: Rotates bits from one end to the other or through the carry flag. Commonly used to operate on numbers wider than 32-bits: shl eax, 1 sar esi, cl ;eax is logically shifted left 1 bit pos. ;esi is arithmetically shifted right shdr eax, ebx, 12 ;eax shifted right by 12 and filled ;from the left with the right shdl ax, bx, 14 ;12 bits of ebx. rol si, 14 rcr bl, cl ;si rotated left by 14 places. ;bl rotated right cl places through carry. shl ax, 1 ;Original 48-bit number in dx, bx and ax. ;Shift ax left 1 binary place. rcl bx, 1 ;Rotate carry bit from previous shl into ;low order bit of bx. rcl dx, 1 ;Rotate carry bit from previous rcl in dx.
  • 8. 8 Systems Design & Programming CMPE 310Arithmetic, Logic and Control Instructions Bit/String Scan Bit Scan Instruction (80386 and up): Scan through an operand searching for a 1 bit. Zero flag is set if a 1 bit is found, position of bit is saved in destination register. String Scan Instructions: scasb/w/d compares the al/ax/eax register with a byte block of memory and sets the flags. Often used with repe and repne cmpsb/w/d compares 2 sections of memory data. bsl ebx, eax bsr bl, cl ;eax scanned from the left. ;cl scanned from the right.
  • 9. 9 Systems Design & Programming CMPE 310Arithmetic, Logic and Control Instructions Program Control Instructions Conditional and Unconditional Jumps, Calls, Returns, Interrupts Unconditional Jumps Short jump: PC-relative using two bytes (+127/-128 bytes). (PC-relative: constant added to eip). Near jump: Within segment (max of +/- 2G). Far jump: Four bytes give the offset and two bytes give a new segment address. The segment value refers to a descriptor in protected mode. jmp short NEXT NEXT: add ax, bx ;short keyword is optional. jmp near eax ;Jump to address given by eax. jmp [eax] ;Jump to address given by [ax]. jmp far LABEL ;Jump to address given by LABEL.
  • 10. 10 Systems Design & Programming CMPE 310Arithmetic, Logic and Control Instructions Flow-of-Control Instructions Conditional Jumps: Test flag bits S, Z, C, P and O. For unsigned numbers: For signed numbers For either signed or unsigned: Test cx instead of flags: ja ;Jump if above (Z=0 and C=0) ;Jump if below or equal (Z=1 or C=1)jbe ;Jump if >= (S=O)jge ;Jump if < (S<>O)jl ;Jump if != (Z=0)jne ;Jump if ==; or jump if zero (Z=1)je or jz ;Jump if carry set (C=1)jc ;Jump if cx==0jcxz ;Jump if ecx==0jecxz
  • 11. 11 Systems Design & Programming CMPE 310Arithmetic, Logic and Control Instructions Flow-of-Control Instructions Conditional Set instructions: Set a byte to either 01H or 00H, depending on the outcome of condition under test. LOOP Instruction: Combination of decrement ecx and jnz conditional jump. Decrement ecx If ecx != 0, jump to label else fall through. Example ;Set al=1 if >than (test Z==0 AND S==0)setg al ;else set al to 0 ;Jump if ecx != 0loop LABEL ;Jump if (Z = 1 AND ecx != 0)loope ;Jump if (Z = 0 AND ecx != 0)loopne