The Art-Music, Literature and Linguistics Forum
April 20, 2024, 03:13:07 pm
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Here you may discover hundreds of little-known composers, hear thousands of long-forgotten compositions, contribute your own rare recordings, and discuss the Arts, Literature and Linguistics in an erudite and decorous atmosphere full of freedom and delight.
 
  Home Help Search Gallery Staff List Login Register  

64-bit Assembly Language - a simple example

Pages: [1]   Go Down
  Print  
Author Topic: 64-bit Assembly Language - a simple example  (Read 32789 times)
0 Members and 1 Guest are viewing this topic.
guest54
Guest
« on: December 18, 2011, 03:07:01 pm »

Here is a straightforward batch file which will assemble and link the programme. Of course the addresses of nasm and MS Visual Studio will have to be changed to suit your own installation; in my case Sample.asm is in a directory on drive e, and the batch file is run from there.

------------------------------------------------------------------------------------
c:\nasm\nasm -f win64 -Ox -Z Sample.err Sample.asm -l Sample.lst
if errorlevel 1 goto nasmfail
k:
cd \"Microsoft Visual Studio 10.0"\VC
call vcvarsall.bat x64
e:
link Sample.obj /subsystem:console /defaultlib:msvcrt.lib /defaultlib:kernel32.lib /entry:_start
if errorlevel 1 goto linkfail
pause
goto end

:nasmfail
:linkfail
@echo There were errors, so examine Sample.err
pause

:end
-----------------------------------------------------------------------------------

And a few notes:

1) GetCommandLineA is an example of how to call a Windows API. Any of the hundreds of Windows APIs may be called in a similar way. If you use arguments on the command line after the programme name, for some reason Windows inserts an extra space, such that there will be TWO spaces dividing the programme name from the first argument. Also, if you invoke the programme in some way other than from a command prompt window, Windows will return the command line in an altered format, inside quotation marks.

2) Register usage: Note that registers r13, r14, and r15 should be preserved by all functions that are called. More information about that, and about the prologue and epilogue, should be available here: http://msdn.microsoft.com/en-US/library/tawsa7cb%28v=VS.80%29.aspx

3) printf: The format specifiers %d %c %s and so on may all be used in exactly the same way as in a "c" programme. But escape sequences such as "\n" will not work. That is why the CR and LF (0x0d and 0x0a) are explicitly supplied where desired. Note too the important zero which must always terminate the strings to be printed.

4) To display quadwords in printf you may use %lld or %llf (where the "ll" is two lower-case "L"s, not the number 11).

5) In one of the printf statements I have inserted a 07 character, to demonstrate a proper beep, not the often inaudible and pathetic sound produced by MS Windows.

6) Of course if you are not invoking the programme from the command line you should call _getch again right at the end so that the window is not closed before the screen can be read!

7) The executable easily fits into one allocation unit of 4096 bytes, which is something like one-tenth of the space required by the equivalent thing written in "c".
Report Spam   Logged

Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by EzPortal
Bookmark this site! | Upgrade This Forum
SMF For Free - Create your own Forum


Powered by SMF | SMF © 2016, Simple Machines
Privacy Policy