943,782 Members | Top Members by Rank

Ad:
  • Assembly Discussion Thread
  • Unsolved
  • Views: 7593
  • Assembly RSS
May 2nd, 2008
0

Convert C code to Assembly code

Expand Post »
Hello, I tried to convert below code, but I couldn't.. What is wrong?
  1. void invert(unsigned char *image, int width, int height){
  2. for(int i=0;i<width*height;i++)
  3. image[i]=255-image[i];
  4. }
assembly Syntax (Toggle Plain Text)
  1. ---------------------ASM---------
  2. invert PROC
  3. push ebp ; save ebp register
  4. mov ebp,esp ; get current stack pointer
  5.  
  6. mov esi,[ebp+8] ; get first parameter
  7. mov eax,[ebp+12] ; get second parameter
  8. mov ebx,[ebp+16] ; get third parameter
  9. mul ebx ; eax=eax * ebx
  10. mov ecx,eax ; get image size
  11. dec ecx
  12. mov al,255
  13.  
  14. L1:
  15. sub al,[esi] ; al=al-image[i]
  16. mov [esi],al ; image[i]=al
  17. inc esi
  18. loop L1
  19.  
  20. pop ebp ; restore ebp
  21. ret ; return to caller
  22.  
  23. invert ENDP
--------------------------------------------------
please help me....thanks...
Last edited by Ancient Dragon; May 2nd, 2008 at 4:54 pm. Reason: add code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
guneky is offline Offline
1 posts
since May 2008
May 3rd, 2008
0

Re: Convert C code to Assembly code

Click to Expand / Collapse  Quote originally posted by guneky ...
Hello, I tried to convert below code, but I couldn't..
Actually that's not correct, you did convert it and a reasonably good job too, the only problem is it's not working as expected.

1. You only initialize AL to -1 (255 or 0FFH) once and then inside your loop it's modified.

asm Syntax (Toggle Plain Text)
  1. L1: push eax
  2. sub al, [esi]
  3. stosb
  4. pop eax
  5. loop L1

Note: mov [esi], al and then incrementing ESI is not wrong, it's just STOSB saves code space.
Reputation Points: 47
Solved Threads: 17
Posting Whiz in Training
Tight_Coder_Ex is offline Offline
215 posts
since Feb 2005
Jan 26th, 2011
0
Re: Convert C code to Assembly code
pls tel d 8951 assembly code for follwing c program......

#include<reg51.h>
void delay(unsigned int time);
void gate_open();
void gate_close();
unsigned char Rx,coin;

int k=0;
sbit L1=P0^3; //LED connected

sbit E_M=P0^0; //L293D enable
sbit L293_A=P0^1; //L293D INPUT1
sbit L293_B=P0^2; //L293D INPUT2

sbit C=P1^0; //coin arrived
sbit S1=P1^1; //BOTTOM SENSOR
sbit S2=P1^2; //TOP SENSOR


void main() //main function
{
unsigned int i=0,j=0;

P0=0X00;
P1=0XFF; // setting port1 as input port
P2=0X00;
P3=0X00;



while(1)
{
while(S1==0); //wait till a vehicle arrives
delay(100);
if(S2==1)
{
coin=2; //heavy vehicle
}
else
{
coin=1; //light vehicle
}
L1=1;

while(coin > 0) //count coins
{
while(C==1);
delay(100);
while(C==0);
coin--;
}
L1=0;
delay(100);
gate_open();
delay(400);
gate_close();

}


}

void delay(unsigned int time) //delay time=t ms
{
unsigned int i,j;
for(i=0;i<time;i++)
for(j=0;j<1275;j++);
}

void gate_open()
{
L293_A=1;
L293_B=0;
E_M=1;
delay(400);
E_M=0;
L293_A=0;
L293_B=0;


}
void gate_close()
{
L293_A=0;
L293_B=1;
E_M=1;
delay(400);
E_M=0;
L293_A=0;
L293_B=0;

}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gracy zacharias is offline Offline
1 posts
since Jan 2011

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Assembly Forum Timeline: Trouble with compareing strings
Next Thread in Assembly Forum Timeline: how to visualize on screen an integer





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC