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 PortalCOMP3137Adding Subtracting Num…
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 44 of 73
    Topic Study Note Illustration
    44

    Adding and Subtracting Numbers in Registers

    In assembly language, performing addition and subtraction is done using the ADD and SUB instructions, respectively. These instructions are used to modify the contents of registers (or...

    Adding and Subtracting Numbers in Registers (Assembly Language)

    In assembly language, performing addition and subtraction is done using the ADD and SUB instructions, respectively. These instructions are used to modify the contents of registers (or memory) by adding or subtracting values.

    1. ADD Instruction (Addition)

    The ADD instruction adds the value of the source operand to the destination operand and stores the result in the destination operand. It is commonly used for performing arithmetic operations, such as adding integers.

    Syntax:

    ADD destination, source
    
    • destination: The operand (register, memory location, or another operand) where the result will be stored.
    • source: The operand (register, immediate value, or memory location) whose value will be added to the destination.

    Effect on Flags:

    The ADD instruction affects several flags in the processor's Flags Register:

    • Carry Flag (CF): Set if the addition results in a carry out of the most significant bit.
    • Zero Flag (ZF): Set if the result is zero.
    • Sign Flag (SF): Set if the result is negative (for signed integers).
    • Overflow Flag (OF): Set if the addition results in an overflow (i.e., the result cannot be represented in the available number of bits).
    • Parity Flag (PF): Set if the number of 1-bits in the result is even.

    Example of ADD Instruction

    Example 1: Adding Two Registers

    MOV AX, 5      ; AX = 5
    MOV BX, 10     ; BX = 10
    ADD AX, BX     ; AX = AX + BX = 5 + 10 = 15
    

    In this example:

    • The value 5 is loaded into the AX register.
    • The value 10 is loaded into the BX register.
    • The ADD AX, BX instruction adds the value in BX (10) to AX (5), and the result (15) is stored back in AX.

    Example 2: Adding an Immediate Value to a Register

    MOV AX, 20     ; AX = 20
    ADD AX, 30     ; AX = 20 + 30 = 50
    

    Here, the immediate value 30 is added to the value in AX (20), and the result 50 is stored back in AX.

    Example 3: Adding a Register to a Memory Location

    MOV AX, [num]   ; Load value from memory location 'num' into AX
    ADD AX, BX      ; AX = AX + BX (Adds value of BX to AX)
    

    In this case:

    • The value at the memory location num is loaded into the AX register.
    • The value in BX is then added to AX.

    2. SUB Instruction (Subtraction)

    The SUB instruction subtracts the source operand from the destination operand and stores the result in the destination operand. This is used for subtracting one number from another.

    Syntax:

    SUB destination, source
    
    • destination: The operand (register, memory location, or another operand) where the result will be stored.
    • source: The operand (register, immediate value, or memory location) that will be subtracted from the destination.

    Effect on Flags:

    The SUB instruction also affects several flags in the Flags Register:

    • Carry Flag (CF): Set if a borrow occurs during the subtraction (i.e., if the result is negative in an unsigned subtraction).
    • Zero Flag (ZF): Set if the result is zero.
    • Sign Flag (SF): Set if the result is negative (for signed integers).
    • Overflow Flag (OF): Set if the subtraction results in an overflow (i.e., the result cannot be represented in the available number of bits).
    • Parity Flag (PF): Set if the number of 1-bits in the result is even.

    Example of SUB Instruction

    Example 1: Subtracting Two Registers

    MOV AX, 15     ; AX = 15
    MOV BX, 10     ; BX = 10
    SUB AX, BX     ; AX = AX - BX = 15 - 10 = 5
    

    Here:

    • AX contains 15, and BX contains 10.
    • The SUB AX, BX instruction subtracts the value in BX (10) from AX (15), and the result (5) is stored in AX.

    Example 2: Subtracting an Immediate Value from a Register

    MOV AX, 50     ; AX = 50
    SUB AX, 20     ; AX = 50 - 20 = 30
    

    In this example:

    • The immediate value 20 is subtracted from the value in AX (50), and the result (30) is stored back in AX.

    Example 3: Subtracting a Register from a Memory Location

    MOV AX, [num]  ; Load value from memory location 'num' into AX
    SUB AX, BX     ; AX = AX - BX (Subtracts value of BX from AX)
    

    In this case:

    • The value stored at the memory location num is loaded into AX.
    • The value in BX is then subtracted from AX.

    3. Handling Signed and Unsigned Numbers

    • Signed Addition/Subtraction: Both ADD and SUB can handle signed integers (positive and negative numbers). The result will be stored according to the signed interpretation of the operand.

      • Overflow Flag (OF) will be set if the signed result cannot be represented in the given number of bits (e.g., adding two large positive numbers and overflowing to a negative result).
      • For signed numbers, Carry Flag (CF) is typically not affected during signed operations. However, it can be relevant when dealing with unsigned numbers.
    • Unsigned Addition/Subtraction: For unsigned integers, the Carry Flag (CF) will indicate whether a carry or borrow occurred.

      • In unsigned arithmetic, CF will be set if there is a carry during addition (i.e., the result exceeds the maximum representable value), or a borrow during subtraction (i.e., the result is negative).

    4. Handling Larger Numbers (Multi-Byte Registers)

    When working with larger numbers (e.g., 32-bit or 64-bit), ADD and SUB can also operate on 32-bit or 64-bit registers. For example, in 32-bit registers like EAX, EBX, or ECX (in x86 assembly) or RAX, RBX, etc. (in x64), the same syntax applies, but the operands are typically 32-bit or 64-bit values.

    Example of Adding 32-bit Registers:

    MOV EAX, 5000000  ; EAX = 5000000
    MOV EBX, 1000000  ; EBX = 1000000
    ADD EAX, EBX      ; EAX = EAX + EBX = 5000000 + 1000000 = 6000000
    

    Example of Subtracting 64-bit Registers:

    MOV RAX, 10000000000  ; RAX = 10000000000
    MOV RBX, 5000000000   ; RBX = 5000000000
    SUB RAX, RBX          ; RAX = RAX - RBX = 10000000000 - 5000000000 = 5000000000
    

    5. Conclusion

    • ADD and SUB are essential arithmetic instructions in assembly language for adding and subtracting numbers.
    • The ADD instruction adds the value of the source operand to the destination operand.
    • The SUB instruction subtracts the source operand from the destination operand.
    • Both instructions affect the processor’s flags, such as the Carry Flag (CF), Zero Flag (ZF), Sign Flag (SF), and Overflow Flag (OF).
    • These operations are fundamental for performing arithmetic operations, handling counters, manipulating data, and more in low-level programming.

    By mastering ADD and SUB, you gain the ability to manipulate and process numerical data in an efficient, low-level manner.

    ← Previous topic 43
    How to Move Integer Number in Register
    Next topic 45 →
    Declaration and Initialization of Variables

    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