Author Topic: Big Array in Assembler  (Read 9302 times)

nobody

  • Guest
Big Array in Assembler
« on: July 17, 2007, 06:47:41 PM »
Hi I am new to the Assembler language but I am an experienced programmer in Java / C++.
I would like to do the following in NASM.

1) Create an array of size 10.000.000 of 32bit Integer (~40MByte).
2) Add first half of the array to the second half.

The array may also be static.
But how do I solve the MByte limitations?

I am running Windows and have a Intel Core Duo processor.
I am thankful for any help!

Benjamin

nobody

  • Guest
Re: Big Array in Assembler
« Reply #1 on: July 17, 2007, 11:31:25 PM »
my_array resd 10000000

is fine with Nasm. If you're encountering "limits", maybe:

push 10000000 * 4
call some_kinda_malloc

The latter is "better coding practice", perhaps, but the former is no problem to Nasm. I've got a Linux implementation in 476 bytes (including populating the array and displaying results). What's your Java/C++ implementation weigh?

Best,
Frank

nobody

  • Guest
Re: Big Array in Assembler
« Reply #2 on: July 18, 2007, 09:18:51 AM »
Benjamin again...

To answer your question in Java the Code looks exactly like:

public class Example {

public static void main(String[] args) {
      final int max = 10000000;
      final int halfMax = max / 2;

int[] data = new int[max];
      for (int i = 0, j = halfMax; i < halfMax; i++, j++) {
         data[j] += data;
      }
   }

}

Since java generates byte code you can't compare it exactly.
But the size of the Example.class file was 581 Bytes.

I would be very thankful if someone could translate my code to (working under win32) NASM.
I am trying several days but I can't get it work.

Where can I can simple examples for NASM (concerning arrays)?

Benjamin

nobody

  • Guest
Re: Big Array in Assembler
« Reply #3 on: July 18, 2007, 10:51:31 AM »
Hi Benjamin again :)

581 bytes! I'm impressed. Maybe Java deserves more credit than I've been giving it. (although I'll bet there's some "runtime" involved)

Dr. Paul Carter's tut has an array example...

http://www.drpaulcarter.com/~pcasm

(and I posted a Linux example on c.l.a.x. that might help you)

I'm not familiar with Java, but I believe "new" implies that we ought to allocate the buffer. I think all we'd have to do (in Windows) is push the size we want (10000000 * 4 - 'cause we want the size in bytes), and call... maybe GlobalAlloc, maybe VirtuallAlloc - I don't know which would be most appropriate (or maybe some other *Alloc). I suppose it's polite to free it when we're done...

extern GlobalAlloc
extern GlobalFree
extern ExitProcess
global my_entry

section .bss
bufptr resd 1

section .text
my_entry:
push 10000000 * 4
call [GlobalAlloc]

test eax, eax
jz no_damn_memory

mov [bufptr], eax

In the example I posted with the static buffer, we could refer to the array by name - "[array + ecx]" or so. We won't be able to do that with an allocated array...

mov esi, [bufptr]
xor ecx, ecx

loop_top:
mov eax, [esi + ecx + HALFSIZE]
add [esi + ecx], eax

HLLs do "pointer arithmetic" for us. If "p" points to bytes, "p++" adds one to "p". If "p" points to "int"s (assuming dwords - 32 bits - 4 bytes) "p++" adds 4 to "p". If "p" points to double precision floats, "p++" adds 8. In asm, we have to do it ourselves.

add ecx, 4
cmp ecx, HALFSIZE
jb loop_top

push dword [bufptr]
call GlobalFree ; ???

Don't forget to exit cleanly.

I don't do Windows, but "something like that"...

Your Java example doesn't seem to initiallize the array to anything, and doesn't do anything with the results, so this code doesn't either. The Linux example I posted to c.l.a.x. did (but didn't *store* the result, as you want)... but the display routine will have to be different for Windows.

For test purposes, you may want to start with a smaller array, as "x87asm" suggested in c.l.a.x.

If you post the specific problems you're having with Nasm (or Windows), we may be able to suggest something...

Best,
Frank