Sunday 27 March 2016

Reverse Wordwise Code in Assembly Language | String Instructions in Assembly Language

Reverse Wordwise Code in Assembly Language | String Instructions in Assembly Language
Description:  Assume that the following variables are declared in the current data segment:


myString: db 'HaZa CS is very helpful',0
x: db 15
y: db 12

This assembly language routine ReverseWordwise (str,x,y) reverses the contents of this ASCII string, word-wise without using any temporary array (in place). Following is the sample output for the given string:

helpful very is CS HaZa

Also, only string instructions have been used for string processing (scanning/reading /writing).



Download Reverse Wordwise Code in Assembly Language


Assembly Language Code

[org 0x0100]

   jmp start

clearScreen:
   mov ax , 0xB800
   mov es , ax
   mov di , 0
   mov ax , 0x0720
   mov cx , 2000
  
   rep stosw
  
   ret
  
strlen:
   push bp
   mov bp , sp
  
   push ax
   push cx
   push es
   push di
  
   les di , [bp+4]
   mov cx , 0xFFFF
   xor al , al
   repne scasb
   mov ax , 0xFFFF
   sub ax , cx
   dec ax
   mov [bp+8] , ax
  
   pop di
   pop es
   pop cx
   pop ax
   pop bp
   ret 4

printStr:             ;[BP+10]=string  [BP+8]=strlen  [BP+6]=x-coordinate   [BP+4]=y-coordinate
   push bp
   mov bp , sp
   push ax
   push cx
   push si
   push di
   push es
  
   mov ax , 0xB800
   mov es , ax
  
   mov al , 80
   mul byte[bp+4]
   add ax , [bp+6]
   shl ax , 1
   mov di , ax
  
   mov si , [bp+10]
   mov cx , [bp+8]
  
   mov ah , 0x07
   print:
      lodsb
      stosw
      loop print
     
   pop es
   pop di
   pop si
   pop cx
   pop ax
   pop bp
   ret 8
  
ReverseAlphabets:            ;[BP+6]=string     [BP+4]=strlen
   push bp
   mov bp , sp
  
   push ax
   push cx
   push dx
   push si
   push di
  
   mov dx , 0
   mov ax , [bp+4]
   mov cx , 2
   div cx
  
   mov dx , ax
   mov cx , 0
  
   mov ax , ds
   mov es , ax
   mov si , [bp+6]
   mov di , [bp+6]
  
   workReverseAlphabets:
      lodsb
      add si , [bp+4]
      sub si , 2
      sub si , cx
      sub si , cx
      movsb
      mov di , si
      dec di
      stosb
      inc cx
      cmp cx , dx
      jnl finishReverseAlphabets
      mov si , [bp+6]
      add si , cx
      mov di , [bp+6]
      add di , cx
      jmp workReverseAlphabets
  
   finishReverseAlphabets:
      pop di
      pop si
      pop dx
      pop cx
      pop ax
      pop bp
      ret
     
ReverseWordwise:      ; [BP+10]=local variable  [BP+8]=string  [BP+6]=x-coordinate   [BP+4]=y-coordinate
   push bp
   mov bp , sp
  
   push ax
   push bx
   push cx
   push dx
   push si
   push di
   push es
  
   mov di , [bp+8]
   sub sp , 2
   push ds
   push di
   call strlen
  
   pop ax
   mov [bp+10] , ax          ;[BP+6]=strlen
  
   push word [bp+8]      ;push string
   push word [bp+10]     ;push strlen
   call ReverseAlphabets
   add sp , 4
  
   mov ax , ds
   mov es , ax
   mov di , [bp+8]
   push di
   mov al , ' '
   mov cx , [bp+10]
   reverseNext:
      repne scasb
      mov dx , [bp+10]
      sub dx , cx
      dec dx
      pop bx
      sub dx , bx
      add dx , [bp+8]
      push bx
      jcxz size_inc
      jmp proceed
      size_inc:
          inc dx
      proceed:
      push dx
      call ReverseAlphabets
      jcxz Printing
      pop dx
      pop bx
      add bx , dx
      inc bx
      push bx
      jmp reverseNext
  
   Printing:
      add sp , 4         ;clears the stack
      call clearScreen
      push word [bp+8]
      push word [bp+10]
      push word [bp+6]
      push word [bp+4]
      call printStr
  
      pop es
      pop di
      pop si
      pop dx
      pop cx
      pop bx
      pop ax
      pop bp
      ret 4

start:
   sub sp , 2         ;local variable
   push string
   mov ax , 0
   mov al , [posX]
   push ax
   mov al , [posY]
   push ax
   call ReverseWordwise
  
   mov ax , 0x4c00
   int 0x21
  
posX: db  15
posY: db 12

string:db 'HaZa CS is very helpful',0


No comments

Post a Comment

Recent Posts