<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>asm: calling C procs</title>
		<link>http://www.allegro.cc/forums/view/465704</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Thu, 03 Mar 2005 07:21:54 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well, this is a homework question, but I need help!</p><p>What I need to do is to read 1024 bytes from &quot;filename.txt&quot; using fopen, fread, and fclose using ASM.</p><p>in C:
</p><div class="source-code snippet"><div class="inner"><pre><span class="p">#include &lt;stdio.h&gt;</span>
<span class="k1">int</span> main<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span>
  <span class="k1">char</span> buffer<span class="k2">[</span><span class="n">1024</span><span class="k2">]</span><span class="k2">;</span>
  FILE<span class="k3">*</span> file <span class="k3">=</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_337.html" target="_blank">fopen</a><span class="k2">(</span><span class="s">"filename.txt"</span>,<span class="s">"r"</span><span class="k2">)</span><span class="k2">;</span>
  <a href="http://www.delorie.com/djgpp/doc/libc/libc_349.html" target="_blank">fread</a><span class="k2">(</span>file,buffer,<span class="n">1024</span><span class="k2">)</span><span class="k2">;</span>
  <a href="http://www.delorie.com/djgpp/doc/libc/libc_308.html" target="_blank">fclose</a><span class="k2">(</span>file<span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>

Sure, I can use -S and take a look to the .s but it&#39;s very difficult to read... (for me)</p><p>So, these are my ideas (ASM):
</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td><span class="k1">extern</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_337.html" target="_blank">fopen</a></td></tr><tr><td class="number">2</td><td><span class="k1">extern</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_349.html" target="_blank">fread</a></td></tr><tr><td class="number">3</td><td><span class="k1">extern</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_308.html" target="_blank">fclose</a></td></tr><tr><td class="number">4</td><td>&#160;</td></tr><tr><td class="number">5</td><td><span class="c">// data segment</span></td></tr><tr><td class="number">6</td><td>buffer db <span class="n">1024</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_281.html" target="_blank">dup</a><span class="k2">(</span><span class="n">0</span><span class="k2">)</span></td></tr><tr><td class="number">7</td><td>&#160;</td></tr><tr><td class="number">8</td><td><span class="c">// segment</span></td></tr><tr><td class="number">9</td><td>push eax             <span class="k2">;</span>save eax</td></tr><tr><td class="number">10</td><td><span class="k1">xor</span>  eax, eax        <span class="k2">;</span>eax <span class="k3">=</span> <span class="n">0</span></td></tr><tr><td class="number">11</td><td>push <span class="s">'filename.txt'</span>  <span class="k2">;</span>arg1</td></tr><tr><td class="number">12</td><td>push <span class="s">'r'</span>             <span class="k2">;</span>arg2</td></tr><tr><td class="number">13</td><td>call <a href="http://www.delorie.com/djgpp/doc/libc/libc_337.html" target="_blank">fopen</a></td></tr><tr><td class="number">14</td><td>pop  eax             <span class="k2">;</span>eax <span class="k3">=</span> FILE<span class="k3">*</span></td></tr><tr><td class="number">15</td><td>push eax             <span class="k2">;</span>arg1</td></tr><tr><td class="number">16</td><td>push offset buffer   <span class="k2">;</span>arg2</td></tr><tr><td class="number">17</td><td>push length buffer   <span class="k2">;</span>arg3</td></tr><tr><td class="number">18</td><td>call <a href="http://www.delorie.com/djgpp/doc/libc/libc_349.html" target="_blank">fread</a></td></tr><tr><td class="number">19</td><td>push eax             <span class="k2">;</span>arg1</td></tr><tr><td class="number">20</td><td>call <a href="http://www.delorie.com/djgpp/doc/libc/libc_308.html" target="_blank">fclose</a></td></tr><tr><td class="number">21</td><td>pop  eax             <span class="k2">;</span>restore eax</td></tr></tbody></table></div></div><p>

I know it sucks, but anyone can help me?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (lucaz)</author>
		<pubDate>Tue, 01 Mar 2005 06:12:21 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I havent used x86 asm in a while, but I dont think you can just push strings onto the stack. You have to have a data section or something and give them labels, then move the labels into registers and push the registers onto the stack. For easy reference on how to do this, just use gcc -S <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (kazzmir)</author>
		<pubDate>Tue, 01 Mar 2005 06:15:19 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>yes is true.<br />The -S flag include a lot of stuff and &quot;advanced asm&quot; I don&#39;t understand it <img src="http://www.allegro.cc/forums/smileys/sad.gif" alt=":(" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (lucaz)</author>
		<pubDate>Tue, 01 Mar 2005 06:16:48 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>C functions are prepended with a &#39;_&#39;, so the correct function names are _fopen, _fread and _fclose.</p><p>[edit]<br />You can&#39;t push strings or characters into the stack. You have to create a string in the data segment with the string contents and push the address.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Oscar Giner)</author>
		<pubDate>Tue, 01 Mar 2005 07:31:39 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Ok, if I change the string stuff, this is a good sketch?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (lucaz)</author>
		<pubDate>Tue, 01 Mar 2005 07:40:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>C functions are prepended with a &#39;_&#39;</p></div></div><p>
Not necesarilly. They aren&#39;t in Linux, but they are in some versions of MinGW.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Tue, 01 Mar 2005 07:42:58 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The xor is useless, you don&#39;t want to set eax to 0 for anything.</p><p>Funaction arguments are pushed in reverse order, from right to left.</p><p>Remove the pop after the call to fopen.</p><p>I don&#39;t know what offset and length are. I&#39;ve never seen them.</p><p>But if it&#39;s homework... what is your teacher doing? Not teaching you, certainly <img src="http://www.allegro.cc/forums/smileys/tongue.gif" alt=":P" /></p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
but they are in some versions of MinGW.
</p></div></div><p>
They&#39;re in all versions of MingW I&#39;ve tryed. Also in MSVC and in the old TurboC. I though that was part of the standard. I don&#39;t know why this is not true in linux.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Oscar Giner)</author>
		<pubDate>Tue, 01 Mar 2005 07:48:42 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>They&#39;re in all versions of MingW I&#39;ve tryed.</p></div></div><p>
Same here, but APEG prepends its asm functions with _ in MinGW and the C code refuses to recognize them for some people. Even removing the _ apparently doesn&#39;t do anything. :/</p><p>As for the xor, I see a lot of assembly code do that to set a register to 0. I just assumed it was more CPU efficient.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Tue, 01 Mar 2005 07:54:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
As for the xor, I see a lot of assembly code do that to set a register to 0. I just assumed it was more CPU efficient.
</p></div></div><p>
Yes, xoring is faster than a &#39;mov eax, 0&#39;, but he doesn&#39;t need to set eax to 0 in the first place <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /></p><p>[edit]<br />One more thing. This may depend on the assembler you use, but I think the keyword for importing symbols is &#39;public&#39;, not &#39;extern&#39;.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Oscar Giner)</author>
		<pubDate>Tue, 01 Mar 2005 07:56:35 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>cookies!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (lucaz)</author>
		<pubDate>Tue, 01 Mar 2005 08:33:12 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>okay, then, the code approximated is this: ?</p><div class="source-code"><div class="toolbar"></div><div class="inner"><table width="100%"><tbody><tr><td class="number">1</td><td>&#160;</td></tr><tr><td class="number">2</td><td><span class="k1">extern</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_337.html" target="_blank">fopen</a> <span class="c">// or public fopen</span></td></tr><tr><td class="number">3</td><td><span class="k1">extern</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_349.html" target="_blank">fread</a></td></tr><tr><td class="number">4</td><td><span class="k1">extern</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_308.html" target="_blank">fclose</a></td></tr><tr><td class="number">5</td><td>&#160;</td></tr><tr><td class="number">6</td><td><span class="c">// data segment</span></td></tr><tr><td class="number">7</td><td>buffer db <span class="n">1024</span> <a href="http://www.delorie.com/djgpp/doc/libc/libc_281.html" target="_blank">dup</a><span class="k2">(</span><span class="n">0</span><span class="k2">)</span></td></tr><tr><td class="number">8</td><td>&#160;</td></tr><tr><td class="number">9</td><td><span class="c">// segment</span></td></tr><tr><td class="number">10</td><td>push eax             <span class="k2">;</span>save eax</td></tr><tr><td class="number">11</td><td><span class="k1">xor</span>  eax, eax        <span class="k2">;</span>eax <span class="k3">=</span> <span class="n">0</span></td></tr><tr><td class="number">12</td><td>push <span class="s">'r'</span>             <span class="k2">;</span>arg2</td></tr><tr><td class="number">13</td><td>push <span class="s">'filename.txt'</span>  <span class="k2">;</span>arg1</td></tr><tr><td class="number">14</td><td>call <a href="http://www.delorie.com/djgpp/doc/libc/libc_337.html" target="_blank">fopen</a></td></tr><tr><td class="number">15</td><td><span class="c">// pop  eax             ;eax = FILE*  -- I don't understand because the pop is not necessary</span></td></tr><tr><td class="number">16</td><td>&#160;</td></tr><tr><td class="number">17</td><td>push length buffer   <span class="k2">;</span>arg3</td></tr><tr><td class="number">18</td><td>push offset buffer   <span class="k2">;</span>arg2</td></tr><tr><td class="number">19</td><td>push eax             <span class="k2">;</span>arg1</td></tr><tr><td class="number">20</td><td>call <a href="http://www.delorie.com/djgpp/doc/libc/libc_349.html" target="_blank">fread</a></td></tr><tr><td class="number">21</td><td>push eax             <span class="k2">;</span>arg1</td></tr><tr><td class="number">22</td><td>call <a href="http://www.delorie.com/djgpp/doc/libc/libc_308.html" target="_blank">fclose</a></td></tr><tr><td class="number">23</td><td>pop  eax             <span class="k2">;</span>restore eax</td></tr></tbody></table></div></div><p>
// When use the buffer??</p><p>Lukas
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Lucas pepe)</author>
		<pubDate>Tue, 01 Mar 2005 20:33:14 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You&#39;ve created a 2nd account?</p><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
I don&#39;t understand because the pop is not necessary
</p></div></div><p>
The return value is stored in eax, not in the stack. So fopen will put FILE* in eax.</p><p>Have you tried that code and see if it works? I think that would be much easier.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Oscar Giner)</author>
		<pubDate>Tue, 01 Mar 2005 20:55:30 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>You should also check eax is different from NULL after opening the file.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ReyBrujo)</author>
		<pubDate>Tue, 01 Mar 2005 20:58:56 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>
You&#39;ve created a 2nd account?
</p></div></div><p>
No, he&#39;s a friend (lukas) Im lucaz.<br />We&#39;ll fix the pops.</p><p>I&#39;ll try the code now.</p><p>EDIT:<br />welcome back reybrujo!, donde estabas?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (lucaz)</author>
		<pubDate>Tue, 01 Mar 2005 21:19:26 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Got a user interface course on the last week of January, and then spent most of February doing interface testings and user testings. I missed the hardcore of C programming, though <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ReyBrujo)</author>
		<pubDate>Tue, 01 Mar 2005 21:31:19 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>Not necesarilly. They aren&#39;t in Linux, but they are in some versions of MinGW.</p></div></div><p>Its the object file format. elf doesn&#39;t require it, but PE/COFF does.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Wed, 02 Mar 2005 08:16:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>BTW, you can push strings on the stack. You just can&#39;t use them like that. You have to pass esp as the argument to the function, since the function wants a pointer.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Billybob)</author>
		<pubDate>Thu, 03 Mar 2005 00:19:17 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks, I get a 7 <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (lucaz)</author>
		<pubDate>Thu, 03 Mar 2005 07:21:54 +0000</pubDate>
	</item>
</rss>
