How do I convert c code to asm

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

Join Date: Jul 2009
Posts: 1
Reputation: kiuhnmgtrdcv is an unknown quantity at this point 
Solved Threads: 0
kiuhnmgtrdcv kiuhnmgtrdcv is offline Offline
Newbie Poster

How do I convert c code to asm

 
0
  #1
Jul 19th, 2009
hi everyone
I hope you can help me with converting some c code into assembly code, i have tried much now, and i still don't get it.
i'm new to programming ASM
this c code gets two number from the user to draw an ellipse
  1. #include<stdio.h>
  2. #include<graphics.h>
  3. #include<math.h>
  4. #include<dos.h>
  5. void main()
  6. {
  7. long d1,d2;
  8. long rx,ry,rxsq,rysq,tworxsq,tworysq,dx,dy;
  9. int gd=DETECT,gm,x,y;
  10. clrscr();
  11. printf("enter x radius of the ellipse(20)");
  12. scanf("%ld",&rx);
  13. printf("enter y radius of the ellipse(40)");
  14. scanf("%ld",&ry);
  15. initgraph(&gd,&gm,"");
  16.  
  17. rxsq=rx*rx;
  18. rysq=ry*ry;
  19. tworxsq=2*rxsq;
  20. tworysq=2*rysq;
  21. x=0;
  22. y=ry;
  23. d1=rysq-rxsq*ry+(0.25*rxsq);
  24. dx=tworysq*x;
  25. dy=tworxsq*y;
  26.  
  27.  
  28. do{
  29. putpixel(200+x,200+y,15);
  30. putpixel(200-x,200-y,15);
  31. putpixel(200+x,200-y,15);
  32. putpixel(200-x,200+y,15);
  33. if(d1<0)
  34. {
  35. x=x+1;
  36. y=y;
  37. dx=dx+tworysq;
  38. d1=d1+dx+rysq;
  39. }
  40. else
  41. {
  42. x=x+1;
  43. y=y-1;
  44. dx=dx+tworysq;
  45. dy=dy-tworxsq;
  46. d1=d1+dx-dy+rysq;
  47. }
  48. delay(200);
  49. }
  50. while(dx<dy); // in dige yani chi ?
  51. d2=rysq*(x+0.5)*(x+0.5)+rxsq*(y-1)*(y-1)-rxsq*rysq;
  52. do{
  53. putpixel(200+x,200+y,15);
  54. putpixel(200-x,200-y,15);
  55. putpixel(200+x,200-y,15);
  56. putpixel(200-x,200+y,15);
  57. if(d2>0){
  58. x=x;
  59. y=y-1;
  60. dy=dy-tworxsq;
  61. d2=d2-dy+rxsq;
  62. }
  63. else
  64.  
  65.  
  66. {
  67. x=x+1;
  68. y=y-1;
  69. dy=dy-tworxsq;
  70. dx=dx+tworysq;
  71. d2=d2+dx-dy+rxsq;
  72. }
  73. }
  74. while(y>0);
  75. getch();
  76. }
my asm code
  1. putpix macro x,y
  2. mov cx,x
  3. mov dx,y
  4. mov ah,0ch
  5. mov al,9
  6. int 10h
  7. endm
  8.  
  9. init macro
  10. mov al,13h
  11. mov ah,0
  12. int 10h
  13. endm
  14.  
  15. .model small
  16. .stack 128
  17. .data
  18. d1 dw 0
  19. d2 dw 0
  20. rx dw 0
  21. ry dw 0
  22. rxsq dw 0
  23. rysq dw 0
  24. tworxsq dw 0
  25. tworysq dw 0
  26. x dw 0
  27. y dw 0
  28. dxx dw 0
  29. dyy dw 0
  30. km1 dw 0
  31. km2 dw 0
  32. km3 dw 0
  33. two dw 2
  34. four dw 4
  35.  
  36. .code
  37. main proc far
  38. mov ax,@data
  39. mov ds,ax
  40. init
  41. mov rx,30
  42. mov ry,10
  43.  
  44. mov ax,rx
  45. mul ax
  46. mov rxsq,ax
  47.  
  48. mov ax,ry
  49. mul ax
  50. mov rysq,ax
  51.  
  52. mov ax,rxsq
  53. mul two
  54. mov tworxsq,ax
  55.  
  56. mov ax,rysq
  57. mul two
  58. mov tworysq,ax
  59.  
  60. mov x,0
  61. mov bx,ry
  62. mov y,bx
  63. ;-----
  64. ;d1=rysq-rxsq*ry+(0.25*rxsq)
  65. ;4d1=4rysq-4rxsq*ry+(rxsq);
  66. ;
  67. mov ax,rysq
  68. mul four
  69. mov cx,ax
  70. ;
  71. mov ax,rxsq
  72. mul ry
  73. mul four
  74. mov bx,ax
  75. ;
  76. sub cx,bx
  77. add cx,rxsq
  78. ;
  79. mov ax,cx
  80. div four
  81. mov d1,ax
  82. ;-----
  83. mov ax,rxsq
  84. mul two
  85. mov dxx,ax
  86. mov ax,rysq
  87. mul two
  88. mov dyy,ax
  89. ;***loop1
  90. lw1:cmp dxx,ax
  91. jge endw1
  92. ;200+x
  93. mov bx,x
  94. add bx,200
  95. ;200+y
  96. mov cx,y
  97. add cx,200
  98. putpix bx,cx
  99. ;200-x
  100. mov bx,x
  101. sub bx,200
  102. neg bx
  103. ;200-y
  104. mov cx,y
  105. sub cx,200
  106. neg cx
  107. putpix bx,cx
  108. ;200+x
  109. mov bx,x
  110. add bx,200
  111. ;200-y
  112. mov cx,y
  113. sub cx,200
  114. neg cx
  115. putpix bx,cx
  116. ;200-x
  117. mov bx,x
  118. sub bx,200
  119. neg bx
  120. ;200+y
  121. mov cx,y
  122. add cx,200
  123. putpix bx,cx
  124. cmp d1,0
  125. jge else1
  126. inc x
  127. mov bx,tworysq
  128. add dxx,bx
  129. mov bx,rysq
  130. add d1,bx
  131. jmp ifexit1
  132. else1:
  133. inc x
  134. dec y
  135. mov bx,tworysq
  136. add dxx,bx
  137. mov bx,tworxsq
  138. sub dyy,bx
  139. ;d1=d1+dx-dy+rysq;
  140. mov bx,dxx
  141. sub bx,dyy
  142. add bx,rysq
  143. add d1,bx
  144. ifexit1:
  145. mov cx,09999h
  146. l12:
  147. loop l12
  148. jmp lw1
  149. endw1:
  150. ;d2=rysq*(x+0.5)*(x+0.5)+rxsq*(y-1)*(y-1)-rxsq*rysq;
  151. ;4d2=rysq*(2x+1)*(2x+1)+4rxsq*(y-1)*(y-1)-4rxsq*rysq;
  152. ;4d2=km1+km2+km3
  153. ;km1
  154. mov ax,x
  155. mul two
  156. add ax,1
  157. mul ax
  158. mul rysq
  159. mov km1,ax
  160. ;km2
  161. mov ax,y
  162. dec ax
  163. mul ax
  164. mul rxsq
  165. mul four
  166. mov km2,ax
  167. ;km3
  168. mov ax,rxsq
  169. mul ax
  170. mul four
  171. mov km3,ax
  172.  
  173. mov ax,km1
  174. add ax,km2
  175. add ax,km3
  176. div four
  177. mov d2,ax
  178.  
  179. ;***loop2
  180. lw2:
  181. cmp y,0
  182. jle endw2
  183. ;200+x
  184. mov bx,x
  185. add bx,200
  186. ;200+y
  187. mov cx,y
  188. add cx,200
  189. putpix bx,cx
  190. ;200-x
  191. mov bx,x
  192. sub bx,200
  193. neg bx
  194. ;200-y
  195. mov cx,y
  196. sub cx,200
  197. neg cx
  198. putpix bx,cx
  199. ;200+x
  200. mov bx,x
  201. add bx,200
  202. ;200-y
  203. mov cx,y
  204. sub cx,200
  205. neg cx
  206. putpix bx,cx
  207. ;200-x
  208. mov bx,x
  209. sub bx,200
  210. neg bx
  211. ;200+y
  212. mov cx,y
  213. add cx,200
  214. putpix bx,cx
  215.  
  216. cmp d2,0
  217. jle else2
  218. dec y
  219. mov bx,tworxsq
  220. sub dyy,bx
  221. ;d2=d2-dy+rxsq;
  222. mov bx,rxsq
  223. add bx,dyy
  224. sub d2,bx
  225. jmp ifexit2
  226. else2:
  227. inc x
  228. dec y
  229. mov bx,tworxsq
  230. sub y,bx
  231. add dx,tworysq
  232. ;d2=d2+dx-dy+rxsq;
  233. mov bx,dxx
  234. add bx,rxsq
  235. sub bx,dyy
  236. sub d2,dx
  237. ifexit2:
  238. jmp lw2
  239. endw2:
  240. getch:mov ah,0
  241. int 16h
  242. mov ah,4ch
  243. int 21h
  244.  
  245. main endp
  246. end main
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: How do I convert c code to asm

 
0
  #2
Jul 19th, 2009
Looks like you're trying to do a deviation of the Bresenham DDA algorithm. Is this a class assignment or a personal or work project?

This is a big function. One method is to dump the assembly code generated by the compiler to get some ideas.

It also looks like you're not trying to write this for speed! You're stalling the processor almost all the time!

Interesting thing about a DDA algorithm, it uses a binary base in conjunction with increment/decrement. Rework your C code to be closer in appearance to the assembly!

How volatile is the putpixel() function? If X,Y are preserved then no need to move from memory to registers for each pixel. Just modify the displacement!

Years ago I wrote a graphics driver for drawing lines, circles, etc. With the Bresenham DDA algorithm but using self modifying code for fastest speed! If you're using an older pre Pentium computer you should be able to do the same. But one step at a time.

Okay, reformating your code while typing this have noticed a few things in the c code! Shouldn't y=y not be there? And It looks like your error correction is d1 ? (e) might be more familiar here!

Stop mixing floating-point with your integer code. Not
*0.25 instead >>2

You're dealing with integers...

d1 = rysq - rxsq*ry + (0.25*rxsq);
should be more like
d1 = rysq - rxsq*ry + (rxsq >>2);


DO NOT...
mul 4
Do a shift
  1. shl ax,2
much much faster!!!

  1. div 4
  2. asr ax,2

Your operations such as...
  1. mov bx,x
  2. sub bx,200
  3. neg bx
...is WRONG!
- ( x - 200)
See the problem!
Instead use something like the following!


Store your 200 in a register, interlace the +/- x/y math then process.
  1. mov si,200 ; Center X axis
  2. mov di,200 ; Center Y axis
  3.  
  4. mov bx,si ; +x +y
  5. mov cx,di
  6. add bx,x
  7. add cx,y
  8. putpix bx,cx
  9.  
  10. mov bx,si ; -x -y
  11. mov cx,di
  12. sub bx,x
  13. sub cx,y
  14. putpix bx,cx
  15.  
  16. mov bx,si ; +x -y
  17. mov cx,di
  18. add bx,x
  19. sub cx,y
  20. putpix bx,cx
  21.  
  22. mov bx,si ; -x +y
  23. mov cx,di
  24. sub bx,x
  25. add cx,y
  26. putpix bx,cx
Last edited by wildgoose; Jul 19th, 2009 at 6:28 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: How do I convert c code to asm

 
0
  #3
Jul 19th, 2009
My question is, why are you still using fossil-ware?
I mean, how many Gigs of (processor speed, memory, hard disk) do you have?

A whole football field to play in, and you're still scribbling on the back of an old envelope.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 15
Reputation: kiuhnmgrtdcv is an unknown quantity at this point 
Solved Threads: 0
kiuhnmgrtdcv kiuhnmgrtdcv is offline Offline
Newbie Poster

Re: How do I convert c code to asm

 
0
  #4
Jul 19th, 2009
thank you Mr wildgoose for your reply
i fix some errors
i fix putmix macro
and i use shift operation instead of mul
and fixed some other things like x=x & y=y but still doesn't work
  1. putpix macro x,y
  2. mov ax,0A000h
  3. mov es,ax ;{es = $A000}
  4. mov ax,y
  5. shl ax,6
  6. mov bx,ax ;{bx = y * 64}
  7. shl ax,1
  8. shl ax,1 ;{ax = y * 256}
  9. add bx,ax ;{bx = y * 320}
  10. add bx,x ;{bx = y * 320 + x}
  11. mov al,9 ;{load color}
  12. mov es:[bx],al ;{set point}
  13. endm
  14.  
  15. init macro
  16. mov al,13h
  17. mov ah,0
  18. int 10h
  19. endm
  20.  
  21. .model small
  22. .stack 128
  23. .data
  24. d1 dw 0
  25. d2 dw 0
  26. rx dw 0
  27. ry dw 0
  28. rxsq dw 0
  29. rysq dw 0
  30. tworxsq dw 0
  31. tworysq dw 0
  32. x dw 0
  33. y dw 0
  34. dxx dw 0
  35. dyy dw 0
  36. km1 dw 0
  37. km2 dw 0
  38. km3 dw 0
  39. two dw 2
  40. four dw 4
  41.  
  42. .code
  43. main proc far
  44. mov ax,@data
  45. mov ds,ax
  46. init
  47. mov rx,30
  48. mov ry,10
  49.  
  50. mov ax,rx
  51. mul ax
  52. mov rxsq,ax
  53.  
  54. mov ax,ry
  55. mul ax
  56. mov rysq,ax
  57.  
  58. ; tworxsq=2*rxsq;
  59. ; tworysq=2*rysq;
  60. mov ax,rxsq
  61. shl ax,1
  62. mov tworxsq,ax
  63.  
  64. mov ax,rysq
  65. shl ax,1
  66. mov tworysq,ax
  67. ;x=0;
  68. ;y=ry;
  69. mov x,0
  70. mov bx,ry
  71. mov y,bx
  72. ;-----
  73. ;d1=rysq-rxsq*ry+(0.25*rxsq)
  74. ;d1=rysq-rxsq*ry+(rxsq>>4);
  75. ;
  76. mov bx,rxsq
  77. shr bx,2
  78. add bx,rysq
  79.  
  80. mov ax,rxsq
  81. mul ry
  82. sub ax,bx
  83. neg ax
  84. mov d1,ax
  85. ;-----
  86. ;dx=tworysq*x;
  87. mov ax,tworysq
  88. mul x
  89. mov dxx,ax
  90. ;dy=tworxsq*y;
  91. mov ax,tworxsq
  92. mul y
  93. mov dyy,ax
  94. ;***loop1
  95. lw1:cmp dxx,ax
  96. jge endw1
  97. mov si,200 ; Center X axis
  98. mov di,200 ; Center Y axis
  99. mov bx,si ; +x +y
  100. mov cx,di
  101. add bx,x
  102. add cx,y
  103. putpix bx,cx
  104.  
  105. mov bx,si ; -x -y
  106. mov cx,di
  107. sub bx,x
  108. sub cx,y
  109. putpix bx,cx
  110.  
  111. mov bx,si ; +x -y
  112. mov cx,di
  113. add bx,x
  114. sub cx,y
  115. putpix bx,cx
  116.  
  117. mov bx,si ; -x +y
  118. mov cx,di
  119. sub bx,x
  120. add cx,y
  121. putpix bx,cx
  122. cmp d1,0
  123. jge else1
  124. ;y=y; ???
  125. mov y,y
  126. inc x
  127. mov bx,tworysq
  128. add dxx,bx
  129. mov bx,rysq
  130. add d1,bx
  131. jmp ifexit1
  132. else1:
  133. inc x
  134. dec y
  135. mov bx,tworysq
  136. add dxx,bx
  137. mov bx,tworxsq
  138. sub dyy,bx
  139. ;d1=d1+dx-dy+rysq;
  140. mov bx,dxx
  141. sub bx,dyy
  142. add bx,rysq
  143. add d1,bx
  144. ifexit1:
  145. mov cx,09999h
  146. l12:
  147. loop l12
  148. jmp lw1
  149. endw1:
  150. ;d2=rysq*(x+0.5)*(x+0.5)+rxsq*(y-1)*(y-1)-rxsq*rysq;
  151. ;d2=rysq*(x+0.5)*(x+0.5)+rxsq*(y-1)*(y-1)-rxsq*rysq;
  152. ;4d2=rysq*(2x+1)*(2x+1)+4rxsq*(y-1)*(y-1)-4rxsq*rysq;
  153. ;4d2=km1+km2+km3
  154. ;km1
  155. mov ax,x
  156. mul two
  157. add ax,1
  158. mul ax
  159. mul rysq
  160. mov km1,ax
  161. ;km2
  162. mov ax,y
  163. dec ax
  164. mul ax
  165. mul rxsq
  166. mul four
  167. mov km2,ax
  168. ;km3
  169. mov ax,rxsq
  170. mul ax
  171. mul four
  172. mov km3,ax
  173. mov ax,km1
  174. add ax,km2
  175. add ax,km3
  176. div four
  177. mov d2,ax
  178. ;***loop2
  179. lw2:
  180. cmp y,0
  181. jle endw2
  182. mov si,200 ; Center X axis
  183. mov di,200 ; Center Y axis
  184. mov bx,si ; +x +y
  185. mov cx,di
  186. add bx,x
  187. add cx,y
  188. putpix bx,cx
  189.  
  190. mov bx,si ; -x -y
  191. mov cx,di
  192. sub bx,x
  193. sub cx,y
  194. putpix bx,cx
  195.  
  196. mov bx,si ; +x -y
  197. mov cx,di
  198. add bx,x
  199. sub cx,y
  200. putpix bx,cx
  201.  
  202. mov bx,si ; -x +y
  203. mov cx,di
  204. sub bx,x
  205. add cx,y
  206. putpix bx,cx
  207. cmp d2,0
  208. jle else2
  209. dec y
  210. mov bx,tworxsq
  211. sub dyy,bx
  212. ;d2=d2-dy+rxsq;
  213. mov bx,dyy
  214. sub bx,rxsq ;
  215. neg bx
  216. add d2,bx
  217. jmp ifexit2
  218. else2:
  219. inc x
  220. dec y
  221. mov bx,tworxsq
  222. sub y,bx
  223. add dx,tworysq
  224. ;d2=d2+dx-dy+rxsq;
  225. mov bx,dxx
  226. add bx,rxsq
  227. sub bx,dyy
  228. add d2,dx
  229. ifexit2:
  230. jmp lw2
  231. endw2:
  232. getch:mov ah,0
  233. int 16h
  234. mov ah,4ch
  235. int 21h
  236. main endp
  237. end main
Last edited by kiuhnmgrtdcv; Jul 19th, 2009 at 9:55 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: How do I convert c code to asm

 
1
  #5
Jul 19th, 2009
Of course it doesn't work. You were suppose to analyze what I did, review your code, and single-step, etc. and make appropriate changes! One typically doesn't learn by being handed the answer. They typically need to be nudged into the correct direction so they can hopefully find the answer themselves!

Did you analyze what putpix does? If you had you would have seen that it uses bx for the offset calculation for the 256 color pixel write. You were using the BIOS call before, but now you are writing directly to memory.

What two registers are you passing in? AND what registers are pixel write using?

What processor are you using? This may be 16-bit 8086 code but are you running on a modern day Pentium type or an old 8086 through 80486? If a newer computer then using the old shifting for the multiplication is a mistake! On the older processors multiple shifts waas MUCH faster then a multiply. But on the newer processors it is quite the reverse of that!

I don't like heavy macros like you're using because of this kind of mistake. YOU DO NOT NEED TO KEEP RESETTING ES to the Video segment!!! You aren't using it anywhere else, so set it at your code initialization, and not in the write for each pixel!

Second, You aren't calling a function to plot each pixel so why are you using real X and real Y for each pixel write?

How many bytes in a stride? # of pixels for one pixel row? According to your own code 320. You use an anchor point each time.

so once you calculate your starting position (anchor point of elipse).
(+x) gets right edge -(x*2) gets left edge
(+y) gets bottom edge -(y*2) gets top edge.

Once you get this working post again, there are multitudes of optimizations you can do!
Last edited by wildgoose; Jul 19th, 2009 at 2:10 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 15
Reputation: kiuhnmgrtdcv is an unknown quantity at this point 
Solved Threads: 0
kiuhnmgrtdcv kiuhnmgrtdcv is offline Offline
Newbie Poster

Re: How do I convert c code to asm

 
0
  #6
Jul 19th, 2009
Originally Posted by Salem View Post
My question is, why are you still using fossil-ware?
I mean, how many Gigs of (processor speed, memory, hard disk) do you have?

A whole football field to play in, and you're still scribbling on the back of an old envelope.
this is my class assignment and i don't know why should i learn
i have no plan to develope an os or write the driver of the graphic card
i know with assembly we can crack applications through disAsm..
personaly i use c# and java to earn money
thank you Mr wildgoose for commenting
Last edited by kiuhnmgrtdcv; Jul 19th, 2009 at 3:43 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: How do I convert c code to asm

 
0
  #7
Jul 19th, 2009
There is nothing wrong with starting with basic Assembly Language.
How are you coming with the rework?
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 15
Reputation: kiuhnmgrtdcv is an unknown quantity at this point 
Solved Threads: 0
kiuhnmgrtdcv kiuhnmgrtdcv is offline Offline
Newbie Poster

Re: How do I convert c code to asm

 
0
  #8
Jul 27th, 2009
finally i find a source that draws an ellipse!
but i have a problem when i try to tasm it please help
http://img2.pict.com/5c/e0/0d/1348515/0/untitled.gif
  1. .MODEL small
  2. .DATA
  3.  
  4. ; Draws an ellipse using Floating point arithmetic
  5.  
  6. X_centre EQU [si] ;Centre of the ellipse
  7. Y_centre EQU [si+2] ;
  8. A EQU word ptr [si+4] ;Ellipse 1/2 width
  9. B EQU word ptr [si+6] ;Ellipse 1/2 height
  10. colour EQU byte ptr [si+8] ;Colour attribute
  11. plotadot EQU [si+14] ;Address of the pixel plotting routine
  12.  
  13. X_offset dw ? ;X distance from ellipse centre
  14. Y_offset dw ? ;Y " " "
  15. temp dw ? ;Scratch variable
  16. ;-------------------------------------------------------------------------------
  17. .CODE
  18. PUBLIC ellipse
  19. ;-----------------------------------------------;
  20. ; Plot the ellipse defined by the equation ;
  21. ; ;
  22. ; X**2/A**2 + Y**2/B**2 = 1 ;
  23. ; ;
  24. ; Where, 2A is the ellipse width and B is the ;
  25. ; ellipse height. A is > B ;
  26. ; ;
  27. ; Entry: si = address of parameter block ;
  28. ; es = Address of video memory ;
  29. ;-----------------------------------------------;
  30.  
  31. ellipse proc uses ax bx cx dx
  32.  
  33. mov al,colour
  34.  
  35. ; 1st plot the pixels at the extremities of the ellipse
  36.  
  37. mov cx,X_centre
  38. mov dx,Y_centre
  39. add dx,B ;Add on the ellipse 1/2 height
  40.  
  41. call plotadot ;Top pixel located above centre location
  42.  
  43. mov dx,Y_centre
  44. sub dx,B ;Subtract the ellipse 1/2 height
  45.  
  46. call plotadot ;Bottom pixel located below the centre
  47.  
  48. add cx,A ;Add the ellipse 1/2 width
  49. mov dx,Y_centre
  50.  
  51. call plotadot ;Rightmost pixel
  52.  
  53. mov cx,X_centre
  54. sub cx,A ;Subtract the ellipse 1/2 width
  55.  
  56. call plotadot ;Leftmost pixel
  57.  
  58. ; Calculate the Y coordinates corresponding to all positions X from centre
  59.  
  60. mov cx,A ;Set loop index to ellipse 1/2 width
  61. dec cx ;Don't need to do the extreme positions
  62. eloop: push cx ;Save the loop index
  63.  
  64. mov temp,cx ;Get the X position
  65. fild temp
  66. fimul temp ;st=X**2
  67. fild A ;st=A,st(1)=X**2
  68. fimul A ;st=A**2,st(1)=X**2
  69.  
  70. fdiv ;st=X**2/A**2
  71. fld1 ;st=1, st(1)=X**2/A**2
  72. fsubr ;st = 1 - X**2/A**2
  73. fsqrt ;st = SQRT(1-X**2/A**2)
  74. fimul B ;st = B*SQRT(1-X**2/A**2)
  75. fistp y_offset
  76.  
  77. mov dx,Y_offset
  78. mov X_offset,cx ;Take a copy of the X offset
  79. mov cx,X_centre
  80. add cx,X_offset
  81.  
  82. mov dx,Y_centre
  83. add dx,Y_offset
  84.  
  85. call plotadot ;X_centre+X_offset, Y_centre+Y_offset
  86.  
  87. mov dx,Y_centre
  88. sub dx,Y_offset
  89.  
  90. call plotadot ;X_centre+X_offset, Y_centre-Y_offset
  91.  
  92. mov cx,X_centre
  93. sub cx,X_offset
  94.  
  95. call plotadot ;X_centre-X_offset, Y_centre-Y_offset
  96.  
  97. mov dx,Y_centre
  98. add dx,Y_offset
  99.  
  100. call plotadot ;X_centre_X_offset, Y_centre+Y_offset
  101.  
  102. skip: pop cx ;Restore the loop index
  103. loop eloop
  104.  
  105. ret
  106. ellipse endp
  107. end

http://img2.pict.com/5c/e0/0d/1348515/0/untitled.gif
Last edited by kiuhnmgrtdcv; Jul 27th, 2009 at 7:30 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: How do I convert c code to asm

 
0
  #9
Jul 27th, 2009
USES is typically a MASM thing and its about register preservation. I don't use TASM but you can remove the uses line, and merely push bx yourself. AX, CX, DX are typically scratch but BX is preserved thus make sure it gets pushed, and then popped.

The version of TASM you are using is DOS based and thus 16-bit (REAL MODE), same as the code. I'm assuming you've added other code to put the computer into graphics mode and calls this function. Also that you've created a plotadot function? You can easily rework this function to fit your application!

  1. void ellipse( short cx, short cy, // center of ellipse
  2. short width, short height,
  3. char color, // 2 bytes ?
  4. short pad[2], // 4 bytes
  5. funcptr *plotadot 2 bytes
  6. );

Interesting. I don't see bx inuse, Most likely used by plotadot.

Too much heavy math for a DDA but kind of looks like a DDA. Will have to pick it apart to understand the math better!
Last edited by wildgoose; Jul 27th, 2009 at 11:15 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: How do I convert c code to asm

 
0
  #10
Jul 27th, 2009
This algorithm only deals with a fixed orientation of an ellipse but it uses the width for the stepping factor. Which means that if you set the width to half the height or thinner, you will see holes between pixels. Once you get this working you should modify the algorithm to use whichever is the longest, width or height as the stepping factor. It will replot some pixels but should be a consistent drawn line of pixels, and not a series of dotted pixels. And I'm guessing the reason the corners were painted separately is that the algorithm was not inclusive of its limits. [0,width) 0<=x<width not 0<=X<=width, thus they wouldn't have been plotted. But will have to analyze the math to know for sure!

So once you get this working, you should improve upon the algoithm to make your version your own. Since you did 'find' this version, you really should have posted credit to the original author, otherwise it really is plagiarism!
Last edited by wildgoose; Jul 27th, 2009 at 11:46 am.
Reply With Quote Quick reply to this message  
Reply

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




Views: 1104 | Replies: 16
Thread Tools Search this Thread



Tag cloud for Assembly
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC