tags:

views:

47

answers:

2

Hi

I am new to Pentium assembly programming.

Could you check if I am doing the translation of C to assembly correctly?

Condition: 32-bit addresses, 32 bit integers and 16 bit characters.

char[5] vowels="aeiou";

Translate: vowels db "aeoiu" ; or should it be "vowels dw "aeoiu" ?

How to access vowels[p]? Is it byte[vowels+p*2]? (since characters are 16 bit? )

Many thanks

A: 

Yes, the nth element of an array of type T starting at arr is located at memory address arr+n*sizeof(T).

Alexander Gessler
"vowels dw "aeoiu" or vowels db "aeoiu"? Which one is correct?
leon
A: 

My Intel assembly syntax is a little byte rusty, but word[vowels+p*2] is certainly more correct than byte[vowels+p*2]. You have to multiply by the size of the elements yourself and specify the kind of data read (here, 16-bit quantities).

For the first question, it depends how your assembler interprets "" after db and dw. I do not know about that — I never mixed assembly and 16-bit encodings — but I would assume dw is correct.

Speaking of which, remember to use instruction MOVZX instead of the 16-bit registers that the assembler may let you use. Using the 16-bit registers in 32-bit mode generates long instructions that also execute slowly for a variety of reasons. MOVZX expands the 16-bit value read to occupy an entire 32-bit register, which is the correct way to handle them.

Pascal Cuoq
If I do "vowels dw "aeoiu" in the beginning. When I want to express : vowels[p], should I use word[vowels+p*4] instead of word[vowels+p*2]? Since I declared each char in vowels as a word(16 bit).
leon
I think that the "word" in `word[vowels+p*2]` only serves to specify what kind of quantity you want to be read, regardless of the computation of the address, which you must do yourself and where `vowels+p*2` is correct since each of your chars is 16-bit.
Pascal Cuoq