{"id":5156,"date":"2026-02-04T08:05:09","date_gmt":"2026-02-04T14:05:09","guid":{"rendered":"https:\/\/ykim.synology.me\/wordpress\/?p=5156"},"modified":"2026-03-08T13:18:15","modified_gmt":"2026-03-08T18:18:15","slug":"hello-world-in-masm","status":"publish","type":"post","link":"https:\/\/ykim.synology.me\/wordpress\/hello-world-in-masm-5156\/","title":{"rendered":"Hello World in MASM"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">[Gemini]<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-theme-palette-12-color\">Microsoft Macro Assembler (MASM)<\/mark><\/strong> is the standard for writing assembly code on Windows. Modern MASM usually targets <strong>x64<\/strong> (64-bit), which uses a specific calling convention and 64-bit registers.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Below is a classic &#8220;Hello World&#8221; example written for <strong>MASM x64<\/strong>, utilizing the Windows API (<code>MessageBoxA<\/code>).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. MASM x64 &#8220;Hello World&#8221; Example<\/h2>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#24292e;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" style=\"color:#24292e;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>; --- External Windows API functions ---\nextern MessageBoxA : proc\nextern ExitProcess : proc\n\n.data\n    window_title db \"MASM x64\", 0\n    message      db \"Hello from Microsoft Assembler!\", 0\n\n.code\nmain proc\n    ; --- Shadow Space ---\n    ; Windows x64 ABI requires 32 bytes of \"shadow space\" on the stack \n    ; before calling a function.\n    sub rsp, 28h             \n\n    ; --- Setup Arguments for MessageBoxA ---\n    ; RCX = hWnd (NULL = 0)\n    ; RDX = lpText (Address of message)\n    ; R8  = lpCaption (Address of title)\n    ; R9  = uType (MB_OK = 0)\n    \n    xor rcx, rcx             ; Set RCX to 0\n    lea rdx, message         ; Load effective address of message\n    lea r8, window_title     ; Load effective address of title\n    xor r9, r9               ; Set R9 to 0\n    \n    call MessageBoxA         ; Call the API\n\n    ; --- Cleanup and Exit ---\n    xor rcx, rcx             ; Exit code 0\n    call ExitProcess\nmain endp\n\nend<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-light\" style=\"background-color: #fff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #6A737D\">; --- External Windows API functions ---<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6F42C1\">extern<\/span><span style=\"color: #24292E\"> MessageBoxA : proc<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6F42C1\">extern<\/span><span style=\"color: #24292E\"> ExitProcess : proc<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6F42C1\">.data<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    window_title <\/span><span style=\"color: #D73A49\">db<\/span><span style=\"color: #24292E\"> &quot;MASM x64&quot;, <\/span><span style=\"color: #005CC5\">0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    message      <\/span><span style=\"color: #D73A49\">db<\/span><span style=\"color: #24292E\"> &quot;Hello from Microsoft Assembler!&quot;, <\/span><span style=\"color: #005CC5\">0<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">.code<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">main proc<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    <\/span><span style=\"color: #6A737D\">; --- Shadow Space ---<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    <\/span><span style=\"color: #6A737D\">; Windows x64 ABI requires 32 bytes of &quot;shadow space&quot; on the stack <\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    <\/span><span style=\"color: #6A737D\">; before calling a function.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    <\/span><span style=\"color: #D73A49\">sub<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #005CC5\">rsp<\/span><span style=\"color: #24292E\">, <\/span><span style=\"color: #005CC5\">28h<\/span><span style=\"color: #24292E\">             <\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    <\/span><span style=\"color: #6A737D\">; --- Setup Arguments for MessageBoxA ---<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    <\/span><span style=\"color: #6A737D\">; RCX = hWnd (NULL = 0)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    <\/span><span style=\"color: #6A737D\">; RDX = lpText (Address of message)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    <\/span><span style=\"color: #6A737D\">; R8  = lpCaption (Address of title)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    <\/span><span style=\"color: #6A737D\">; R9  = uType (MB_OK = 0)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    <\/span><span style=\"color: #D73A49\">xor<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #005CC5\">rcx<\/span><span style=\"color: #24292E\">, <\/span><span style=\"color: #005CC5\">rcx<\/span><span style=\"color: #24292E\">             <\/span><span style=\"color: #6A737D\">; Set RCX to 0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    <\/span><span style=\"color: #D73A49\">lea<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #005CC5\">rdx<\/span><span style=\"color: #24292E\">, message         <\/span><span style=\"color: #6A737D\">; Load effective address of message<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    <\/span><span style=\"color: #D73A49\">lea<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #005CC5\">r8<\/span><span style=\"color: #24292E\">, window_title     <\/span><span style=\"color: #6A737D\">; Load effective address of title<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    <\/span><span style=\"color: #D73A49\">xor<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #005CC5\">r9<\/span><span style=\"color: #24292E\">, <\/span><span style=\"color: #005CC5\">r9<\/span><span style=\"color: #24292E\">               <\/span><span style=\"color: #6A737D\">; Set R9 to 0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    <\/span><span style=\"color: #D73A49\">call<\/span><span style=\"color: #24292E\"> MessageBoxA         <\/span><span style=\"color: #6A737D\">; Call the API<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    <\/span><span style=\"color: #6A737D\">; --- Cleanup and Exit ---<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    <\/span><span style=\"color: #D73A49\">xor<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #005CC5\">rcx<\/span><span style=\"color: #24292E\">, <\/span><span style=\"color: #005CC5\">rcx<\/span><span style=\"color: #24292E\">             <\/span><span style=\"color: #6A737D\">; Exit code 0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    <\/span><span style=\"color: #D73A49\">call<\/span><span style=\"color: #24292E\"> ExitProcess<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">main endp<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">end<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">2. Key Components Explained<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">The Registers (x64)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In the example above, we use 64-bit registers. The Windows calling convention uses specific registers for passing arguments:<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li><strong>RCX<\/strong>: 1st Argument<\/li>\n\n\n\n<li><strong>RDX<\/strong>: 2nd Argument<\/li>\n\n\n\n<li><strong>R8<\/strong>: 3rd Argument<\/li>\n\n\n\n<li><strong>R9<\/strong>: 4th Argument<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Shadow Space (<code>sub rsp, 28h<\/code>)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">One of the most common pitfalls in MASM for Windows is forgetting the <strong>Shadow Space<\/strong>. Even if a function takes fewer than 4 arguments, the caller must reserve 32 bytes ($20h$) plus an extra 8 bytes ($8h$) to maintain 16-byte stack alignment.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Directives<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>.data<\/code>: Defines the section for initialized data (strings, constants).<\/li>\n\n\n\n<li><code>.code<\/code>: Defines the section for executable instructions.<\/li>\n\n\n\n<li><code>proc<\/code> \/ <code>endp<\/code>: Marks the beginning and end of a procedure (function).<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">3. How to Assemble and Link<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To run this, you typically use the tools provided with Visual Studio (<strong>ml64.exe<\/strong> for the assembler and <strong>link.exe<\/strong> for the linker).<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li><strong>Assemble:<\/strong> <code>ml64.exe \/c my_file.asm<\/code><\/li>\n\n\n\n<li><strong>Link:<\/strong> <code>link.exe my_file.obj \/subsystem:windows \/defaultlib:user32.lib \/defaultlib:kernel32.lib \/entry:main<\/code><\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Would you like me to show you how to perform a simple mathematical calculation (like a Fibonacci sequence) using MASM instead?<\/strong><\/p>\n<div style='text-align:center' class='yasr-auto-insert-overall'><\/div><div style='text-align:center' class='yasr-auto-insert-visitor'><\/div>","protected":false},"excerpt":{"rendered":"<p>[Gemini] Microsoft Macro Assembler (MASM) is the standard for writing assembly code on Windows. Modern MASM usually targets x64 (64-bit), which uses a specific calling convention and 64-bit registers. Below is a classic &#8220;Hello World&#8221; example written for MASM x64, utilizing the Windows API (MessageBoxA). 1. MASM x64 &#8220;Hello World&#8221; Example 2. Key Components Explained&#8230;<\/p>\n","protected":false},"author":4,"featured_media":5191,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"_kadence_starter_templates_imported_post":false,"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","yasr_overall_rating":0,"yasr_post_is_review":"","yasr_auto_insert_disabled":"","yasr_review_type":"","fifu_image_url":"","fifu_image_alt":"","iawp_total_views":0,"footnotes":""},"categories":[10,364],"tags":[],"class_list":["post-5156","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software-slug","category-masm-slug"],"yasr_visitor_votes":{"stars_attributes":{"read_only":false,"span_bottom":false},"number_of_votes":0,"sum_votes":0},"jetpack_featured_media_url":"https:\/\/ykim.synology.me\/wordpress\/wp-content\/uploads\/2026\/02\/20260205-Microsoft-Macro-Assembler-5.1-book.png","_links":{"self":[{"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/posts\/5156","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/comments?post=5156"}],"version-history":[{"count":5,"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/posts\/5156\/revisions"}],"predecessor-version":[{"id":5791,"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/posts\/5156\/revisions\/5791"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/media\/5191"}],"wp:attachment":[{"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/media?parent=5156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/categories?post=5156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/tags?post=5156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}