Showing posts with label Assembly Language - Instruction set. Show all posts
Showing posts with label Assembly Language - Instruction set. Show all posts

Saturday, 24 August 2013

Assembly - The SCAS Instruction

The SCAS instruction is used for searching a particular character or set of characters in a string. The data item to be searched should be in AL (for SCASB), AX (for SCASW) or EAX (for SCASD) registers. The string to be searched should be in memory and pointed by the ES:DI (or EDI) register.
Look at the following program to understand the concept:
section .text
    global _start         ;must be declared for using gcc
_start: ;tell linker entry point

   mov ecx,len
   mov edi,my_string
   mov al , 'e'
   cld
   repne scasb
   je found ; when found
   ; If not not then the following code
   mov eax,4
   mov ebx,1
   mov ecx,msg_notfound
   mov edx,len_notfound
   int 80h
   jmp exit
found:
   mov eax,4
   mov ebx,1
   mov ecx,msg_found
   mov edx,len_found
   int 80h
exit:
   mov eax,1
   mov ebx,0
   int 80h
section .data
my_string db 'hello world', 0
len equ $-my_string  
msg_found db 'found!', 0xa
len_found equ $-msg_found
msg_notfound db 'not found!'
len_notfound equ $-msg_notfound   
When the above code is compiled and executed, it produces following result:
found!

Assembly - The CMPS Instruction

The CMPS instruction compares two strings. This instruction compares two data items of one byte, word or doubleword, pointed to by the DS:SI and ES:DI registers and sets the flags accordingly. You can also use the conditional jump instructions along with this instruction.
The following example demonstrates comparing two strings using the CMPS instruction:
section .text
    global _start            ;must be declared for using gcc
_start: ;tell linker entry point
   mov esi, s1
   mov edi, s2
   mov ecx, lens2
   cld
   repe  cmpsb
   jecxz  equal            ;jump when ecx is zero

   ;If not equal then the following code
   mov eax, 4
   mov ebx, 1
   mov ecx, msg_neq
   mov edx, len_neq
   int 80h
   jmp exit
equal:
   mov eax, 4
   mov ebx, 1
   mov ecx, msg_eq
   mov edx, len_eq
   int 80h
exit:
   mov eax, 1
   mov ebx, 0
   int 80h
section .data
s1 db 'Hello, world!',0    ;our first string
lens1 equ $-s1
s2 db 'Hello, there!', 0   ;our second string
lens2 equ $-s2
msg_eq db 'Strings are equal!', 0xa
len_eq  equ $-msg_eq
msg_neq db 'Strings are not equal!'
len_neq equ $-msg_neq
When the above code is compiled and executed, it produces following result:
Strings are not equal!

Assembly - The STOS Instruction

The STOS instruction copies the data item from AL (for bytes - STOSB), AX (for words - STOSW) or EAX (for doublewords - STOSD) to the destination string, pointed to by ES:DI in memory.
The following example demonstrates use of the LODS and STOS instruction to convert an upper case string to its lower case value:
section .text
    global _start         ;must be declared for using gcc
_start: ;tell linker entry point
        mov    ecx, len
        mov    esi, s1
        mov    edi, s2
loop_here:
 lodsb
 or      al, 20h
 stosb
 loop    loop_here 
 cld
 rep movsb
 mov edx,20 ;message length
 mov ecx,s2 ;message to write
 mov ebx,1 ;file descriptor (stdout)
 mov eax,4 ;system call number (sys_write)
 int 0x80 ;call kernel
 mov eax,1 ;system call number (sys_exit)
 int 0x80 ;call kernel
section .data
s1 db 'HELLO, WORLD', 0 ;source
len equ $-s1
section .bss
s2 resb 20              ;destination
When the above code is compiled and executed, it produces following result:
hello, world

Assembly - The LODS Instruction

In cryptography, a Caesar cipher is one of the simplest known encryption techniques. In this method, each letter in the data to be encrypted is replaced by a letter some fixed number of positions down the alphabet.
In this example, let us encrypt a data by simply replacing each alphabet in it with a shift of two alphabets, so a will be substituted by c, b with d and so on.
We use LODS to load the original string 'password' into the memory.
section .text
   global _start         ;must be declared for using gcc
_start:    ;tell linker entry point
   mov    ecx, len
   mov    esi, s1
   mov    edi, s2
loop_here:
   lodsb
   add al, 02
   stosb
   loop    loop_here          
   cld
   rep     movsb
   mov     edx,20   ;message length
   mov     ecx,s2   ;message to write
   mov     ebx,1    ;file descriptor (stdout)
   mov     eax,4    ;system call number (sys_write)
   int     0x80     ;call kernel
   mov     eax,1    ;system call number (sys_exit)
   int     0x80     ;call kernel
section .data
s1 db 'password', 0 ;source
len equ $-s1
section .bss
s2 resb 10         ;destination
When the above code is compiled and executed, it produces following result:
rcuuyqtf

Assembly - The MOVS Instruction

The MOVS instruction is used to copy a data item (byte, word or doubleword) from the source string to the destination string. The source string is pointed by DS:SI and the destination string is pointed by ES:DI.
The following example explains the concept:
section .text
    global _start         ;must be declared for using gcc
_start: ;tell linker entry point
 mov ecx, len
 mov esi, s1
 mov edi, s2
 cld
 rep movsb
 mov edx,20 ;message length
 mov ecx,s2 ;message to write
 mov ebx,1 ;file descriptor (stdout)
 mov eax,4 ;system call number (sys_write)
 int 0x80 ;call kernel
 mov eax,1 ;system call number (sys_exit)
 int 0x80 ;call kernel
section .data
s1 db 'Hello, world!',0 ;string 1
len equ $-s1
section  .bss
s2 resb 20              ;destination
When the above code is compiled and executed, it produces following result:
Hello, world!