ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP3137Inc Dec Instructions
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP3137

    Computer Organization and Assembly Language

    Progress0 / 73 done
    Topics73
    1Introduction to Computer Organization2Assembly Language3Comparison of Low-Level and High-Level Languages
    4General Purpose and Special Purpose Registers5Introduction and Usage of RAM6Processor7Registers8System Bus9Instruction Execution Cycle10Assembly and Machine Language11Assembler12Linker and Link Libraries13Programmer's View of a Computer System14RISC and CISC Architecture15Physical Address Calculation16Basic Memory Organization17CPU Organization18Top Level View of Computer Function and Interconnection19Assembler Instruction Cycle20Execute Cycle21Interrupts22Interrupt Cycle23Memory Connection24Input/Output Connection25CPU Connection26MASM27MIPS28Defining Data in MASM Assembler29Elements of Assembly Language30Integer Constants31Integer Expressions32Real Number Constants33Character Constants34String Constants35Reserved Words36Identifiers37Directives38Instructions39The NOP (No Operation) Instruction40Adding and Subtracting Integer41INC and DEC Instructions42NEG Instruction43How to Move Integer Number in Register44Adding and Subtracting Numbers in Registers45Declaration and Initialization of Variables46Moving Data from Variable to Register47Data Definition Statement48BYTE and SBYTE Data49WORD and SWORD Data50Defining DWORD and SDWORD Data51Knowledge about Different Data Types52Operations, Array & Loops53Division and Multiplication in Assembly54Jumps Based on Specific Flags55Jumps Based on Equality56Simple Jump Statements57Jumps Based on Specific Condition58Code Examples59Practice on MASM60Procedures61File Operations Procedures62Labels in Procedures63Stack64Runtime Stack65Conditional Control Flow Directives66Compound Expressions67Data Representation & Conversion68Architecture69Data Path70Control Unit71Critical Path72General Principles of Pipelining73Pipelined Y86 Implementations
    7 min read
    Topic 41 of 73
    Topic Study Note Illustration
    41

    INC and DEC Instructions

    The INC (Increment) and DEC (Decrement) instructions are some of the most fundamental and frequently used operations in assembly language programming. These instructions are used to increase...

    INC and DEC Instructions in Assembly Language

    The INC (Increment) and DEC (Decrement) instructions are some of the most fundamental and frequently used operations in assembly language programming. These instructions are used to increase or decrease the value stored in a register or memory location by 1. They are very efficient because they are single-byte instructions in most assembly languages, making them quicker to execute compared to the ADD and SUB instructions when the increment or decrement is by 1.

    1. INC (Increment) Instruction

    The INC instruction increases the value of a register or memory location by 1. It is shorthand for adding 1 to the operand.

    Syntax:

    INC operand
    
    • operand: This is the register, memory location, or variable whose value will be increased by 1.

    The INC instruction automatically updates the Zero Flag (ZF) and the Sign Flag (SF) based on the result. It does not affect the carry flag (CF) directly. If the result of the increment is zero, the Zero Flag is set. If the result is negative (for signed numbers), the Sign Flag is set.

    Example of INC Instruction

    Example 1: Incrementing a Register

    MOV AX, 5    ; AX = 5
    INC AX       ; AX = 6 (AX is incremented by 1)
    

    In this example:

    • The value 5 is loaded into register AX.
    • The INC AX instruction increases the value in AX by 1, so now AX holds the value 6.

    Example 2: Incrementing a Memory Value

    MOV AX, [num]   ; Load the value from memory location 'num' into AX
    INC [num]       ; Increment the value at memory location 'num' by 1
    

    In this case, the value stored at the memory address num is incremented by 1.

    2. DEC (Decrement) Instruction

    The DEC instruction decreases the value of a register or memory location by 1. It is shorthand for subtracting 1 from the operand.

    Syntax:

    DEC operand
    
    • operand: This is the register, memory location, or variable whose value will be decreased by 1.

    The DEC instruction also updates the Zero Flag (ZF) and the Sign Flag (SF) based on the result. However, similar to INC, the carry flag (CF) is not affected by the DEC operation. If the result of the decrement is zero, the Zero Flag is set, and if the result is negative, the Sign Flag is set.

    Example of DEC Instruction

    Example 1: Decrementing a Register

    MOV AX, 10   ; AX = 10
    DEC AX       ; AX = 9  (AX is decremented by 1)
    

    In this example:

    • The value 10 is loaded into register AX.
    • The DEC AX instruction decreases the value in AX by 1, so now AX holds the value 9.

    Example 2: Decrementing a Memory Value

    MOV AX, [num]    ; Load the value from memory location 'num' into AX
    DEC [num]        ; Decrement the value at memory location 'num' by 1
    

    In this case, the value stored at the memory address num is decremented by 1.

    3. Flags Affected by INC and DEC

    • Zero Flag (ZF):

      • The Zero Flag is set if the result of the operation is zero. This happens after INC when the operand is decremented to zero, or after DEC when the operand is decremented to zero.
    • Sign Flag (SF):

      • The Sign Flag is set or cleared based on the most significant bit of the result. It reflects whether the result is positive or negative. If the result of the INC or DEC operation is negative (for signed integers), the SF is set.
    • Overflow Flag (OF):

      • The Overflow Flag is not affected by INC and DEC operations on unsigned integers. However, if signed integers are used and the result goes beyond the representable range, the OF will be set. For example, if a signed number is incremented beyond its maximum value or decremented below its minimum value, overflow will occur.
    • Carry Flag (CF):

      • The Carry Flag is not affected by INC or DEC. It remains unchanged because the operations only involve adding or subtracting a value of 1.

    4. Common Uses of INC and DEC

    Counting Loops

    The INC and DEC instructions are commonly used in loops where a counter is needed. For example, you can use DEC to count down to zero, or INC to count up.

    Example of a simple countdown using DEC:

    MOV CX, 10      ; Set counter to 10
    loop_start:
        DEC CX      ; Decrease CX by 1
        JNZ loop_start  ; Jump if CX is not zero (CX != 0)
    

    In this loop:

    • The counter CX starts at 10.
    • Each time through the loop, the value in CX is decremented by 1.
    • The JNZ (Jump if Not Zero) instruction causes the loop to continue until CX becomes 0.

    Adjusting Pointers

    INC and DEC are useful for adjusting pointers, especially when iterating through arrays or buffers.

    For example, incrementing a pointer in an array:

    MOV SI, 0        ; SI points to the first element of an array
    INC SI           ; Move to the next element in the array (assuming SI is an index register)
    

    In this case, SI points to the first element of an array. After the INC SI instruction, SI will point to the next element.

    Simple Arithmetic

    In certain cases, you can use INC and DEC to perform simple arithmetic, especially in tight loops or for simple algorithms that only need to add or subtract 1. They are efficient because they use fewer machine cycles than the more general ADD or SUB instructions.

    5. Comparison of INC and DEC with ADD and SUB

    While ADD and SUB are more general-purpose instructions for adding or subtracting arbitrary values, INC and DEC are specifically optimized for increments and decrements by 1. They are usually more efficient in terms of code size and execution time in these cases.

    • ADD and SUB can handle a wide range of values, not just 1.

      ADD AX, 5      ; AX = AX + 5
      SUB AX, 2      ; AX = AX - 2
      
    • INC and DEC are faster and take fewer machine cycles when you only need to add or subtract 1:

      INC AX         ; AX = AX + 1
      DEC AX         ; AX = AX - 1
      

    6. Conclusion

    • INC increases the value of a register or memory location by 1.
    • DEC decreases the value of a register or memory location by 1.
    • Both instructions affect the Zero Flag (ZF) and the Sign Flag (SF), but do not affect the Carry Flag (CF) or Overflow Flag (OF) directly.
    • INC and DEC are efficient and commonly used in loops, counting operations, and pointer arithmetic, making them essential for low-level programming and optimization.

    These simple operations form the building blocks of more complex algorithms and are particularly important in assembly and systems programming where performance and direct hardware control are crucial.

    ← Previous topic 40
    Adding and Subtracting Integer
    Next topic 42 →
    NEG Instruction

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    7mremaining
    1.1kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Computer Organization and Assembly Language