ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Computer Organization and Assembly Language
    COMP3137
    Progress0 / 73 topics
    Topics
    1. Introduction to Computer Organization2. Assembly Language3. Comparison of Low-Level and High-Level Languages4. Register Types (16-bit): General Purpose and Special Purpose Registers5. Introduction and Usage of RAM6. Processor7. Registers8. System Bus9. Instruction Execution Cycle10. Assembly and Machine Language11. Assembler12. Linker and Link Libraries13. Programmer's View of a Computer System14. RISC and CISC Architecture15. Physical Address Calculation16. Basic Memory Organization17. CPU Organization18. Top Level View of Computer Function and Interconnection19. Assembler Instruction Cycle20. Execute Cycle21. Interrupts22. Interrupt Cycle23. Memory Connection24. Input/Output Connection25. CPU Connection26. MASM27. MIPS28. Defining Data in MASM Assembler29. Elements of Assembly Language30. Integer Constants31. Integer Expressions32. Real Number Constants33. Character Constants34. String Constants35. Reserved Words36. Identifiers37. Directives38. Instructions39. The NOP (No Operation) Instruction40. Adding and Subtracting Integer41. INC and DEC Instructions42. NEG Instruction43. How to Move Integer Number in Register44. Adding and Subtracting Numbers in Registers45. Declaration and Initialization of Variables46. Moving Data from Variable to Register47. Data Definition Statement48. BYTE and SBYTE Data49. WORD and SWORD Data50. Defining DWORD and SDWORD Data51. Knowledge about Different Data Types52. Operations, Array & Loops53. Division and Multiplication in Assembly54. Jumps Based on Specific Flags55. Jumps Based on Equality56. Simple Jump Statements57. Jumps Based on Specific Condition58. Code Examples59. Practice on MASM60. Procedures61. File Operations Procedures62. Labels in Procedures63. Stack64. Runtime Stack65. Conditional Control Flow Directives66. Compound Expressions67. Data Representation & Conversion68. Architecture69. Data Path70. Control Unit71. Critical Path72. General Principles of Pipelining73. Pipelined Y86 Implementations
    COMP3137›INC and DEC Instructions
    Computer Organization and Assembly LanguageTopic 41 of 73

    INC and DEC Instructions

    7 minread
    1,134words
    Intermediatelevel

    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 load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time7 min
      Word count1,134
      Code examples0
      DifficultyIntermediate