Page 1 of 1

NASM Array Error

Posted: Sun Nov 22, 2009 11:38 am
by ChrisSkura
In my os I want to make an array that holds the keys pressed by the keyboard and it works if I do this:

Code: Select all

mov byte [key_strokes+0],al
but that won't save a bunch of keystrokes it will only save one.
I want to do something like this

Code: Select all

mov byte [key_strokes+[array_num]],al
inc byte [array_num]
when I do that I get

Code: Select all

error: expression syntax error
and if I try this

Code: Select all

mov byte [key_strokes+array_num],al
inc byte [array_num]
i get

Code: Select all

error: beroset-p-637-invalid effective address
I looked at a bunch of tutorials on google and I'm doing what they say but am still getting errors

Re: NASM Array Error

Posted: Sun Nov 22, 2009 12:05 pm
by Firestryke31
You will have to store the pointer in a register, possibly "esi" or "edi" (if in 32-bit mode. If 16-bit, drop the "e").

Code: Select all

 mov esi, key_strokes
 add esi, byte [array_num]
 mov [esi], al
 inc byte [array_num]
Note that the code is written off the top of my head and is untested. It may not even assemble. However, it is meant to convey the general idea of what you need to do.

Re: NASM Array Error

Posted: Sun Nov 22, 2009 1:10 pm
by gravaera
What's most important for you to understand is that NASM does not support the idea of an 'array'. Technically, at the machine level it's all just byte addressable RAM.

If you wish, you may think of the routine of indexing into an array (take it to be a pointer, or a symbol address) as simply adding the offset to the original pointer. You understand this already, based on your example.

Code: Select all

   ;; 'array': Symbol giving the start addr of a contiguous 256 byte RAM sequence.
array:
   resb 0x100

print_chars:
   ;; etc.
ret

main:
   mov esi, array
   mov ecx, 0x0

.continue:
   push byte [esi + ecx]
   call print_chars
   add esp, 4
   inc ecx
   cmp ecx, 255
   jb .continue
Is roughly equivalent to:

Code: Select all

static unsigned char[256];

void print_char(unsigned char ch) {
   // etc.
};

int main(void)
{
   int i=0;
   for (; i<256; i++)
   {
      print_char(array[i]);
   };
};
Except that a compiler will find some way to optimize the assembly language instructions more efficiently.

You may also check the NASM manuals to supplement any information given here.

Re: NASM Array Error

Posted: Sun Nov 22, 2009 2:08 pm
by Thomas
Hi ChrisSkura,
you cannot do that , you might want to take a good look at 80x86 addressing modes . Use a register instead of variable and you will be fine there .

-- Thomas

Re: NASM Array Error

Posted: Mon Nov 23, 2009 2:46 am
by qw
This is not a NASM issue. You just can't address "[key_strokes + [array_num]]" on an 80x86. What you are looking for are addressing modes, which are a bit different in 16 bits and 32 bits modes. Note that Randy Hyde uses MASM syntax in the first link and his own "High Level Assembler" syntax (with source and destination reversed) in the last link.

Roel