Thursday, January 26, 2012

Assembler Working with Strings

Singing with Strings in Assembly

My Guru for this is Vivek Ramachand

About MOVSx
We can use MOVSx to move Strings from one memory location to the other

MOVSB - To move a byte (8 bits)
MOVSW - To move a word (16 bits)
MOVSL - To move double word (32 bits)

The implied thing is Source is always where ESI points to in memory and Destination is always where EDI points in memory.

The Direction Flag (DF Flag)
The DF flag is part of the EFLAGS registers
This flag decides to i++ or i-- ESI, EDI after a MOVSx instruction
If DF is set ie 1, ESI and EDI are i--
If DF is clear ie 0, ESI & EDI are i++
We can set DF using the STD instruction
We can clear DF with the CLD instruction

The REP instruction

It is used to repeat a string instruction till ECX has a value > 0

    * Load ECX with str length
    * Use REP MOVSx to copy string from src to dst

Load Str from Memory into Registers

Loads into the EAX register, source is pointed by ESI

LODSB - Load a byte from memory location into AL
LODSW - Load a word from memory into AX
LODSL - Load a double word from memory into EAX

ESI is automatically i++ or i-- based on DF after LODSx instruction executes

Storing Str from Registers into Memory

Loads into Memory, source pointed by the EAX register

STOSB - store AL to memory
STOSW - store AX to memory
STOSL - store EAX to memory

EDI is i++ ori-- based on DF after STOSx instruction executes

Comparing Strings

Comparing Strings, ESI contains src string & EDI contains dst string

CMPSB - compares byte value
CMPSW - compares word value
CMPSL - compares double word value

EDI is i++ ori-- based on DF after STOSx instruction executes

REPZ - repeat instruction if zero flag is set
REPNZ - repeat instruction if zero flag is not set

Program Starts here

        .ascii "Hello World of Assembly!"
        .asciz "H3ll0"
    .lcomm Destination, 100
    .lcomm DestinationUsingRep, 100
    .lcomm DestinationUsingStos, 100
    .globl _start
        #1. Simple copying using movsb, movsw, movsl
        movl $HelloEarthStr, %esi
        movl $Destination, %edi
        #2. Setting / Clearing the DF flag
        std # set the DF flag
        cld # clear the DF flag
        #3. Using Rep
        movl $HelloEarthStr, %esi
        movl $DestinationUsingRep, %edi
        movl $25, %ecx # Set the string length in ECX
        cld # clear the DF
        rep movsb
        #4. Loading strings from memory into EAX register
        # leal - Load Effective Address into Double Work L
        leal HelloEarthstr, %esi
        movb $0, %al
        dec %esi
        movw $0, %ax
        subl $2, %esi # Make ESI point back to the original string
        #5. Storing strings from EAX to memory
        leal DestinationUsingStos, %edi
        #6. Comparing Strings
        leal HelloWorldString, %esi
        leal H3ll0, %edi
        dec %esi
        dec %edi
        subl $2, %esi
        subl $2, %edi

        # The exit() routine called
        movl $1, %eax
        movl $10, %ebx
        int $0x80

1 comment:

  1. This blog is really informative i really had fun reading it.