954,490 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Convert C code to Assembly code

Hello, I tried to convert below code, but I couldn't.. What is wrong?

void invert(unsigned char *image, int width, int height){
         for(int i=0;i<width*height;i++)
               image[i]=255-image[i];
}
---------------------ASM---------
invert PROC
	push ebp					; save ebp register
	mov ebp,esp					; get current stack pointer
	
	mov esi,[ebp+8]				; get first parameter
	mov eax,[ebp+12]			; get second parameter
	mov ebx,[ebp+16]			; get third parameter
	mul ebx						; eax=eax * ebx
	mov ecx,eax					; get image size
	dec ecx
	mov al,255
	
	L1:
		sub al,[esi]			; al=al-image[i]
		mov [esi],al			; image[i]=al
		inc esi
		loop L1
	
	pop ebp						; restore ebp
	ret							; return to caller
	
invert ENDP

--------------------------------------------------
please help me....thanks...

guneky
Newbie Poster
1 post since May 2008
Reputation Points: 10
Solved Threads: 0
 
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.

L1:    push    eax
         sub      al, [esi]
         stosb
         pop     eax
         loop    L1


Note: mov [esi], al and then incrementing ESI is not wrong, it's just STOSB saves code space.

Tight_Coder_Ex
Posting Whiz in Training
215 posts since Feb 2005
Reputation Points: 47
Solved Threads: 17
 

pls tel d 8951 assembly code for follwing c program......

#include
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

gracy zacharias
Newbie Poster
1 post since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You