{"id":2831,"date":"2020-10-06T22:42:38","date_gmt":"2020-10-06T19:42:38","guid":{"rendered":"http:\/\/demensdeum.com\/blog\/?p=2831"},"modified":"2024-12-16T22:32:25","modified_gmt":"2024-12-16T19:32:25","slug":"x86_64-assembler-c-one-love","status":"publish","type":"post","link":"https:\/\/demensdeum.com\/blog\/hi\/2020\/10\/06\/x86_64-assembler-c-one-love\/","title":{"rendered":"x86_64 Assembler + C = One Love"},"content":{"rendered":"<p>In this note I will describe the process of calling C functions from assembler.<br \/>Let&#8217;s try calling printf(\u201cHello World!\\n\u201d); and exit(0);<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>    message: db \"Hello, world!\", 10, 0\n\nsection .text\n    extern printf\n    extern exit\n    global main\n\nmain:\n    xor\trax, rax\n    mov\trdi, message    \n    call printf\n    xor rdi, rdi\n    call exit\n<\/code><\/pre>\n<\/div>\n<p>Everything is much simpler than it seems, in the .rodata section we will describe static data, in this case the string \u201cHello, world!\u201d, 10 is the newline character, also do not forget to zero it.<\/p>\n<p>In the code section, we will declare external functions printf, exit of the stdio, stdlib libraries, and also declare the entry function main:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>    extern printf\n    extern exit\n    global main\n<\/code><\/pre>\n<\/div>\n<p>We pass 0 to the return register from the rax function, you can use mov rax, 0; but to speed it up, use xor rax, rax; Next, we pass a pointer to a string to the first argument:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code><p>\u0414\u0430\u043b\u0435\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u0432\u043d\u0435\u0448\u043d\u044e\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0421\u0438 printf:<\/p>\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>    xor\trax, rax\n    mov\trdi, message    \n    call printf\n    xor rdi, rdi\n    call exit\n<\/code><\/pre>\n<\/div>\n<p>By analogy, we pass 0 to the first argument and call exit:<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-unknown\" data-lang=\"unknown\"><code>    call exit\n<\/code><\/pre>\n<\/div>\n<p>As Americans say:<br \/>Who doesn't listen to anyone<br \/>That pilaf is eaten by @ <a href=\"https:\/\/twitter.com\/comrade_wolgast\/status\/1313561809337683970\" target=\"_blank\" rel=\"noopener noreferrer\">Alexander Pelevin<\/a><\/p>\n<h3>Sources<\/h3>\n<p><a href=\"https:\/\/www.devdungeon.com\/content\/how-mix-c-and-assembly\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/www.devdungeon. com\/content\/how-mix-c-and-assembly<\/a><br \/><a href=\"https:\/\/nekosecurity.com\/x86-64-assembly\/part-3-nasm-anatomy-syscall-passing-argument\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/nekosecurity.com\/x86-64-assembly\/part-3-nasm-anatomy-syscall-passing-argument<\/a><br \/>\n<a href=\"https:\/\/www.cs.uaf.edu\/2017\/fall\/cs301\/reference\/x86_64.html\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/www.cs.uaf.edu\/2017\/fall\/cs301\/reference\/x86_64.html<\/a><\/p>\n<h3>Source code<\/h3>\n<p><a href=\"https:\/\/gitlab.com\/demensdeum\/assembly-playground\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/gitlab.com\/demensdeum\/assembly-playground<\/a <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this note I will describe the process of calling C functions from assembler.Let&#8217;s try calling printf(\u201cHello World!\\n\u201d); and exit(0); message: db &#8220;Hello, world!&#8221;, 10, 0 section .text extern printf extern exit global main main: xor rax, rax mov rdi, message call printf xor rdi, rdi call exit Everything is much simpler than it seems,<a class=\"more-link\" href=\"https:\/\/demensdeum.com\/blog\/hi\/2020\/10\/06\/x86_64-assembler-c-one-love\/\">Continue reading <span class=\"screen-reader-text\">&#8220;x86_64 Assembler + C = One Love&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[61,52],"tags":[165,166,164],"class_list":["post-2831","post","type-post","status-publish","format-standard","hentry","category-techie","category-tutorials","tag-asm","tag-assembly","tag-nasm","entry"],"translation":{"provider":"WPGlobus","version":"3.0.2","language":"hi","enabled_languages":["en","ru","zh","de","fr","ja","pt","hi"],"languages":{"en":{"title":true,"content":true,"excerpt":false},"ru":{"title":true,"content":true,"excerpt":false},"zh":{"title":true,"content":true,"excerpt":false},"de":{"title":true,"content":true,"excerpt":false},"fr":{"title":true,"content":true,"excerpt":false},"ja":{"title":true,"content":true,"excerpt":false},"pt":{"title":true,"content":true,"excerpt":false},"hi":{"title":false,"content":false,"excerpt":false}}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/posts\/2831","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/comments?post=2831"}],"version-history":[{"count":16,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/posts\/2831\/revisions"}],"predecessor-version":[{"id":3905,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/posts\/2831\/revisions\/3905"}],"wp:attachment":[{"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/media?parent=2831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/categories?post=2831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/demensdeum.com\/blog\/hi\/wp-json\/wp\/v2\/tags?post=2831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}