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›Moving Data from Variable to Register
    Computer Organization and Assembly LanguageTopic 46 of 73

    Moving Data from Variable to Register

    7 minread
    1,134words
    Intermediatelevel

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

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