Author Topic: DB and friends  (Read 17666 times)

Offline jiangshi

  • Jr. Member
  • *
  • Posts: 7
  • Country: us
DB and friends
« on: April 26, 2012, 09:40:31 PM »
Greetings!

I need some clarification on the sizes of some of the pseudo instructions.  Below is my current understanding. While the manual speaks to these, the definitions were not clear to me.
 
db is for a byte
dw is for a word - a word being 16 bits
dd is for a double word
qd is for a quad word

for pointer specifiers
byte -> storage by byte
word -> storage by word
dword -> storage by double word
qword -> storage by quad word

Thanks!
~jiangshi

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: DB and friends
« Reply #1 on: April 26, 2012, 10:15:07 PM »
As you say...

db is for a byte - 8 bits
dw is for a word - a word being 16 bits
dd is for a double word - 32 bits
qd is for a quad word - 64 bits

for pointer specifiers
byte -> storage by byte - 8 bits
word -> storage by word - 16 bits
dword -> storage by double word - 32 bits
qword -> storage by quad word - 64 bits

I've added explicit bit sizes... I'm not sure what the problem is, Jiangshi. Need examples? The pseudo-instructions are pretty obvious, I think(?). The size specifiers would go like...
Code: [Select]
mov [esi], byte 42 ; fill 8 bits with 42
mov [esi], word 42 ; fill 16 bits with 42
mov [esi], dword 42 ; fill 32 bits with 42
mov [esi], qword 42 ; fill 64 bits with 42

Integer dwords and qwords and floating point dwords (single precision) and qwords (double precision) are distinguished by the presence of a '.'.

What have I missed?

Best,
Frank


Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: DB and friends
« Reply #2 on: April 27, 2012, 06:30:14 AM »
Like Frank, I'm not exactly sure what you're asking. Are you possibly confused by the db versus byte convention? DB, DW, DD, etc. are used to "Define" a type. DB means "Define Byte". The 'BYTE' keyword is used to specify the size of operands. There is also another used in the uninitialized data section called RESB or "Reserve Byte" which is useful if you just want to set up some storage and don't care what the operating system might have there.

qd is for a quad word - 64 bits

close. :P

DQ

About Bryant Keller
bkeller@about.me

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: DB and friends
« Reply #3 on: April 27, 2012, 12:24:27 PM »
... but not quite close enough to assemble... Thanks Bryant!

Best,
Frank


Offline jiangshi

  • Jr. Member
  • *
  • Posts: 7
  • Country: us
Re: DB and friends
« Reply #4 on: April 27, 2012, 04:18:14 PM »
Thank you both for looking at this. As good as the Nasm manual is, I could not quite figure out if my understanding of defining data sizes was correct. Frank, you confirmed that I have it correct - thank you.

I have a chunk of code, converting ascii to floating point and back again, that I am changing over from masm format to Nasm. I did not write the code; it was written by Richard Detmer, author of "Introduction to 80x86 Assembly Language and Computer Architecture" and taken from his book.  The code is working more or less, but is having one or two "sizing" issues. I'm still a bit rusty. And Bryant, thank you for reminding me that the D in DB and DW etc. stands for define. I keep tripping over data as in data byte rather than define byte.

Thank you for the clarifications.

BTW, speaking of books, most likely you are aware of these, but I will mention them in case someone else wants the information. Detmer's book I have already mentioned. It is good for anyone who is interested in developing Windows assembly language programs using MS Visual Studio. It is possible to develop pure assembly language projects in MSVC, even the free Express version.

Personally, I prefer the Linux world and there are some good books that address assembly language programming in Linux using the Nasm assembler. Jeff Duntemann's "Assembly Language Step by Step" 3rd ed. This is an excellent book, especially for a beginner. Some have complained that it moves too slowly, but I appreciate the extra detail, and he writes with some humor.

Dr. Paul Carter's "PC Assembly Language." Dr. Carter's book is free in pdf format, and the printed book is not very expensive. He provides a good treatment on the co-processor. Sivarama P. Dandamudi's book "Guide to Assembly Language Programming in Linux" is an excellent book!  I also have Bob Neveln's "Linux Assembly Language Programming," but I have never used it. In leafing through it, he appears to be giving some good examples on talking to the hardware.

There are a couple of others - Blum's book if you are into Gas and the AT&T syntax. Also for those interested in doing 64 bit asm using Nasm - Seyfarth's book, "Introduction to 64 Bit Intel Assembly Language Programming for Linux." I have a 64 bit machine that I am just starting to play with using this book, but I cannot say how good the book may or may not be. At first blush, it appears very good, but I need a lot more brushing up in the 32 bit world before jumping into the deep end.

Thanks for doing Nasm!

~jiangshi

Offline jiangshi

  • Jr. Member
  • *
  • Posts: 7
  • Country: us
Re: DB and friends
« Reply #5 on: April 27, 2012, 04:30:32 PM »
Quote

close. :P

DQ

Thanks for the catch - I had missed that. My fingers are dyslexic.

Offline jiangshi

  • Jr. Member
  • *
  • Posts: 7
  • Country: us
Re: DB and friends
« Reply #6 on: April 27, 2012, 06:38:41 PM »
Frank, a question about this syntax:

Code: [Select]
mov [esi], byte 42 ; fill 8 bits with 42
mov [esi], word 42 ; fill 16 bits with 42
mov [esi], dword 42 ; fill 32 bits with 42
mov [esi], qword 42 ; fill 64 bits with 42

Wouldn't I need to put a size specifier in front of the [esi] ?

Thanks.
~jiangshi

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: DB and friends
« Reply #7 on: April 27, 2012, 07:27:00 PM »
Either way,
Code: [Select]
mov byte [esi], 42
mov [esi], byte 42
assemble to the same thing. (even "both" works - just tried it!) In some cases, Nasm may be fussier about which side of the comma the size-specifier goes, but generally Nasm just needs to know the size. Maybe worth mentioning that Masm "remembers" the size you said the variable was (and uses the "ptr" keyword for this) - Nasm does not!
Code: [Select]
myvar db ? ; Masm?
myvar resb 1 ; Nasm
...
mov myvar, 42 ; OK for Masm? "byte ptr" optional?
mov [myvar], byte 42 ; required for Nasm

If there's a register involved, Nasm can determine the size...
Code: [Select]
mov [esi], al
mov [esi], eax

Nasm will complain about "operation size not specified" if it needs to be told...

Maybe worth mentioning that, since Nasm doesn't "remember" the size of your variables, it will let you do:
Code: [Select]
myvar db 0
...
mov [myvar], eax
without notification of an error. You're supposed to know! This "amnesia" is deliberate, though some people would call it a "misfeature". You may sometimes  intend to do this, usually it's an error!

Thanks for the book recommendations! I'll add Jonathan Bartlett's "Programming from the Ground Up" - AT&T syntax (although we have the examples translated to Nasm), but a nice "low level" introduction to assembly language...

Best,
Frank