SlideShare une entreprise Scribd logo
1  sur  10
Addition and Subtraction
 INC and DEC Instructions
 ADD Instruction
 SUB Instruction
 NEG Instruction
 Implementing Arithmetic Expressions
 Flags Affected by Addition and
Subtraction
 Example Program (AddSub3)
INC and DEC Instructions
• The INC (increment) and DEC (decrement) instructions,
respectively, add 1 and subtract 1 from
a single operand. The Example is
.data
myWord WORD 1000h
.code
inc myWord ; myWord =1001h
mov bx,myWord
dec bx ; BX = 1000h
ADD Instruction
• The ADD instruction adds a source operand to a destination
operand of the same size. The Example is
.data
var1 DWORD 10000h
var2 DWORD 20000h
.code
mov eax,var1 ; EAX = 10000h
add eax,var2 ; EAX = 30000h
SUB Instruction
• The SUB instruction subtracts a source operand from a
destination operand
.data
var1 DWORD 30000h
var2 DWORD 10000h
.code
mov eax,var1 ; EAX = 30000h
sub eax,var2 ; EAX = 20000h
NEG Instruction
• The NEG (negate) instruction reverses the sign of a number by
converting the number to its
two’s complement.
NEG reg
NEG mem
(Recall that the two’s complement of a number can be found
by reversing all the bits in the destination operand and adding
1.)
Implementing Arithmetic Expressions
Rval = -Xval + (Yval - Zval);
Rval SDWORD ?
Xval SDWORD 26
Yval SDWORD 30
Zval SDWORD 40
; first term: -Xval
mov eax , Xval
neg eax ; EAX = -26
Then Yval is copied to a register and Zval is subtracted:
; second term: (Yval - Zval)
mov ebx,Yval
sub ebx,Zval ; EBX = -10
Finally, the two terms (in EAX and EBX) are added:
; add the terms and store:
add eax , ebx
mov Rval , eax ; Rval = -36
Unsigned Operations: Zero And Carry
The Zero flag is set when the result of an arithmetic operation is zero.
For Example
mov ecx,1
sub ecx,1 ; ECX = 0, ZF = 1
mov eax,0FFFFFFFFh
inc eax ; EAX = 0, ZF = 1
inc eax ; EAX = 1, ZF = 0
dec eax ; EAX = 0, ZF = 1
Addition and the Carry Flag
When adding two unsigned integers, the Carry flag is a copy of the
carry out of the MSB of the destination operand. Intuitively, we can
say CF 1 when the
sum exceeds the storage size of its destination operand.
mov al,FFh
add al,1 ; AL = 00, CF = 1
1 1 1 1 1 1
+
CF
1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 01
Subtraction and the Carry Flag
• A subtract operation sets the Carry flag when a larger
unsigned integer is subtracted from a smaller one.
mov al,1
sub al,2 ; AL = FFh , CF = 1
(1)
+ (-2)
CF (FFh)
0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 11
Signed Operations: Sign and Overflow
Flags
The Sign flag is set when the result of a signed arithmetic operation
is negative.
mov eax,4
sub eax,5 ; EAX = -1, SF = 1
Overflow Flag
largest possible integer signed byte value is +127 ; adding 1 to it
causes overflow:
mov al,+127
add al,1 ; OF = 1
the smallest possible negative integer byte value is 128. Subtracting
1 from it causes
underflow.
mov al,-128
sub al,1 ; OF = 1

Contenu connexe

Tendances

Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 80869840596838
 
8257 DMA Controller
8257 DMA Controller8257 DMA Controller
8257 DMA ControllerShivamSood22
 
Chapter 3 INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING
Chapter 3 INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMINGChapter 3 INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING
Chapter 3 INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMINGFrankie Jones
 
bus and memory tranfer (computer organaization)
bus and memory tranfer (computer organaization)bus and memory tranfer (computer organaization)
bus and memory tranfer (computer organaization)Siddhi Viradiya
 
chapter 7 Logic, shift and rotate instructions
chapter 7 Logic, shift and rotate instructionschapter 7 Logic, shift and rotate instructions
chapter 7 Logic, shift and rotate instructionswarda aziz
 
Chapter 5The proessor status and the FLAGS registers
Chapter 5The proessor status and the FLAGS registersChapter 5The proessor status and the FLAGS registers
Chapter 5The proessor status and the FLAGS registerswarda aziz
 
Memory Reference Instructions
Memory Reference InstructionsMemory Reference Instructions
Memory Reference InstructionsRabin BK
 
Interfacing memory with 8086 microprocessor
Interfacing memory with 8086 microprocessorInterfacing memory with 8086 microprocessor
Interfacing memory with 8086 microprocessorVikas Gupta
 
Computer instructions
Computer instructionsComputer instructions
Computer instructionsAnuj Modi
 
Addressing modes of 80386
Addressing modes of 80386Addressing modes of 80386
Addressing modes of 80386PDFSHARE
 
Memory & I/O interfacing
Memory & I/O  interfacingMemory & I/O  interfacing
Memory & I/O interfacingdeval patel
 
Ascii adjust & decimal adjust
Ascii adjust & decimal adjustAscii adjust & decimal adjust
Ascii adjust & decimal adjustTech_MX
 
Instruction Set Architecture
Instruction Set ArchitectureInstruction Set Architecture
Instruction Set ArchitectureDilum Bandara
 
Computer Organization and Architecture.
Computer Organization and Architecture.Computer Organization and Architecture.
Computer Organization and Architecture.CS_GDRCST
 
Memory organisation ppt final presentation
Memory organisation ppt final presentationMemory organisation ppt final presentation
Memory organisation ppt final presentationrockymani
 

Tendances (20)

Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 8086
 
8257 DMA Controller
8257 DMA Controller8257 DMA Controller
8257 DMA Controller
 
Chapter 3 INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING
Chapter 3 INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMINGChapter 3 INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING
Chapter 3 INSTRUCTION SET AND ASSEMBLY LANGUAGE PROGRAMMING
 
Unit 4-booth algorithm
Unit 4-booth algorithmUnit 4-booth algorithm
Unit 4-booth algorithm
 
Presentation on risc pipeline
Presentation on risc pipelinePresentation on risc pipeline
Presentation on risc pipeline
 
bus and memory tranfer (computer organaization)
bus and memory tranfer (computer organaization)bus and memory tranfer (computer organaization)
bus and memory tranfer (computer organaization)
 
chapter 7 Logic, shift and rotate instructions
chapter 7 Logic, shift and rotate instructionschapter 7 Logic, shift and rotate instructions
chapter 7 Logic, shift and rotate instructions
 
Chapter 5The proessor status and the FLAGS registers
Chapter 5The proessor status and the FLAGS registersChapter 5The proessor status and the FLAGS registers
Chapter 5The proessor status and the FLAGS registers
 
8051 instruction set
8051 instruction set8051 instruction set
8051 instruction set
 
Memory Reference Instructions
Memory Reference InstructionsMemory Reference Instructions
Memory Reference Instructions
 
Interfacing memory with 8086 microprocessor
Interfacing memory with 8086 microprocessorInterfacing memory with 8086 microprocessor
Interfacing memory with 8086 microprocessor
 
Computer instructions
Computer instructionsComputer instructions
Computer instructions
 
Addressing modes of 80386
Addressing modes of 80386Addressing modes of 80386
Addressing modes of 80386
 
Memory & I/O interfacing
Memory & I/O  interfacingMemory & I/O  interfacing
Memory & I/O interfacing
 
8086 memory segmentation
8086 memory segmentation8086 memory segmentation
8086 memory segmentation
 
Ascii adjust & decimal adjust
Ascii adjust & decimal adjustAscii adjust & decimal adjust
Ascii adjust & decimal adjust
 
Instruction formats-in-8086
Instruction formats-in-8086Instruction formats-in-8086
Instruction formats-in-8086
 
Instruction Set Architecture
Instruction Set ArchitectureInstruction Set Architecture
Instruction Set Architecture
 
Computer Organization and Architecture.
Computer Organization and Architecture.Computer Organization and Architecture.
Computer Organization and Architecture.
 
Memory organisation ppt final presentation
Memory organisation ppt final presentationMemory organisation ppt final presentation
Memory organisation ppt final presentation
 

Similaire à Assembly language (addition and subtraction)

Chap 3_2.ppt
Chap 3_2.pptChap 3_2.ppt
Chap 3_2.pptinian2
 
Instruction 8.pptx
Instruction 8.pptxInstruction 8.pptx
Instruction 8.pptxHebaEng
 
Intel codetable
Intel codetableIntel codetable
Intel codetableJ R7
 
Microprocssor
MicroprocssorMicroprocssor
Microprocssorriyadh8
 
instruction-set-of-8086-mr-binu-joy3.ppt
instruction-set-of-8086-mr-binu-joy3.pptinstruction-set-of-8086-mr-binu-joy3.ppt
instruction-set-of-8086-mr-binu-joy3.pptssuser2b759d
 
NSC #2 - D1 01 - Rolf Rolles - Program synthesis in reverse engineering
NSC #2 - D1 01 - Rolf Rolles - Program synthesis in reverse engineeringNSC #2 - D1 01 - Rolf Rolles - Program synthesis in reverse engineering
NSC #2 - D1 01 - Rolf Rolles - Program synthesis in reverse engineeringNoSuchCon
 
Assembly language-lab9
Assembly language-lab9Assembly language-lab9
Assembly language-lab9AjEcuacion
 
Low-level Shader Optimization for Next-Gen and DX11 by Emil Persson
Low-level Shader Optimization for Next-Gen and DX11 by Emil PerssonLow-level Shader Optimization for Next-Gen and DX11 by Emil Persson
Low-level Shader Optimization for Next-Gen and DX11 by Emil PerssonAMD Developer Central
 
Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 8086Vijay Kumar
 

Similaire à Assembly language (addition and subtraction) (20)

[ASM]Lab4
[ASM]Lab4[ASM]Lab4
[ASM]Lab4
 
Chap 3_2.ppt
Chap 3_2.pptChap 3_2.ppt
Chap 3_2.ppt
 
[ASM]Lab7
[ASM]Lab7[ASM]Lab7
[ASM]Lab7
 
8086 instruction set
8086  instruction set8086  instruction set
8086 instruction set
 
Instruction set of 8086 Microprocessor
Instruction set of 8086 Microprocessor Instruction set of 8086 Microprocessor
Instruction set of 8086 Microprocessor
 
Assignment on alp
Assignment on alpAssignment on alp
Assignment on alp
 
Assignment on alp
Assignment on alpAssignment on alp
Assignment on alp
 
Arithmetic instrctions
Arithmetic instrctionsArithmetic instrctions
Arithmetic instrctions
 
Instruction 8.pptx
Instruction 8.pptxInstruction 8.pptx
Instruction 8.pptx
 
Al2ed chapter7
Al2ed chapter7Al2ed chapter7
Al2ed chapter7
 
Intel codetable
Intel codetableIntel codetable
Intel codetable
 
8086 Instruction set
8086 Instruction set8086 Instruction set
8086 Instruction set
 
Microprocssor
MicroprocssorMicroprocssor
Microprocssor
 
instruction-set-of-8086-mr-binu-joy3.ppt
instruction-set-of-8086-mr-binu-joy3.pptinstruction-set-of-8086-mr-binu-joy3.ppt
instruction-set-of-8086-mr-binu-joy3.ppt
 
NSC #2 - D1 01 - Rolf Rolles - Program synthesis in reverse engineering
NSC #2 - D1 01 - Rolf Rolles - Program synthesis in reverse engineeringNSC #2 - D1 01 - Rolf Rolles - Program synthesis in reverse engineering
NSC #2 - D1 01 - Rolf Rolles - Program synthesis in reverse engineering
 
Chapter1c
Chapter1cChapter1c
Chapter1c
 
Assembly language-lab9
Assembly language-lab9Assembly language-lab9
Assembly language-lab9
 
Low-level Shader Optimization for Next-Gen and DX11 by Emil Persson
Low-level Shader Optimization for Next-Gen and DX11 by Emil PerssonLow-level Shader Optimization for Next-Gen and DX11 by Emil Persson
Low-level Shader Optimization for Next-Gen and DX11 by Emil Persson
 
Instruction set
Instruction setInstruction set
Instruction set
 
Instruction set of 8086
Instruction set of 8086Instruction set of 8086
Instruction set of 8086
 

Plus de Muhammad Umar Farooq (17)

Linear network
Linear networkLinear network
Linear network
 
Digital vs analogue
Digital vs analogueDigital vs analogue
Digital vs analogue
 
Project planning and scheduling
Project planning and schedulingProject planning and scheduling
Project planning and scheduling
 
It training
It trainingIt training
It training
 
Cyber crime ethics and un ethics
Cyber crime ethics and un ethicsCyber crime ethics and un ethics
Cyber crime ethics and un ethics
 
It usages & role
It usages &  roleIt usages &  role
It usages & role
 
Future prediction-ds
Future prediction-dsFuture prediction-ds
Future prediction-ds
 
Computer virus
Computer virusComputer virus
Computer virus
 
Singular and non singular matrix
Singular and non singular matrixSingular and non singular matrix
Singular and non singular matrix
 
Recursion
RecursionRecursion
Recursion
 
Ring
RingRing
Ring
 
Power
PowerPower
Power
 
Principal ideal
Principal idealPrincipal ideal
Principal ideal
 
Quotient ring
Quotient ringQuotient ring
Quotient ring
 
What is communication
What is communicationWhat is communication
What is communication
 
Ring homomorphism
Ring homomorphismRing homomorphism
Ring homomorphism
 
Leaner algebra presentation (ring)
Leaner algebra presentation (ring)Leaner algebra presentation (ring)
Leaner algebra presentation (ring)
 

Dernier

mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 

Dernier (20)

mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 

Assembly language (addition and subtraction)

  • 1. Addition and Subtraction  INC and DEC Instructions  ADD Instruction  SUB Instruction  NEG Instruction  Implementing Arithmetic Expressions  Flags Affected by Addition and Subtraction  Example Program (AddSub3)
  • 2. INC and DEC Instructions • The INC (increment) and DEC (decrement) instructions, respectively, add 1 and subtract 1 from a single operand. The Example is .data myWord WORD 1000h .code inc myWord ; myWord =1001h mov bx,myWord dec bx ; BX = 1000h
  • 3. ADD Instruction • The ADD instruction adds a source operand to a destination operand of the same size. The Example is .data var1 DWORD 10000h var2 DWORD 20000h .code mov eax,var1 ; EAX = 10000h add eax,var2 ; EAX = 30000h
  • 4. SUB Instruction • The SUB instruction subtracts a source operand from a destination operand .data var1 DWORD 30000h var2 DWORD 10000h .code mov eax,var1 ; EAX = 30000h sub eax,var2 ; EAX = 20000h
  • 5. NEG Instruction • The NEG (negate) instruction reverses the sign of a number by converting the number to its two’s complement. NEG reg NEG mem (Recall that the two’s complement of a number can be found by reversing all the bits in the destination operand and adding 1.)
  • 6. Implementing Arithmetic Expressions Rval = -Xval + (Yval - Zval); Rval SDWORD ? Xval SDWORD 26 Yval SDWORD 30 Zval SDWORD 40 ; first term: -Xval mov eax , Xval neg eax ; EAX = -26 Then Yval is copied to a register and Zval is subtracted: ; second term: (Yval - Zval) mov ebx,Yval sub ebx,Zval ; EBX = -10 Finally, the two terms (in EAX and EBX) are added: ; add the terms and store: add eax , ebx mov Rval , eax ; Rval = -36
  • 7. Unsigned Operations: Zero And Carry The Zero flag is set when the result of an arithmetic operation is zero. For Example mov ecx,1 sub ecx,1 ; ECX = 0, ZF = 1 mov eax,0FFFFFFFFh inc eax ; EAX = 0, ZF = 1 inc eax ; EAX = 1, ZF = 0 dec eax ; EAX = 0, ZF = 1
  • 8. Addition and the Carry Flag When adding two unsigned integers, the Carry flag is a copy of the carry out of the MSB of the destination operand. Intuitively, we can say CF 1 when the sum exceeds the storage size of its destination operand. mov al,FFh add al,1 ; AL = 00, CF = 1 1 1 1 1 1 1 + CF 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 01
  • 9. Subtraction and the Carry Flag • A subtract operation sets the Carry flag when a larger unsigned integer is subtracted from a smaller one. mov al,1 sub al,2 ; AL = FFh , CF = 1 (1) + (-2) CF (FFh) 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 11
  • 10. Signed Operations: Sign and Overflow Flags The Sign flag is set when the result of a signed arithmetic operation is negative. mov eax,4 sub eax,5 ; EAX = -1, SF = 1 Overflow Flag largest possible integer signed byte value is +127 ; adding 1 to it causes overflow: mov al,+127 add al,1 ; OF = 1 the smallest possible negative integer byte value is 128. Subtracting 1 from it causes underflow. mov al,-128 sub al,1 ; OF = 1