2

I'm reading Assembly Language Step by Step Programming with Linux by Jeff Duntemann.

In the book he mentioned that the code

EatMsg: db "Eat at Joe's",10
mov ecx,EatMsg

Copies not the content of EatMsg but the memory where the content is stored at. This is understood and I have confirmed it via edb. But the statement:

EatMsg: dw "Eat at Joe's",10

When I checked this code in the debugger its the same memory address when using the db directive, that's understood as well however I know that dw, refers to "define word" (2 bytes).

But can someone tell me what actually happens during execution does the CPU reads a word at a time, because of the dw directive? I'm trying to visualize it.

The book confuses me because he mention that the general purpose registers would hold the address of the define string then I read the below excerpt and got confuse:

WordString: dw 'CQ'
DoubleString: dd 'Stop'

The DW directive defines a word-length variable, and a word (16 bits) may hold two 8-bit characters. Similarly, the DD directive defines a double word (32-bit) variable, which may hold four 8-bit characters. The different handling comes in when you load these named strings into registers. Consider these two instructions:

mov ax,WordString
mov edx,DoubleString

In the first MOV instruction, the characters ‘‘CQ’’ are placed into register AX, with the ‘‘C’’ in AL and the ‘‘Q’’ in AH. In the second MOV instruction, the four characters ‘‘Stop’’ are loaded into EDX in little-endian order, with the ‘‘S’’ in the lowest-order byte of EDX, the ‘‘t’’ in the second-lowest byte, and so on. This sort of thing is a lot less common (and less useful) than using DB to define character strings, and you won’t find yourself doing it very often. Because eatsyscall.asm does not incorporate any uninitialized data, I’ll hold off discussing such definitions until we look at the next example program.

How the CPU executes this and whats the benefit of using the db directive against the dw directive?

Thanks in advance

2
  • 2
    In addition to "what assembler are you using?", we need to ask "what assembler was Jeff Duntemann using?". First Edition used Masm, Second and Third Edition use Nasm. The text quoted above with WordString and DoubleString without the square brackets is incorrect for Nasm. Commented May 9, 2015 at 22:22
  • Its the Third Edition which is NASM but it seems your telling me he made a mistake either the code he wrote was wrong or his explanation is wrong. The WordString and DoubleString only needs to be surrounded by square brackets if its referring to the data at the memory address not having them simply refers to the address. But his explanation refers to the data at memory. I seen errors before in this book so I believe you are right. Thank you. Commented May 10, 2015 at 5:53

1 Answer 1

0

Which assembler do you use? In nasm dw with a string will just pad the string to the two byte boundary.

The difference between two and four bytes being read has nothing to do with db or dw but instead the use of ax (a two byte register) vs edx (a four byte register).

Loading a two byte register will cause two bytes of memory to be read. For instance if you instead load dx from data declared with dd you would get a register filled with two of the four bytes.

In summary, dd, db, dw and friends are just about having the assembler produce the correct binary representation of some data you have in mind. In the end its just binary data at an address and you could get by with just db if that's all you had, it would just involve more thinking about the layout on your part.

2
  • Yea i understand its when u load back the data, the size of the register should match the data you are planning to load back. But his example code is loading the address and his explanation is referring to loading the data. The code should have used [WordString] and [DoubleString] to match his explanation. I'll still give you the answer but I believe its an error with his book. @FrankKolter comment made me realize this. Commented May 10, 2015 at 14:21
  • You are correct -- his explanation of his example does not match the behavior of nasm. Commented May 10, 2015 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.