NASM Array Error

Programming, for all ages and all languages.
Post Reply
ChrisSkura
Member
Member
Posts: 33
Joined: Sat Nov 07, 2009 2:47 am

NASM Array Error

Post 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
User avatar
Firestryke31
Member
Member
Posts: 550
Joined: Sat Nov 29, 2008 1:07 pm
Location: Throw a dart at central Texas
Contact:

Re: NASM Array Error

Post 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.
Owner of Fawkes Software.
Wierd Al wrote: You think your Commodore 64 is really neato,
What kind of chip you got in there, a Dorito?
User avatar
gravaera
Member
Member
Posts: 737
Joined: Tue Jun 02, 2009 4:35 pm
Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.

Re: NASM Array Error

Post 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.
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
User avatar
Thomas
Member
Member
Posts: 281
Joined: Thu Jun 04, 2009 11:12 pm

Re: NASM Array Error

Post 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
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: NASM Array Error

Post 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
Post Reply