Author Topic: Marcos for NASM Assembler?  (Read 16462 times)

nobody

  • Guest
Marcos for NASM Assembler?
« on: June 22, 2008, 02:57:28 PM »
Hello all,
I grew to LOVE assembler since I did a lot of ReverceEngeniering and I love my Assembly now.

I want to code some stuff in ASSEMBLER. I was thinking of Inline Assembler in Cpp but that is just sucky. Since its AT&T Asm. (I use MingW)
So, I wanted to do somestuff in Pure Assembler.
But I wonder now, are there some NASM Macros? For example for print, read, file_read, file_write and maybe some Network stuff (Receive / Send TCP Sockets).
Or do I have to write that myself?
Also, how can I Assemble to Win32 PE or to Linux ELF?
Since I only seem to be able to assemble to COM files..

Thanks,
Cheers,
Robin

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Marcos for NASM Assembler?
« Reply #1 on: June 23, 2008, 03:26:24 AM »
The best part of Nasm's macro system is that anybody can write their own macros, customized to their own taste. The worst part of it is that everybody does. :)

To me, "Pure Assembly" and "macros" would be mutually exclusive, but you can mix-and-match at any level you want, I guess. There are some macros in the "misc" directory in the source code, if you've got that. There are some macros in the "contributions" section of the download page here. One of the more recent "packages" for Nasm (including Nasm, which you've presumably got, a linker, resource compiler, macros, and examples) is the NasmX project:

http://www.asmcommunity.net/projects/nasmx/

Whether you use the IDE or not, there are extensive macros (nagoa+.inc) in the NaGoA project:

http://www.visual-assembler.pt.vu/

For Linux... and other 'nixish OSen... The "asmutils" package includes a complex macro system which will allow you to assemble the same source code for Linux, BSD, and a couple others:

http://asm.sourceforge.net/asmutils.html

Also for Linux, Jeff Owens has got a "package" - IDE, library, and tools more than macros. The AsmRef section is the most "asm friendly" documentation I've come across:

http://linuxasmtools.net/

I've written a set of macros that allows:

;--------------------
; nasm -f elf hw2uyane.asm
; ld -o hw2uyane hw2uyane.o

%include "yanetut.inc"
    putstring "Please tell me your name?  "
    getstring  79,name
    putstring "Hello, "
    putstring name
    putstring "! Welcome to Linux uhhh... Assembly!", linefeed
    putstring "BTW, the answer is "

mov ecx, 42
    mov ebx, 1
    add ebx, ecx
    ; HLLp, I can't read this part!!!

putnumber ebx
    putstring ", not "
    putnumber ecx
    putstring ". Deep Thought was off-by-one.", linefeed

depart victorious
;-----------------------

This is intended as a joke (goofin' on HLA), not something anyone would actually use... But many things are possible...  

Now... how do you assemble for Win32/ELF? The Friendly Manual might have some hints. :)

Essentially, specify the output format on the command line "-f win32" or "-f elf32" (a.k.a. "-f elf"). After that, RTFM for the linker...

Or... you can assemble in "-f bin" (the default), as you've been doing for .com files, but with "bits 32" specified in the source, and an appropriate executable header stuffed into it. Both "nagoa+.inc" and "elf.inc" from the asmutils package have macros to "simplify" doing this. Aside from "obsessively short", there isn't too much advantage to this - using a linker is more flexible... but it's an option...

If that's not enough to get you started... ask more! :)

Best,
Frank

nobody

  • Guest
Re: Marcos for NASM Assembler?
« Reply #2 on: June 23, 2008, 06:00:21 PM »
Thanks that Explained A LOT!
There is one think I dont know yet.
And that is, How to make a Macro? (feeling like a moron)
Do I have to do something like: %define Hello: mov ah, 0x09
At least that is what I did understand out of the Manual..

But, then again I have problems reading that kind of stuff...
Oh, I also heard that you could use Pre-made dlls Made in C or by a Thirth Party.
But, I have no idea how to find out how to talk to the dll file. And extract a Function form it.

Can somebody explain it to me or give it to me a Document that I should read?

I did try google. But I keep finding rubbish about .net C#.. so thats not what I want..

Thanks again,

Cheers,
Robin

nobody

  • Guest
Re: Marcos for NASM Assembler?
« Reply #3 on: June 27, 2008, 06:39:46 AM »
Hi Robin,

> Do I have to do something like: %define Hello: mov ah, 0x09
> At least that is what I did understand out of the Manual..

That would work, if it were not for the ':'. Valid characters for an identifier can be found here...



This works:

%define hello mov ah, 9

hello

Or we can do this:

%define to_ah(x) mov ah, (x)

to_ah (9)

Can have more than one parameter. If you've never written "int 21" instead of "int 21h", you will. Can be avoided...

%define dos_int int 21h

dos_int

Those are single line macros.  Multi-line macros are a bit different. The "1" indicates the number of parameters expected.

%macro do_dos_subfn 1
mov ah, %1
dos_int
%endmacro

do_dos_subfn 9
do_dos_subfn 4Ch

Nasm will warn, if you try to invoke the macro with the wrong number of parameters:

; do_dos_subfn -> warning/warning
; do_dos_subfn 1, 2 -> warning/error

Macros with the same name, but different number of parameters are distinct. Ranges of parameters can be specified. A default parameter can be specified, if one is missing. A "+" indicates that the last parameter is "greedy" - all following parameters are treated as part of the last one...

%macro dos_print 1+
section .data
%%the_text db %1
db '$'
section .text
mov dx, %%the_text
mov ah, 9
int 21h
%endmacro

dos_print "hello world", 13, 10, "goodbye", 13, 10

And so on...

> But, then again I have problems reading that kind of stuff...

Yeah, RTFM puts me to sleep (thus the delayed reply). Nonetheless, I recommend it. I haven't gotten too far working out Nasm's macro system by trial and error. :)

> Oh, I also heard that you could use Pre-made dlls Made in C or by a Thirth
> Party.
> But, I have no idea how to find out how to talk to the dll file. And extract
> a Function form it.
>
> Can somebody explain it to me or give it to me a Document that I should read?

The OS (and the linker) take care of most of it for you. In a Windows program, you don't have much choice but to use a third-party .dll, since that's how the API is implemented. Any example should do - the win32-nasm-users group on Yahoo has a bunch of examples in the "files" section. Mostly a case of declaring the function "global" and linking against the right library. I don't "do Windows" so I can't give you much information. MS has a huge help system on line...

> I did try google. But I keep finding rubbish about .net C#.. so thats not what
> I want..

Where will it end, C^^? Curiously, some of these examples *are* helpful, since we're just calling the API anyway. Watch what APIs they call, and with what parameters, and then just do it in asm! :)

Best,
Frank