Can someone tell me what is wrong?

Doing a Run Length Encoding program in Assembly...

Page 1 of 1

10 Replies - 1036 Views - Last Post: 22 November 2010 - 03:10 PM Rate Topic: -----

#1 JNDare  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-November 10

Can someone tell me what is wrong?

Posted 18 November 2010 - 12:49 PM

\\Hey ppl...i'm working on a program so he can descompact e compact files...this is code for descompact, but when i run in Turbo c++, it crashes, can someone tell me what is wrong or correct me?

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>




void decom(char *s)
{
//unsigned char s[20]="aaabccd";
//unsigned char s2[20];

asm{
		push ax
		push si
		push di
		
		lea di, s
		lea si, s
		mov ax,ds
	    	mov es,ax
		
		xor ch, ch
   }

while_: asm{
		mov al, [si]
		cmp al, '0'
		je fim
		cmp al, [si+1]
		jne write
		inc si
		mov ch,[si+1]
		inc si
		mov [di], al
		inc di
		mov [di], al
		inc di
		jmp write2
	}
write: asm{
		mov [di], al
		inc di
		inc si
		jmp while_
	}

write2: asm{
		cmp ch, 0
		je while2
		inc di
		mov [di], al
		dec ch
		jmp write2
	}
while2: asm{
		inc si
		jmp while_
	}

end: asm{
		//mov byte ptr [di], 0
 		pop di
		pop si
		pop ax

	}



printf("code text= %s",s);
getch();
//return 0;
  }

int main()
{
unsigned char s[20]="aa2EdFF5";
decom(s);

return 0;
}


MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 18 November 2010 - 01:20 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Can someone tell me what is wrong?

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: Can someone tell me what is wrong?

Posted 18 November 2010 - 12:59 PM

When you say this crashes are there any error messages when it crashes?

Please post in code tags.

Jim

This post has been edited by jimblumberg: 18 November 2010 - 01:00 PM

Was This Post Helpful? 0
  • +
  • -

#3 JNDare  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-November 10

Re: Can someone tell me what is wrong?

Posted 18 November 2010 - 01:29 PM

Nope, it doesn't give any error....it simply crashes after i debug it...
What do you mean in "post in code tag's" ? ...sorry i'm new at this...
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2896
  • View blog
  • Posts: 10,031
  • Joined: 08-August 08

Re: Can someone tell me what is wrong?

Posted 18 November 2010 - 01:37 PM

Read that stuff in the red box you see when you post a new topic.
Was This Post Helpful? 0
  • +
  • -

#5 JNDare  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-November 10

Re: Can someone tell me what is wrong?

Posted 18 November 2010 - 01:47 PM

Oh ok...like this?


#include <stdio.h>
#include <conio.h>
#include <stdlib.h>




void decom(char *s)
{
//unsigned char s[20]="aaabccd";
//unsigned char s2[20];

asm{
		push ax
		push si
		push di
		
		lea di, s
		lea si, s
		mov ax,ds
	    	mov es,ax
		
		xor ch, ch
   }

while_: asm{
		mov al, [si]
		cmp al, '0'
		je fim
		cmp al, [si+1]
		jne write
		inc si
		mov ch,[si+1]
		inc si
		mov [di], al
		inc di
		mov [di], al
		inc di
		jmp write2
	}
write: asm{
		mov [di], al
		inc di
		inc si
		jmp while_
	}
write2: asm{
		cmp ch, 0
		je while2
		inc di
		mov [di], al
		dec ch
		jmp write2
	}
while2: asm{
		inc si
		jmp while_
	}

end: asm{
		//mov byte ptr [di], 0
 		pop di
		pop si
		pop ax

	}



printf("code text= %s",s);
getch();
//return 0;
  }

int main()
{
unsigned char s[20]="aa2EdFF5";
decom(s);

return 0;
}



Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: Can someone tell me what is wrong?

Posted 18 November 2010 - 02:38 PM

Is there a specific reason you are using inline assembly?

Quote

Nope, it doesn't give any error....it simply crashes after i debug it...


Does this mean you are using the debugger? If so what line does the debugger fail on?


Jim
Was This Post Helpful? 0
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Can someone tell me what is wrong?

Posted 18 November 2010 - 02:47 PM

Question: Why write the code in assembly?

Generally the way I do things if I want to use some assembly optimized code is write the algorithm first in C then compile to assembly and then edit. For us to help you we now have to decipher the assembly.

The first thing that strikes me is line 29:je fim I don't see that "fim" is defined.

The "while" loop looks pretty unstable to. -- You seems to be looking for the '\0' char to know when you reached the end of the string, but you are incrementing si all over the place and so I would say that you maybe missing the '\0' char at the end.
Was This Post Helpful? 1
  • +
  • -

#8 JNDare  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-November 10

Re: Can someone tell me what is wrong?

Posted 22 November 2010 - 12:41 PM

Hi Ppl! Sorry i took so long to correct and answer those questions...i'm writing my program in assembly cause it's what the problem/project wants, oh and sorry for the word "fim", i'm portuguese...and in english "fim" is end, i tought that maybe the problem is ins write2, but i can't figure out what to do.


#include <stdio.h>
#include <conio.h>
#include <stdlib.h>




void decom(char *s)
{
//unsigned char s[20]="aaabccd";
//unsigned char s2[20];

asm{
		push ax
		push si
		push di
		
		lea di, s
		lea si, s
		mov ax,ds
	    	mov es,ax
		
		xor ch, ch
   }

while_: asm{
		mov al, [si]
		cmp al, '0'
		je end
		cmp al, [si+1]
		jne write
		inc si
		mov ch,[si+1]
		inc si
		mov [di], al
		inc di
		mov [di], al
		inc di
		jmp write2
	}
write: asm{
		mov [di], al
		inc di
		inc si
		jmp while_
	}

write2: asm{
		cmp ch, 0
		je while2
		inc di
		mov [di], al
		dec ch
		jmp write2
	}
while2: asm{
		inc si
		jmp while_
	}

end: asm{
		//mov byte ptr [di], 0
 		pop di
		pop si
		pop ax

	}



printf("code text= %s",s);
getch();
//return 0;
  }

int main()
{
unsigned char s[20]="aa2EdFF5";
decom(s);

return 0;
}



Was This Post Helpful? 0
  • +
  • -

#9 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Can someone tell me what is wrong?

Posted 22 November 2010 - 01:52 PM

I really think that the problem is that you are incrementing si in a bunch of places but have a single exist condition of [si] == '\0'

Your logic seems to be:
char di[100];
char *si = "aa2EdFF5";
char al;
while ((al = *si) != '\0') {
    if(al == *(si+1)) { 
        *di++ = al; 
        si++; 
        continue; 
    }
    si++;
    char ch = *(si+1);
    si++;
    *di++ = al;
    *di++ = al;
    while(ch != 0) {
      di++;
      *di = al;
      ch--;
    }
    si++

}


So what is it you are trying to do? What is your pseudocode? Because I am almost sure that the above code is NOT what you are trying to do.
Was This Post Helpful? 1
  • +
  • -

#10 JNDare  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-November 10

Re: Can someone tell me what is wrong?

Posted 22 November 2010 - 02:29 PM

What i'm trying to do is program that compact's and descompact's files, like you have a text "aaaabbcdeeeeefghhhij" and you put like "a3b1cde4fgh2ij", and otherwise. And the code that i show to you is to descompact...
Was This Post Helpful? 0
  • +
  • -

#11 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Can someone tell me what is wrong?

Posted 22 November 2010 - 03:10 PM

SO start out by thinking though how you want the algorithm to work. For me I would write a little program in C or C++.

First thing to realize is that you are looking at an ASCII encoding -- therefore the numbers are the ASCII chars 0x30 though 0x39. Your program is definitely not taking this into account.

#include <iostream>

using namespace std;

int main() {
    char data[] = "a3b1cde4fgh2ij";
    char result[256];
    char *si = data;
    char *di = result;

    while (*si != 0) {        
        char al = *si;
        //Is the next char a number
        char ch = *(si+1);
        if(ch >= 0x30 && ch <= 0x39) {
            ch = ch - 0x30; //convert digit to a number
            while (ch--) {
                *di++ = al;
            }
            si++; //we need to advance si 2 chars. This is one -- the regular advance is the second.
        } else {
            *di++ = al;
        }
        si++;
    }
    *di = 0;
    cout << result;
    return 0;
}


output:
> "C:\CProjects\Forum Help\topic200871_1.exe " 
aaabcdeeeefghhij


So now all we need to do is convert this to assembly. Well I programmed it so that it would be pretty easy to see what needs to happen.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1