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›NEG Instruction
    Computer Organization and Assembly LanguageTopic 42 of 73

    NEG Instruction

    6 minread
    981words
    Intermediatelevel

    NEG Instruction in Assembly Language

    The NEG (Negate) instruction is used in assembly language to change the sign of an operand. In other words, it negates (or inverts) the value of the operand — it changes a positive value to negative and a negative value to positive. This is equivalent to multiplying the operand by -1.

    1. NEG Instruction Syntax

    The NEG instruction operates on a single operand and negates its value.

    Syntax:

    NEG operand
    
    • operand: The operand can be a register, memory location, or immediate value (typically a register or memory location). The instruction changes the sign of the operand by negating it.

    2. How the NEG Instruction Works

    • Effect on Registers: When NEG is applied to a register, the value in the register is negated (multiplied by -1).
    • Effect on Memory: When applied to a memory location, the value at the memory location is negated.
    • Effect on Immediate Values: The NEG instruction generally doesn't work directly on immediate values because immediate values are constant values encoded in the instruction. However, you can load an immediate value into a register or memory first and then negate it.

    Internally, NEG works by performing a two's complement operation on the operand:

    • To negate a number, it first inverts all the bits (this is called the ones' complement), then adds 1 to the result. This is known as two's complement negation.

    3. Flags Affected by NEG

    The NEG instruction affects the following flags in the processor's Flags Register:

    • Zero Flag (ZF): Set if the result of the operation is zero. For example, if the operand was 0, negating it will still result in 0, setting the Zero Flag.
    • Sign Flag (SF): Set if the result is negative. If the result is positive, it will be cleared.
    • Overflow Flag (OF): The Overflow Flag is set if there is a signed overflow during negation. A signed overflow occurs when negating a number results in a value that cannot be represented by the operand's register size. For example, in a signed 8-bit system, negating 0x80 (which is -128) would cause an overflow because 128 cannot be represented in 8 bits.
    • Carry Flag (CF): The Carry Flag is not affected by the NEG instruction in most cases, as the operation is not related to a carry in the addition/subtraction sense.

    4. Example of NEG Instruction

    Example 1: Negating a Register Value

    MOV AX, 5     ; AX = 5
    NEG AX        ; AX = -5  (AX is negated)
    

    In this example:

    • The value 5 is loaded into the register AX.
    • The NEG AX instruction negates AX, changing its value to -5.

    Example 2: Negating a Negative Value

    MOV AX, -3    ; AX = -3
    NEG AX        ; AX = 3   (AX is negated)
    

    Here, the value -3 is stored in AX, and after the NEG instruction, the value becomes +3.

    Example 3: Negating a Value from Memory

    MOV AX, [num]   ; Load the value at memory location 'num' into AX
    NEG [num]       ; Negate the value at memory location 'num'
    

    In this case:

    • The value stored at the memory location num is negated, and the result is stored back at the same location.

    5. Two's Complement Explanation

    The NEG instruction essentially performs the two's complement of the operand. Here's how two's complement negation works:

    1. Invert all the bits (this is the ones' complement).
    2. Add 1 to the result of the inversion.

    For example, let's negate the value 5 (which in binary for a 16-bit register is 0000 0000 0000 0101):

    • Step 1: Invert all the bits (ones' complement):
      0000 0000 0000 0101  →  1111 1111 1111 1010
      
    • Step 2: Add 1:
      1111 1111 1111 1010
      
    • 1

      1111 1111 1111 1011

    This binary result (1111 1111 1111 1011) is -5 in two's complement, which is the correct negation of 5.

    6. Example of Overflow with NEG

    If we try to negate the smallest possible signed value for a given register size (e.g., -128 in an 8-bit system), we can experience overflow because the result cannot be represented in the same number of bits.

    Example (Overflow with 8-bit Register):

    MOV AL, 0x80     ; AL = -128 (0x80 in two's complement)
    NEG AL           ; This will cause a signed overflow, as 128 cannot be represented in 8-bit signed integer
    

    Here, 0x80 is the two's complement representation of -128 in an 8-bit signed register. When we negate it, the result would be +128, which is too large to fit in an 8-bit signed integer (which can only hold values from -128 to +127). The Overflow Flag (OF) will be set to indicate the overflow.

    7. Summary of the NEG Instruction

    • NEG negates (multiplies by -1) the value of a register or memory location.
    • It works by performing the two's complement operation: inverting the bits and adding 1.
    • The instruction affects the Zero Flag (ZF), Sign Flag (SF), and Overflow Flag (OF).
      • ZF is set if the result is zero.
      • SF reflects the sign of the result (set if negative).
      • OF is set if there is a signed overflow.
    • NEG is commonly used for negating values in arithmetic operations, but care should be taken with signed overflows, especially with the minimum representable value.

    Practical Uses of NEG

    • Arithmetic Operations: It's useful when you need to reverse the sign of a value in an arithmetic expression.
    • Working with Negative Numbers: In situations where you need to compute the negative of a number directly.
    • Optimizations: It can be used for optimizing code that needs sign reversal rather than explicit multiplication by -1.

    The NEG instruction is a simple but powerful tool for low-level manipulation of numbers, and it's widely used in both system programming and performance-critical applications.

    Previous topic 41
    INC and DEC Instructions
    Next topic 43
    How to Move Integer Number in Register

    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 time6 min
      Word count981
      Code examples0
      DifficultyIntermediate