{"id":2580,"date":"2022-08-30T15:26:03","date_gmt":"2022-08-30T15:26:03","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/02\/04\/how-to-create-raw-binary-from-assembler-as-command-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:26:03","modified_gmt":"2022-08-30T15:26:03","slug":"how-to-create-raw-binary-from-assembler-as-command-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/how-to-create-raw-binary-from-assembler-as-command-collection-of-common-programming-errors\/","title":{"rendered":"How to create raw binary from assembler &ldquo;as&rdquo; command-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li>\n<p>Yes, you can use otool after assembling and linking the file to extract the code. <code>otool -t<\/code> will extract the text (code) segment as ASCII hex. For example:<\/p>\n<pre><code>$ cat hello.S\n    .cstring\nLC0:\n    .ascii \"Hello world!\\12\\0\"\n    .text\n    .align 4,0x90\n.globl _main\n_main:\n    pushl   %ebp\n    movl    %esp, %ebp\n    subl    $24, %esp\n    movl    $LC0, (%esp)\n    call    _puts\n    xorl    %eax, %eax\n    leave\n    ret\n    .subsections_via_symbols\n$ gcc -m32 hello.S -o hello\n$ otool -t hello\nhello:\n(__TEXT,__text) section\n00001f20 6a 00 89 e5 83 e4 f0 83 ec 10 8b 5d 04 89 1c 24 \n00001f30 8d 4d 08 89 4c 24 04 83 c3 01 c1 e3 02 01 cb 89 \n00001f40 5c 24 08 8b 03 83 c3 04 85 c0 75 f7 89 5c 24 0c \n00001f50 e8 0b 00 00 00 89 04 24 e8 19 00 00 00 f4 90 90 \n00001f60 55 89 e5 83 ec 18 c7 04 24 a4 1f 00 00 e8 0a 00 \n00001f70 00 00 31 c0 c9 c3 \n$ \n<\/code><\/pre>\n<p>Note that <code>main<\/code> actually starts at <code>00001f60<\/code>:<\/p>\n<pre><code>$ otool -tv hello\nhello:\n(__TEXT,__text) section\nstart:\n00001f20    pushl   $0x00\n00001f22    movl    %esp,%ebp\n00001f24    andl    $0xf0,%esp\n00001f27    subl    $0x10,%esp\n00001f2a    movl    0x04(%ebp),%ebx\n00001f2d    movl    %ebx,(%esp)\n00001f30    leal    0x08(%ebp),%ecx\n00001f33    movl    %ecx,0x04(%esp)\n00001f37    addl    $0x01,%ebx\n00001f3a    shll    $0x02,%ebx\n00001f3d    addl    %ecx,%ebx\n00001f3f    movl    %ebx,0x08(%esp)\n00001f43    movl    (%ebx),%eax\n00001f45    addl    $0x04,%ebx\n00001f48    testl   %eax,%eax\n00001f4a    jne 0x00001f43\n00001f4c    movl    %ebx,0x0c(%esp)\n00001f50    calll   0x00001f60\n00001f55    movl    %eax,(%esp)\n00001f58    calll   0x00001f76\n00001f5d    hlt\n00001f5e    nop\n00001f5f    nop\n_main:\n00001f60    pushl   %ebp\n00001f61    movl    %esp,%ebp\n00001f63    subl    $0x18,%esp\n00001f66    movl    $0x00001fa4,(%esp)\n00001f6d    calll   0x00001f7c\n00001f72    xorl    %eax,%eax\n00001f74    leave\n00001f75    ret\n$\n<\/code><\/pre>\n<\/li>\n<li>\n<p>No, <code>as<\/code> assembler translates source code into object files, like most, if not all, assemblers do. Then you have to use linker in order to link one or more object files, possible with runtime libraries, in order to create an executable file.<\/p>\n<\/li>\n<\/ul>\n<p id=\"rop\"><small>Originally posted 2014-02-04 01:42:44. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Yes, you can use otool after assembling and linking the file to extract the code. otool -t will extract the text (code) segment as ASCII hex. For example: $ cat hello.S .cstring LC0: .ascii &#8220;Hello world!\\12\\0&#8221; .text .align 4,0&#215;90 .globl _main _main: pushl %ebp movl %esp, %ebp subl $24, %esp movl $LC0, (%esp) call _puts [&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-2580","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2580","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=2580"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2580\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2580"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2580"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2580"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}