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 Int…
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
    8 min read
    Topic 40 of 73
    Topic Study Note Illustration
    40

    Adding and Subtracting Integer

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

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

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    8mremaining
    1.3kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Computer Organization and Assembly Language