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 PortalCOMP3137Moving Data Variable R…
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 46 of 73
    Topic Study Note Illustration
    46

    Moving Data from Variable to Register

    In assembly language, one of the most common operations is moving data from a variable (which resides in memory) to a register (which is part of the...

    Moving Data from Variable to Register in Assembly Language

    In assembly language, one of the most common operations is moving data from a variable (which resides in memory) to a register (which is part of the CPU's fast-access storage). The MOV instruction is used to copy data between registers, memory, and constants.

    Moving data from a variable to a register typically involves accessing a specific memory location (where the variable is stored) and loading its value into a register for processing.


    MOV Instruction Overview

    The MOV instruction in assembly language copies data from one operand to another. The operands can be a register, a memory location, or an immediate value (constant).

    Basic Syntax:

    MOV destination, source
    
    • destination: Where the data is moved (typically a register or memory location).
    • source: The value being moved (can be a register, memory location, or constant).

    Types of Data Transfer in MOV

    1. Memory to Register: Moving data from a memory location (variable) to a register.
    2. Register to Register: Copying data between registers.
    3. Immediate to Register: Moving a constant (immediate value) to a register.

    We will focus here on moving data from a memory location (variable) to a register.


    Moving Data from Memory to Register

    When you want to move data from a variable (which is stored in memory) into a register, the syntax would be:

    MOV register, [variable]
    
    • register: The CPU register where the data will be stored.
    • [variable]: The memory address of the variable (enclosed in brackets to indicate memory access).

    Example 1: Moving a Byte Variable to a Register

    In this example, let's assume you have a variable var1 that is a byte-sized variable, and you want to move its value into the AL register.

    section .data
        var1 db 100  ; Declare a byte variable and initialize it to 100
    
    section .text
        MOV AL, [var1]  ; Move the value of var1 (100) into the AL register
    
    • db 100 declares a byte-sized variable var1 with a value of 100.
    • MOV AL, [var1] loads the value of var1 (which is 100) into the AL register.

    After this instruction executes, the AL register will contain the value 100.

    Example 2: Moving a Word Variable to a Register

    If the variable is a word (2 bytes), you can use a 16-bit register such as AX to store the value. Here’s how to move a word-sized variable to a register:

    section .data
        var2 dw 500  ; Declare a word-sized variable and initialize it to 500
    
    section .text
        MOV AX, [var2]  ; Move the value of var2 (500) into the AX register
    
    • dw 500 declares a word-sized variable var2 with a value of 500.
    • MOV AX, [var2] loads the value of var2 (which is 500) into the AX register.

    Now, the AX register will contain the value 500.

    Example 3: Moving a Double Word Variable to a Register

    For a double word (4 bytes), you can use a 32-bit register like EAX:

    section .data
        var3 dd 1000  ; Declare a double word-sized variable and initialize it to 1000
    
    section .text
        MOV EAX, [var3]  ; Move the value of var3 (1000) into the EAX register
    
    • dd 1000 declares a double word-sized variable var3 with the value 1000.
    • MOV EAX, [var3] moves the value of var3 (which is 1000) into the EAX register.

    Now, the EAX register will contain the value 1000.

    Example 4: Moving a String or Array to a Register

    If you want to move an entire string or array from memory to a register, you typically do it one byte (or one word) at a time, because registers are small, and a string may not fit into a register at once.

    Example: Moving a Byte of a String to a Register

    Let’s say you have a string "Hello" stored in memory, and you want to move the first character of the string ('H', ASCII code 72) into the AL register:

    section .data
        message db 'Hello', 0  ; Null-terminated string
    
    section .text
        MOV AL, [message]  ; Move the first byte (ASCII value of 'H') into AL
    
    • The string "Hello" is stored in the .data section.
    • MOV AL, [message] moves the first byte of the string ('H', ASCII value 72) into the AL register.

    After this, the AL register will hold the value 72 (which is the ASCII code for 'H').

    Memory Addressing Modes

    In assembly, there are different types of memory addressing modes to access variables:

    1. Direct Memory Addressing: This is what we've seen in the examples above, where the variable is directly referenced by its name enclosed in brackets. Example: [var1].

    2. Indirect Addressing: You can also use a pointer or index register to indirectly address a memory location. For example, the value in a register can hold the memory address of a variable, and you can access the variable via the register.

    MOV BX, offset var1   ; BX = address of var1
    MOV AL, [BX]          ; Move the byte at the address in BX into AL
    
    1. Indexed Addressing: When working with arrays, you can use an index register along with an offset to access specific elements in the array.
    MOV SI, 2           ; Set index to 2 (3rd element, because SI is zero-based)
    MOV AL, [array + SI] ; Move the 3rd byte of the array into AL
    

    Example 5: Moving Data from a Variable to a Register Using Indirect Addressing

    section .data
        var1 db 100  ; A byte-sized variable
        var2 db 200  ; Another byte-sized variable
        pointer dw var1 ; A pointer to var1 (holds the address of var1)
    
    section .text
        MOV SI, [pointer]  ; SI now holds the address of var1
        MOV AL, [SI]       ; Move the value at address in SI (100 from var1) into AL
    
    • pointer dw var1 stores the address of var1 in the pointer variable.
    • MOV SI, [pointer] loads the address of var1 into the SI register.
    • MOV AL, [SI] then loads the value at the address in SI (which is the value stored in var1) into the AL register.

    Key Points to Remember

    • The MOV instruction moves data from a source operand to a destination operand.
    • Memory to Register: To move data from a memory location (variable) to a register, use MOV register, [memory].
    • When moving data from memory, the brackets ([ ]) indicate memory access.
    • Data can be moved from a byte, word, or double word variable to the appropriate register size.
    • Registers like AX (16-bit), EAX (32-bit), and RAX (64-bit) are used to store the values after moving them from memory.

    This process of moving data is essential for almost every assembly language operation, as registers are used to perform calculations, control program flow, and manipulate data efficiently.

    ← Previous topic 45
    Declaration and Initialization of Variables
    Next topic 47 →
    Data Definition Statement

    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