Hello world program is the legacy way of starting the process of learning any programming languages. In this post, I will try to write how to write a Hello world program in both x86 and x64 ASM. There are three sections in an assembly program namely data, bss and text.
Contents
- Intro
- Code
- Assembling and Running
- Miscellaneous
- Question
1. Intro
This isn't a question per se (though there is one at the bottom) but a HelloWorld app for people on StackOverflow to experiment with.
When I was first trying programing in MASM I tried to find a working HelloWorld application that used the WIN32 API calls (so not linking to C libraries) but couldn't find one (in MASM Syntax). So now that I have some experience I have written one for others wanting to learn assembly to fiddle with.
2. Code
3. Assembling and Running
I assume you have MASM32 installed in your C:MASM32 directory.
- If you do not have MASM installedplease go tohttp://masm32.com/install.htmand follow the instructions.
- If MASM32 is installed in a differentdirectory please change theinstructions accordingly.
- Open up the MASM32 Editor (QEditor) by either clicking on the Desktop Shortcut or if there is no shortcut go to C:MASM32 and double click qeditor.exe
- Copy the code in the code section (only the text that has a gray background) and paste it into the MASM32 Editor (QEditor) and save it.
- After saving the code click the Project menu and select Console Assemble and Link (NOT Assemble and Link (see Miscellaneous))
- Go to START and click Run, then type cmd and hit ENTER a black box with gray text should appear
- Navigate, using Explorer, to where you saved the code in step 3. There should now be a file with the same name as your source file (step 3) but be an exe. Drag and drop the exe file from the Explorer window to the cmd box (step 4 the black box)
- Select the black box and hit ENTER, the text 'Hello World!' should appear.
4. Miscellaneous
Why do I have to click Console Assemble and Run and not just Assemble and Run in the Project Menu?
The reason you have to click Console Assemble and Run is because there are two types of applications, there are GUIs and then there are text base console (DOS) applications. The Hello Would Application is a text based one and so when assembled must be have the settings a console based app would and not a GUI.
See the third paragraph under Remarks in this link for a more detailed explanation.
5. Question
Ok now the question, does anyone here see any problems, errors, or general issues with this code or have any suggestions
rkhb11.7k77 gold badges2323 silver badges4242 bronze badges
Zimm3rZimm3r2,30622 gold badges2525 silver badges4343 bronze badges
3 Answers
The program is fine. It is indeed 'Hello World' version of Win32. However, remember its a console program. In Win32, you will be mostly dealing with Windows, Dialog Boxes and very less with Console (Incase, you want to deal specifically with console, thats another story).
If you want to lean Win32 Assembly, I strongly suggest you to look at Iczelion Tutorials.
Here is the 'Hello World' to start with his tutorials:
Mike Koch1,49522 gold badges1515 silver badges2222 bronze badges
Madhur AhujaMadhur Ahuja16.4k1010 gold badges5555 silver badges100100 bronze badges
This sample code is simpler and easy to understand
justyyjustyy3,00511 gold badge2424 silver badges5757 bronze badges
StdOut is a console function
You can use MessageBox function...
Mario AugustoMario Augusto
Not the answer you're looking for? Browse other questions tagged winapiassemblymasmmasm32 or ask your own question.
Bart Massey
I've scratch-written an assembly version of 'hello world'in x86-64 assembly to see what's going on there. You cansimply say 'make' in this directory to build it, andthen './hello' to run it. It should do the obvious thing,and then exit with status 0.
The assembly code
The assembly code here has several jobs:
- Define the 'hello world' string needed by the program.
- Define the entry point '_start' at which the dynamicloader will start the program.
- Set up
%rbp
so that the C calling conventions aresatisfied. We needn't save the old %rbp here, becausewe will never use it. - Set up the arguments to 'printf' and call it.
- Check the return value to see if there was an errorand call the 'abort' system call if so. (It shouldnever return, so we just jump to it.)
- Call the exit system call to end program execution.
We put the assembly code in file with extension '.S' ratherthan '.s'. The assembler is fine with this, and it means wewill never accidentally overwrite or remove our hand-writtenassembly when working with the C compiler.
C calling conventions for x86-64 Linux
The C compiler expects that functions will be calledwith
%rsp
pointing at top of stack, %rbp
pointingat the location where the return value will be held,and the arguments passed as follows:Floating point arguments are special, and are passed in
%xmm0
..%xmm7
.Additional arguments are passed on the stack, pushed inorder. structs
are passed on the stack, in general,although there are register-passing conventions forstructs. See elsewhere for details.Registers
%rbp
, %rbx
, and %r12
..%r15
arecallee-saves: all others are caller-saves.To make a system call, one first puts the system call numberin
%rax
. Then the syscall arguments are passed as follows:There are at most 6 arguments to any system call: all mustbe either pointers or integers. The resultis returned in
%rax
: if the result is in the range-4095..-1 it is an error number.The Linux x86-64 syscall numbers can be found in
/usr/include/x86_64-linux-gnu/asm/unistd_64.h
.Assembling
There is nothing terribly special about the assemblycommand. The only thing of note is '--gstabs+', which adds adebugging section to the object file such that
gdb
knowsthat it is assembly code and makes it easier to work with.Linking
Most of the linker stuff is straightforward. For dynamicloading, the executable needs to know where the sharedlibraries will live and what dynamic loader to use to getthem.
Note that we have deliberately ignored the C setup routinesthat arrange to have
main
called. This is a bit iffy,since this code potentially also initializes some C librarystuff. If one chose, one could modify our code to builda normal main
function, and then link withto get things set up. In this case, we could justreturn normally instead of exiting.
References
License
This work is licensed under the 'MIT License'. Please seethe file
COPYING
in this distribution for license details.