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 PortalCOMP3137Neg Instruction
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
    6 min read
    Topic 42 of 73
    Topic Study Note Illustration
    42

    NEG Instruction

    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...

    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 view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    6mremaining
    981words
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Computer Organization and Assembly Language