Thursday, January 26, 2012

Moving Data between Registers & Memory

My Guru for this is Vivek Ramachand.

Move command

movl = moves a 32 bit value
movl %eax, %ebx

movw = moves a 16 bit value
movw %ax, %bx

movb = moves a 8 bit value
movb %ah, %bh

Moving Data  

1. Between Registers
 movl %eax, %ebx

2. Between Registers and Memory
location:
                .int 20
movl %eax, location
movl location, %ebx

3. Immediate value into Register
 movl $10, %ebx

4. Immediate value into memory location
location:
                 .byte 0
movb $20, location

5. Moving Data into an Indexed Memory Location
IntegerArray:
                        .int 11,12,13,14,15

Let us select the 4th integer "14" and convert it into "20"
BaseAddress(Offset, Index, Size) = IntegerArray(0,3,4)
movl %eax, IntegerArray(0,3,4)

6. Indirect Addressing using Registers
To extract the memory location of a variable we need to put a $ sign before the variable, it is similar to using &variable in C programming.

movl $location, %edi
#The next command will copy value 9 into the address pointed by edi.
movl $9,(%edi)
#The next cmd will cpy value 9 into the address pointed by edi + 4 bytes.
movl $9,4(%edi)
#The next cmd will cpy value 9 into the address pointed by edi - 2 bytes.
movl $9,-2(%edi)

Now let us move to the code, which will demonstrate all the six shown above.

Program Starts here

#This program shows how to use Data types.
.data
    HelloEarth:
        .ascii "HelloEarth"
    ByteLocation:
        .byte 50
    Int32:
        .int 5
    Int16:
        .short 5
    Float:
        .float 50.55
    IntegerArray:
        .int 50,55,60,65,70
.bss
    .comm LargeBuff, 1000
.text
   
    .globl _start
    _start:
        nop
        #1. MOV immediate value into register
        movl $7, %eax
        #2. MOV immediate value into memory location
        movw $70, Int16
        #3. MOV data between registers
        movl %eax, %ebx
        #4. MOV data from memory to register
        movl Int32, %eax
        #5. MOV data from register to memory
        movb $7, %al
        movb %al, ByteLocation
        #6. MOV data into an indexed memory location
        # Location is decided by BaseAddress(Offset, Index, DataSize)
        # Offset and Index must be registers, Datasize can be a numerical value
        movl $0, %ecx
        movl $2, %edi
        movl $77, IntegerArray(%ecx, %edi, 4)

        #Exit Systemcall to exit the program
        movl $1, %eax
        movl $0, %ebx
        int $0x80

Now I am going to compile the code and link it to create the executable.

bala@lappi:~/ASM$ as -gstabs -o move.o move.s
bala@lappi:~/ASM$ ld -o move move.o
bala@lappi:~/ASM$ gdb ./move

Create a break point 
(gdb) break *_start+1
Breakpoint 2 at 0x4000b1: file move.s, line 23.
(gdb) run
Starting program: /home/bala/ASM/move
Breakpoint 2, _start () at move.s:23
23            movl $7, %eax

#1. MOV immediate value into register
(gdb) info registers
rax            0x0    0
rbx            0x0    0
(gdb) s
(gdb) info registers
rax            0x7    7
rbx            0x0    0
(gdb) s
25            movw $70, Int16
#2. MOV immediate value into memory location

(gdb) x/1dh &Int16
0x600103 :    5
(gdb) s
27            movl %eax, %ebx
#3. MOV data between registers
(gdb) x/1dh &Int16
0x600103 :    70
(gdb) info registers
rax            0x7    7
rbx            0x0    0
rcx            0x0    0

(gdb) s
29            movl Int32, %eax
#4. MOV data from memory to register

(gdb) info registers
rax            0x7    7
rbx            0x7    7
rcx            0x0    0

(gdb) x/1dw &Int32
0x6000ff :    5
(gdb) s
31            movb $7, %al
#5. MOV data from register to memory

(gdb) info registers
rax            0x5    5
rbx            0x7    7
rcx            0x0    0

(gdb) s
32            movb %al, ByteLocation
(gdb) x/1db &ByteLocation
0x6000fe :    50

(gdb) s
36            movl $0, %ecx
#6. MOV data into an indexed memory location

(gdb) x/1db &ByteLocation
0x6000fe :    7
(gdb)  s
37            movl $2, %edi
(gdb) s
38            movl $77, IntegerArray(%ecx, %edi, 4)
(gdb) x/5dw &IntegerArray
0x600109 :    50    55    60    65
0x600119 :    70

(gdb) s
40            movl $Int32, %eax
#7. MOV data in via indirect means ie Memory location
(gdb) x/5dw &IntegerArray
0x600109 :    50    55    77    65
0x600119 :    70

(gdb) print &Int32
$1 = ( *) 0x60010f
(gdb) s
41            movl (%eax), %ebx
(gdb) info registers
rax            0x60010f    6291727
rbx            0x7    7
rcx            0x0    0

(gdb) s
42            movl $777, (%eax)
(gdb) info registers
rax            0x60010f    6291727
rbx            0x5    5
rcx            0x0    0

(gdb) x/1dw &Int32
0x60010f :    5
(gdb) s
44            movl $1, %eax
(gdb) x/1dw &Int32
0x60010f :    777
(gdb)

No comments:

Post a Comment