Author Topic: Few very newbie question (NASM)  (Read 18074 times)

Aleko Popov

  • Guest
Few very newbie question (NASM)
« on: June 11, 2009, 05:02:33 PM »
Hello people,
Ok, i should first say that i am ABSOLUTELY new to this topic - assemblers. I read all kind of forums and websites about NASM and assemblers. But i couldn't find anywhere simple info about it. I did some C++ and i am, let's say, 'familiar' with C programing. That's why i need help to understand NASM. Let's say - in C++ i define integer like this: "int a;"
 - float:"double a;"
 - loops: "for(i=1;i<10;i++)"
 and so on...how does this commands look like in NASM? this is my simple question...how can 'define' integers, floating-point numbers, loops and conditions...
If anyone can help and explain with few words, or tell me where i can find such info...please

Tom Timmermann

  • Guest
Re: Few very newbie question (NASM)
« Reply #1 on: June 11, 2009, 05:41:45 PM »
I would suggest downloading and studying the "PC Assembly Tutorial" by Paul Carter.

http://www.drpaulcarter.com/pcasm/index.php

Also if you compile C code with gcc use:
gcc -S main.c
This will give you an assembly listing.

TomT

nobody

  • Guest
Re: Few very newbie question (NASM)
« Reply #2 on: June 11, 2009, 06:04:39 PM »
gcc -S is such a bad thing to do for examples for nasm (not being AT&T syntax assembler)... OpenWatcom is free, and fairly solid though, also with options to generate assembly files ... so you can take your example C/C++ code and see what it becomes...

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Few very newbie question (NASM)
« Reply #3 on: June 11, 2009, 09:25:49 PM »
Hi Aleko,

I agree with Tom that Dr. Carter's tutorial is a good place to start. Since it uses C to interface with the OS, we don't have to ask, "What OS?" :)

Using the "-S" switch with gcc is okay... if you're using gcc... but it isn't even close to "Nasm syntax". Open Watcom may be better... What compiler are you using now?

A tool I recently found is Agner Fog's "objconv", which Mr. Fog has recently taught Nasm syntax! It's intended for other things (converting object formats), but "objconv -fnasm myfile" (.o or executable) does a nice job of disassembling into Nasm syntax - with optimization tips in the comments!

http://www.agner.org/optimize/#objconv

(output file has .asm extension - watch out you don't overwrite your source!)

Ndisasm will disassemble anything, too, of course, but it doesn't know about object/executable formats, and needs a lot more help on the command line.

You may want to look at *both* a disassembly and asm output from your compiler. "All the examples you can get" is good.

As for defining integers... when I was in school, "the integers" was an infinite set of numbers, not just the ones that can be represented in some arbitrary number of bits! When I was trying to learn C, an "int" was 16 bits, as I recall. Now, I guess "int" is 64 bits (???). In asm, we have to be more specific. ("int" in asm is an instruction, not a number)

8 bits, 1 byte - like a "char" in C... unsigned char, I guess...

mybyte db 42

or, to reserve uninitialized space:

mybyte resb 1

16 bits, 2 bytes - a "short" in C, I guess... You'll be using these a lot if you start off with "dos code". In 32-bit code, not so much.

myword dw 42

uninitialized:

myword resw 1

32 bits, 4 bytes, a "doubleword" - I guess this in "int" in 32-bit C (???)...

mydword dd 42

uninitialized:

mydword resd 1

64 bits, 8 bytes, a "quadword" - maybe this is an "int" in C, these days? Or maybe "size_t"?

myqword dq 42

uninitialized:

myqword resq 1

Nasm goes on to tword, oword, yword... that's enough to start with.

Now floats...

Single precision, 32 bits, "float" in C:

myfloat dd 1.23

the decimal point tells Nasm it's a float, not an integer

Double precision, 64 bits, "double" in C:

myfloat dq 1.23

Extended precision, 80 bits (C doesn't know this one!):

myfloat dt 1.23

A "trick" about floats... the "float" parameter to printf is always promoted to double. This is "transparent" (opaque) in C, but you'll need to know about it if you're calling printf from asm!

In asm, "for" and "if" and the like are handled by conditional jumps... Certain instructions set certain bits in the "flags register", and conditional jumps jump to a new location (or not) depending on the state of these flags. Instead of:

if (condition) do this code

we actually write:

if not (condition), jump over this code

myvar db 42

start:

mov al, [myvar]

top:
cmp al, 42
je got_it ; jump if equal
; what to do if we didn't find it
; probably don't want to "fall through" to the "yes" code
jmp done

got_it:
; what to do if we found it

done:
; exit your program

push 0
call ExitProcess

or

mov eax, 1
int 80h

or

mov ah, 4Ch
int 21h

(Nasm will let you use 0xABCD to indicate hexidecimal numbers, like C. I prefer 0ABCDh)

This is apparently a hard concept for some people to "get", but to me it seems much more intuitive than "if", "while", "for" and such. Macros are available to generate code for "if" and all, but learn what it "really does" first...

Tell us what OS you're using, and maybe we can provide more concrete examples...

Best,
Frank