Convert C code to Assembly code

Please support our Assembly advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2008
Posts: 1
Reputation: guneky is an unknown quantity at this point 
Solved Threads: 0
guneky guneky is offline Offline
Newbie Poster

Convert C code to Assembly code

 
0
  #1
May 2nd, 2008
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. }
  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
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 199
Reputation: Tight_Coder_Ex is an unknown quantity at this point 
Solved Threads: 14
Tight_Coder_Ex's Avatar
Tight_Coder_Ex Tight_Coder_Ex is offline Offline
Junior Poster

Re: Convert C code to Assembly code

 
0
  #2
May 3rd, 2008
Originally Posted by guneky View Post
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.

  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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC