{"id":1324,"date":"2022-08-30T15:15:35","date_gmt":"2022-08-30T15:15:35","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/10\/calling-assembly-function-from-c-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:15:35","modified_gmt":"2022-08-30T15:15:35","slug":"calling-assembly-function-from-c-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/calling-assembly-function-from-c-collection-of-common-programming-errors\/","title":{"rendered":"calling assembly function from c-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m trying to call an assembly function from c,but i keep getting errors.<\/p>\n<pre><code>    .text\n    .globl integrate\n    .type integrate, @function\nintegrate:\n    push %ebp\n    mov %esp, %ebp\n    mov $0,%edi\nstart_loop:                \n    cmp %edi,1024           \n    je loop_exit\n    mov 8(%ebp),%eax          \n    mov 12(%ebp),%ecx          \n    sub %eax,%ecx              \n    add %edi,%ecx\n    incl %edi                \n    jmp start_loop             \nloop_exit:                 \n    movl %ebp, %esp\n    popl %ebp\n    ret   \n<\/code><\/pre>\n<p>This is my assembly function,file called integrate.s.<\/p>\n<pre><code>#include \n\nextern int integrate(int from,int to);\n\nvoid main()\n{\n    printf(\"%d\",integrate(1,10));\n}\n<\/code><\/pre>\n<p>Heres my c code.<\/p>\n<pre><code>function.c:5:6: warning: return type of \u2018main\u2019 is not \u2018int\u2019 [-Wmain]\n\/tmp\/cciR63og.o: In function `main':\nfunction.c:(.text+0x19): undefined reference to `integrate'\ncollect2: ld returned 1 exit status\n<\/code><\/pre>\n<p>Whenever i try to compile my code with gcc -Wall function.c -o function,it gives the &#8216;undefined reference to integrate&#8217; error.I also tried adding link to the integrate.s file from c,like<\/p>\n<pre><code>#include\n<\/code><\/pre>\n<p>but it didnt work as well.Btw what assembly code is doing is not important,for now im just trying to call the function from c successfully.Can anyone help me about solving this problem ?<\/p>\n<ol>\n<li>\n<blockquote>\n<p>warning: return type of \u2018main\u2019 is not \u2018int\u2019<\/p>\n<\/blockquote>\n<p>means that the return type of \u2018main\u2019 is not \u2018int\u2019&#8230; Change it to <code>int<\/code>, then:<\/p>\n<pre><code>int main()\n{\n}\n<\/code><\/pre>\n<p>Also, to solve the linker error, invoke GCC as<\/p>\n<pre><code>gcc -o myprog main.c integrate.s\n<\/code><\/pre>\n<p>and that should work.<\/p>\n<\/li>\n<li>\n<p>I see the following problems with the code:<\/p>\n<ul>\n<li>calling convention mandates you must preserve the value of <code>edi<\/code><\/li>\n<li><code>cmp %edi,1024<\/code> is using <code>1024<\/code> as address and will probably fault. You want <code>cmp $1024,%edi<\/code> for comparing with an immediate number<\/li>\n<li>you are reloading <code>eax<\/code> and <code>ecx<\/code> from the arguments each iteration so the calculation you perform has no effect<\/li>\n<li>you don&#8217;t seem to put any sensible return value into <code>eax<\/code> (it will return the value of <code>from<\/code> that was passed in)<\/li>\n<\/ul>\n<p>The first two points apply even if &#8220;what assembly code is doing is not important&#8221;.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-10 00:16:56. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m trying to call an assembly function from c,but i keep getting errors. .text .globl integrate .type integrate, @function integrate: push %ebp mov %esp, %ebp mov $0,%edi start_loop: cmp %edi,1024 je loop_exit mov 8(%ebp),%eax mov 12(%ebp),%ecx sub %eax,%ecx add %edi,%ecx incl %edi jmp start_loop loop_exit: movl %ebp, %esp popl %ebp ret This is my assembly [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1324","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1324","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1324"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1324\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}