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›How to Move Integer Number in Register
    Computer Organization and Assembly LanguageTopic 43 of 73

    How to Move Integer Number in Register

    7 minread
    1,111words
    Intermediatelevel

    Moving an Integer into a Register in Assembly Language

    In assembly language, moving (or copying) data into a register is typically done using the MOV instruction. The MOV instruction is one of the most fundamental and frequently used instructions in assembly language programming. It is used to transfer data from one location to another, including between registers, memory, and immediate values (constants).

    MOV Instruction Syntax

    The MOV instruction has the following general syntax:

    MOV destination, source
    
    • destination: The register or memory location where the data will be stored (the value that will be moved).
    • source: The data that will be moved to the destination. This can be a register, a memory location, or an immediate value (constant).

    Types of Operands for the MOV Instruction

    1. Register to Register: Moving data between two registers.
    2. Immediate Value to Register: Moving an immediate value (constant) into a register.
    3. Memory to Register: Moving data from a memory location into a register.
    4. Register to Memory: Moving data from a register to a memory location.

    1. Moving an Integer into a Register from an Immediate Value

    An immediate value is a constant (literal) number specified directly in the instruction.

    Syntax:

    MOV register, immediate_value
    

    Example 1: Moving an Immediate Integer into a Register

    MOV AX, 100    ; Move the integer 100 into register AX
    
    • In this example, the value 100 (an immediate integer) is moved into the AX register.

    Example 2: Moving a Negative Integer into a Register

    MOV AX, -50    ; Move the integer -50 into register AX
    
    • Here, -50 (a negative integer) is moved into the AX register.

    Example 3: Using a Hexadecimal Immediate Value

    MOV AX, 0x1A   ; Move the hexadecimal value 0x1A (26 in decimal) into AX
    
    • The value 0x1A is a hexadecimal literal representing the decimal value 26, and it's moved into AX.

    2. Moving an Integer from One Register to Another

    You can move the contents of one register into another register.

    Syntax:

    MOV destination_register, source_register
    

    Example 1: Moving Data Between Registers

    MOV BX, AX     ; Move the value in AX into BX
    
    • In this case, the value stored in AX is copied into the BX register. After this instruction, both AX and BX will have the same value.

    Example 2: Using 32-bit Registers (e.g., EAX, EBX in x86)

    MOV EBX, EAX   ; Move the value in 32-bit register EAX into 32-bit register EBX
    
    • Here, EAX (a 32-bit register) contains an integer, and it is moved into EBX (another 32-bit register).

    3. Moving an Integer from Memory into a Register

    You can load an integer value from a memory location into a register.

    Syntax:

    MOV register, [memory_location]
    
    • memory_location can be a label that represents a memory address or a variable that holds a value.

    Example 1: Moving Data from Memory to Register

    MOV AX, [num]   ; Load the integer at memory location 'num' into register AX
    
    • Here, the value stored at the memory location labeled num is loaded into the AX register.

    Example 2: Using 32-bit Memory Location

    MOV EAX, [myVar] ; Load the value at the memory address 'myVar' into 32-bit register EAX
    
    • This example assumes myVar is a memory location containing a 32-bit integer. The value of myVar is loaded into EAX.

    4. Moving Data from a Register to Memory

    You can also store a value from a register into memory.

    Syntax:

    MOV [memory_location], register
    

    Example 1: Moving Data from Register to Memory

    MOV [num], AX   ; Store the value of AX into memory location 'num'
    
    • The value in AX is stored at the memory location labeled num.

    Example 2: Storing 32-bit Data into Memory

    MOV [myVar], EAX ; Store the value in 32-bit register EAX into memory location 'myVar'
    
    • The value in EAX is stored in the memory location myVar.

    5. Example Program: Moving Integer Values into Registers

    Below is an example program demonstrating various ways of moving integer values into registers.

    section .data
        num1 dw 50         ; Define a word (16-bit integer) with value 50
        num2 dw -30        ; Define a word with value -30
    
    section .text
        global _start
    
    _start:
        ; Moving immediate values into registers
        MOV AX, 100        ; AX = 100 (move immediate value)
        MOV BX, -50        ; BX = -50 (move negative integer)
    
        ; Moving values from memory to registers
        MOV AX, [num1]     ; AX = 50 (load value of num1 into AX)
        MOV BX, [num2]     ; BX = -30 (load value of num2 into BX)
    
        ; Moving values between registers
        MOV CX, AX         ; CX = 50 (move value of AX into CX)
    
        ; Store the value of AX back to memory location num1
        MOV [num1], AX     ; Store the value in AX (50) into memory location 'num1'
    
        ; Exit (Linux system call to exit the program)
        MOV AX, 1          ; Syscall number for exit
        INT 0x80           ; Call kernel
    

    Explanation of the Example Program:

    1. Immediate Value Movement:

      • The program first moves the immediate integer values 100 and -50 into registers AX and BX, respectively.
    2. Memory to Register Movement:

      • The program then moves the integer values from memory locations num1 (which contains 50) and num2 (which contains -30) into registers AX and BX.
    3. Register to Register Movement:

      • The value in register AX is copied into CX.
    4. Register to Memory Movement:

      • Finally, the value in AX (which is 50) is stored back into the memory location num1.

    6. Considerations When Moving Integers

    • Size of Registers: Ensure that the size of the register matches the size of the data being moved. For example, if you want to move a 16-bit integer, use a 16-bit register like AX or BX. For 32-bit values, use EAX or EBX. For 64-bit values (in 64-bit systems), use RAX, RBX, etc.

    • Memory Alignment: In certain architectures, memory must be aligned for efficient access. For example, on x86 systems, 16-bit values should be aligned to even memory addresses, and 32-bit values should be aligned to addresses that are divisible by 4.

    • Immediate Values: When moving immediate values, remember that the number must be within the range that can be represented by the size of the operand. For example, in an 8-bit register, the immediate value should be in the range of -128 to 127 (for signed values).

    Conclusion

    The MOV instruction is crucial for moving integer values into registers in assembly language. It can handle various data types and sources, including immediate values, memory locations, and other registers. By using MOV, you can efficiently transfer data and manipulate it within your program, which is essential for low-level programming, system programming, and performance-critical applications.

    Previous topic 42
    NEG Instruction
    Next topic 44
    Adding and Subtracting Numbers in Registers

    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,111
      Code examples0
      DifficultyIntermediate