Free2Code
 
Time: 2009-01-06, 01:22am
ASSEMLY - String Compression, Help Plz
Subject: ASSEMLY - String Compression, Help Plz  ·  Posted: 2006-05-28, 03:44am
Rank: ? (1)
Member #: 27655
Hello!
I need to make a programm in assembly (it´s better to use Emu8086) that
recieves the STRING from the keyboard and than compress it in this way: input string--> aaabbcddd output string--> 3a2b1c3d.
I know that it is very simple, but I´m just a beginner and can´t make it. I have just one day till tomorrow afternoon. Hope You´ll help me. Thnks

I have an exemple of a code here, but it doesn´t work

.model small
.data
inputstr db 80 dup (' '),'$' ;uncompressed input string & read from keyboard
outputstr db 80 dup (' '),'$' ;compressed output string & displayed
.code
start: mov ax,@data ;Prepare access to Data Segment
mov ds,ax
mov ah,0Ah ;DOS function "Read from file or device"
mov bx,0 ;Standard input device (keyboard)
mov cx,80 ;Maximum length of read input string
mov dx,offset inputstr ;Memory address of read input string
int 21h ;Call DOS function dispatcher
sub ax,2 ;Decrement by 2 the actual input string length(EOL)
mov si, offset inputstr
mov di, offset outputstr
L1: mov bx,si ;Check if we arrived the end of input string (While-type loop)
sub bx,offset inputstr
cmp bx,ax ;Compare with the length of read input string
je L4 ;If so jump to finish
mov cl,1 ;initialize the character counter
L2: mov dl,[si] ;Read the character
cmp dl,[si+1] ;Compare with the next character
jne L3 ;If not equal jump to constructing output string
inc cl ;If equal increment counter
inc si ;And address of next character
jmp L2 ;Repeat counting the current character
L3: mov [di],dl ;Construct the compressed output string. Save character
inc di
add cl,'0' ;Simple single digit conversion to ASCII
mov [di],cl ;Save the counter
inc di ;Prepare for next character
inc si ;
jmp L1 ;Repeat for next character (End of While-type loop)
L4: mov ah,09h ;DOS function "Write string to standard output"
mov dx,offset outputstr ;Display decompressed output string
int 21h ;Call DOS function dispatcher
mov ah,4ch ;DOS function "Exit"
int 21h ;Call DOS function dispatcher
end start


 
  Reply to this ·  Post link ·  Top
Subject: Re: ASSEMLY - String Compression, Help Plz  ·  Posted: 2006-10-09, 08:40am
Rank: ? (5)
Member #: 28723
hi,

there were some mistakes, especially the input function 0Ah /Int 21h works a bit different,
but the general idea was ok, seems to be working now (tested with 16 bit masm)

Code:
  1. .model tiny
  2. .data
  3. inputstr db 82 dup (' '),'$' ;uncompressed input string & read from keyboard
  4. outputstr db 80 dup (' '),'$' ;compressed output string & displayed
  5. crlf db 13,10,'$'
  6. .code
  7. .startup
  8.     mov ah,0Ah ; read string from keyboard
  9.     lea dx,inputstr ; offset of input param
  10.     mov byte ptr inputstr,80 ; 1.st byte contains max length
  11.     int 21h ;Call DOS function dispatcher
  12.     mov ah,byte ptr inputstr+1 ; 2.nd. byte contains actual input length
  13.     mov si,offset inputstr +2 ; actual start of input at offset 2
  14.     lea di, outputstr
  15. L1: 
  16.     mov bx,si ;Check if we arrived the end of input string (While-type loop)
  17.     sub bx,offset inputstr+2 ; +2 for real offset of input string
  18.     cmp bl,ah ;Compare with the length of read input string
  19.     je L4 ;If so jump to finish
  20.     mov cl,1 ;initialize the character counter
  21. L2: 
  22.     mov dl,[si] ;Read the character
  23.     cmp dl,[si+1] ;Compare with the next character
  24.     jne L3 ;If not equal jump to constructing output string
  25.     inc cl ;If equal increment counter
  26.     inc si ;And address of next character
  27.     jmp L2 ;Repeat counting the current character
  28. L3: 
  29.     add cl,'0' ;Simple single digit conversion to ASCII
  30.     mov [di],cl ;Save the counter , counter before character!
  31.     inc di ;Prepare for next character
  32.     mov [di],dl ;move character to outputstring
  33.     inc di ; next destination index
  34.     inc si ; next source index
  35.     jmp L1 ;Repeat for next character (End of While-type loop)
  36. L4:
  37.     mov ah,09h ;DOS function "Write string to standard output"
  38.     lea dx,crlf ;cr+ line feed
  39.     int 21h ;Call DOS function dispatcher
  40.     mov dx,offset outputstr ;Display decompressed output string
  41.     int 21h ;Call DOS function dispatcher
  42.     .exit
  43. end


 
  Reply to this ·  Post link ·  Top

Pages: 1

Please login or register to post a reply.

icons