Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write a "Hello World" app in assembly language? [duplicate]

Possible Duplicate:
how to write hello world in assembler under windows?

I've often heard of applications written using the language of the gods, assembly language. I've never tried though, and I don't even have a clue how to do it.

If I wanted to dabble, how would I go about doing it? I know absolutely nothing about what is required, although presumably some kind of compiler and Notepad.

Just purely out of curiousity, what would I need to write a "Hello World!" application?

Edit to add, I am running Windows 7 64bit

Edit to add, I wonder if there is an assembly language plugin for Visual Studio?

like image 244
NibblyPig Avatar asked Apr 16 '10 14:04

NibblyPig


People also ask

What is assembly language example?

Example of assembly language"EAX," "EBX" and "ECX" are the variables. The first line of code loads "3" into the register "eax." The second line of code loads "4" into the register "ebx." Finally, the last line of code adds "eax" and "ebx" and stores the result of the addition, which is seven, in "ecx."

Which assembly language is the best?

The most commonly used assembly languages include ARM, MIPS, and x86.

Which assembly language should I learn first?

We start off with Inline Assembly, and that is Assembly code wrote inside C/C++. I choose to do so because I want to start simple and then move on to real.


2 Answers

For this, you gain nothing by writing 64-bit code -- you might as well stick to 32-bit code.

If you want output to a MessageBox, it could look like this:

.386
.MODEL flat, stdcall

MessageBoxA PROTO near32 stdcall, window:dword, text:near32,
        windowtitle:near32, style:dword

.stack 8192

.data
message db "Hello World!", 0
windowtitle   db "Win32 Hello World.", 0

.code
main proc
        invoke MessageBoxA, 0, near32 ptr message, near32 ptr windowtitle, 0
        ret
main endp
        end main

If you want output to the console, it's (strangely enough) a bit more complex:

.386
.MODEL flat, stdcall

getstdout = -11

WriteFile PROTO NEAR32 stdcall,     \
        handle:dword,                   \
    buffer:ptr byte,        \
        bytes:dword,                    \
        written: ptr dword,             \
        overlapped: ptr byte

GetStdHandle PROTO NEAR32, device:dword

ExitProcess PROTO NEAR32, exitcode:dword

.stack 8192

.data
message db "Hello World!"
msg_size equ $ - offset message

.data?
written  dd ?

.code
main proc   
    invoke GetStdHandle, getstdout
    invoke WriteFile,                   \
           eax,                         \
           offset message,              \
           msg_size,                    \
           offset written,              \
           0

    invoke ExitProcess, 0
main endp
        end main

In theory, moving to 64 bit code doesn't make a lot of difference -- for example, you can use the same functions in both. In reality, it's a bit painful, because the calling convention for 64-bit code is somewhat complex, and you can't use MASM's invoke for 64-bit code. Working code wouldn't be a whole lot more complex, but getting the code working probably would be a bit more work. The general idea is that for 64-bit code, you allocate space on the stack for all your parameters, but the first N parameters that are small enough to fit, go in registers.

like image 184
Jerry Coffin Avatar answered Oct 18 '22 14:10

Jerry Coffin


Have a look at WinAsm.

like image 25
kgiannakakis Avatar answered Oct 18 '22 14:10

kgiannakakis