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›Adding and Subtracting Numbers in Registers
    Computer Organization and Assembly LanguageTopic 44 of 73

    Adding and Subtracting Numbers in Registers

    7 minread
    1,148words
    Intermediatelevel

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

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