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 Integer
    Computer Organization and Assembly LanguageTopic 40 of 73

    Adding and Subtracting Integer

    8 minread
    1,345words
    Intermediatelevel

    Adding and Subtracting Integers in Assembly Language

    Adding and subtracting integers are fundamental operations in almost all computer programs, and assembly language provides instructions to perform these tasks directly at the machine level. These operations are typically done using registers or memory addresses as operands. Depending on the architecture (e.g., x86, ARM, MIPS), the specific instructions and the syntax may vary slightly, but the basic concepts remain the same.

    Let's explore how addition and subtraction are handled in assembly language, specifically using x86 (Intel syntax) as an example.

    1. Addition of Integers in Assembly

    Addition is one of the simplest arithmetic operations. In assembly language, the ADD instruction is used to add two operands (which can be immediate values, registers, or memory locations).

    ADD Instruction

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

    The ADD instruction adds the value in the source operand to the destination operand and stores the result in destination.

    Example 1: Adding Two Registers

    MOV AX, 5       ; Load the value 5 into register AX
    MOV BX, 3       ; Load the value 3 into register BX
    ADD AX, BX      ; Add the value in BX to AX (AX = AX + BX)
    ; After this, AX = 8
    

    In this example:

    • The value 5 is loaded into register AX.
    • The value 3 is loaded into register BX.
    • The ADD instruction adds the value in BX (which is 3) to the value in AX (which is 5), and the result (8) is stored in AX.

    Example 2: Adding an Immediate Value to a Register

    MOV AX, 10      ; Load the value 10 into register AX
    ADD AX, 5       ; Add the immediate value 5 to AX (AX = AX + 5)
    ; After this, AX = 15
    

    Here, the immediate value 5 is added directly to AX, so the result stored in AX is 15.

    Example 3: Adding a Value from Memory

    MOV AX, 10      ; Load the value 10 into AX
    MOV BX, [num]   ; Load the value from memory location 'num' into BX
    ADD AX, BX      ; Add the value in BX to AX (AX = AX + [num])
    

    In this case, the value stored at the memory address labeled num is loaded into the BX register, and then added to AX.

    2. Subtraction of Integers in Assembly

    Subtraction is also a basic operation, and it is performed using the SUB instruction in assembly language.

    SUB Instruction

    • Syntax:
      SUB destination, source
      
      • destination: The register or memory location where the result will be stored.
      • source: The value (register, immediate, or memory) to subtract from the destination.

    The SUB instruction subtracts the source operand from the destination operand and stores the result in destination.

    Example 1: Subtracting Two Registers

    MOV AX, 10      ; Load the value 10 into AX
    MOV BX, 3       ; Load the value 3 into BX
    SUB AX, BX      ; Subtract the value in BX from AX (AX = AX - BX)
    ; After this, AX = 7
    

    In this example:

    • The value 10 is loaded into AX.
    • The value 3 is loaded into BX.
    • The SUB instruction subtracts the value in BX (which is 3) from the value in AX (which is 10), and the result (7) is stored in AX.

    Example 2: Subtracting an Immediate Value from a Register

    MOV AX, 20      ; Load the value 20 into AX
    SUB AX, 5       ; Subtract the immediate value 5 from AX (AX = AX - 5)
    ; After this, AX = 15
    

    Here, the immediate value 5 is subtracted directly from AX, so the result stored in AX is 15.

    Example 3: Subtracting a Value from Memory

    MOV AX, 20      ; Load the value 20 into AX
    MOV BX, [num]   ; Load the value from memory location 'num' into BX
    SUB AX, BX      ; Subtract the value in BX from AX (AX = AX - [num])
    

    Here, the value stored at the memory address labeled num is loaded into the BX register, and then subtracted from AX.

    3. Carry and Borrow Flags in Arithmetic Operations

    When performing addition and subtraction, the processor’s flags (specifically the carry flag and borrow flag) are updated. These flags can be used for further decision-making or checking for overflow/underflow.

    • Carry Flag (CF): This flag is set when there is a carry out of the most significant bit during addition, or a borrow in subtraction.

      • For addition, the CF is set if the sum of two numbers exceeds the size of the register (e.g., adding two 8-bit numbers that produce a result larger than 255 in an 8-bit register).
      • For subtraction, the CF is cleared (set to 0) if there is a borrow.
    • Overflow Flag (OF): The OF is set when the result of an operation overflows the range of the signed numbers that can be represented in the register. It is used primarily for signed integer operations.

    Example: Carry Flag in Addition

    MOV AX, 0xFFFF   ; AX = 65535 (maximum value for 16-bit register)
    ADD AX, 1        ; AX = 65536, which overflows (carry flag will be set)
    

    In this example, adding 1 to AX (which already holds the largest 16-bit value 0xFFFF) causes an overflow, and the carry flag will be set.

    Example: Borrow Flag in Subtraction

    MOV AX, 5       ; AX = 5
    MOV BX, 10      ; BX = 10
    SUB AX, BX      ; AX = -5 (borrow flag will be set)
    

    In this example, subtracting 10 from 5 results in a negative value, and the borrow flag will be set to indicate that the subtraction couldn't be performed without a borrow.

    4. Signed and Unsigned Arithmetic

    • Signed Integers: These are integers that can be positive or negative. When performing operations like addition and subtraction on signed integers, the overflow flag (OF) and the sign flag (SF) will be useful to detect overflows or negative results.
    • Unsigned Integers: These are integers that are always positive. The carry flag is typically used in addition or subtraction for detecting overflows or borrows.

    In assembly, you may need to be mindful of whether the integers are signed or unsigned because the handling of overflow and carry differs.

    5. Example Program: Adding and Subtracting Integers

    Here is a simple example program that demonstrates both addition and subtraction of integers in x86 assembly:

    section .data
        num1 db 10    ; First number
        num2 db 5     ; Second number
    
    section .text
        global _start
    
    _start:
        ; Load values into registers
        MOV AL, [num1]   ; AL = 10 (load num1 into AL)
        MOV BL, [num2]   ; BL = 5  (load num2 into BL)
    
        ; Add num1 and num2
        ADD AL, BL       ; AL = AL + BL = 10 + 5 = 15
    
        ; Now AL = 15, do something with the result (like storing it or printing)
        
        ; Subtract num2 from num1
        MOV AL, [num1]   ; Reload AL with num1 (10)
        SUB AL, [num2]   ; AL = AL - num2 = 10 - 5 = 5
    
        ; End program (this is platform-specific, e.g., for Linux or Windows)
        MOV AX, 1        ; Exit system call number
        INT 0x80         ; Make the system call to exit
    

    In this example:

    • First, the program loads two numbers (num1 and num2) into the registers AL and BL.
    • Then, it performs addition (ADD AL, BL) and stores the result in AL.
    • After that, it reloads the value of num1 into AL and performs subtraction (SUB AL, [num2]).

    Conclusion

    Adding and subtracting integers in assembly language is done using simple instructions like ADD and SUB. These instructions work on registers, memory locations, or immediate values. Flags like the carry flag and overflow flag are updated based on the results of the operations, and they can be used to handle special cases like overflow or underflow. Understanding how to use these instructions

    Previous topic 39
    The NOP (No Operation) Instruction
    Next topic 41
    INC and DEC Instructions

    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 time8 min
      Word count1,345
      Code examples0
      DifficultyIntermediate