How to check an "array's length" in Assembly Language (ASM),

54,600

Solution 1

Assembly is a lot more lowlevel than Java. This, among other things, means there's no such thing as an "array". At least in the safe Java form in which you know it.

What would be equivalent to an array is allocating a chunk of memory, and treat it as an array. The length and such you'll have to manage yourself, though, as all you have is a chunk of memory containing your data. If you want to store any metadata, such as length, you'll have to do that yourself.

Arrays as you know them in Java contain metadata such as length, and do bounds checking. These do the same thing you'll have to do, only they hide it so you won't have to worry about those things.

I suggest you take a look at the following for an introduction on how one would commonly create and use what is equivalent to an array in assembly:

Solution 2

There is no such thing as an array in assembly (as far as I know). You are free to invent how an array works if you wish.

If you're reading the assembly generated by a compiler then you will have to ask specifically about that compiler.

One way of doing this would be to allow the first byte of the array to store the length of each element. Another way would be to null terminate the array (this is generally how strings are maintained).

Solution 3

I found this thread very enlightening on the subject: Asm Examples: Finding the size of an array with different data types

Solution 4

a db 10h,20h,30h,40h,50h,60h
n db n-a

in the above code 'a' is an array having 6 elements by default 'a' will point to the first element of the array and 'n' will be initialised just after the array. so value 'n-a' will correspond to the length of the array which we are storing in n. don't initialise other variables in between a and n. This might give you wrong results.

Solution 5

You could use the following program (ATT assembly syntax) to determine the number of elements in an array:

.section .data
array:
    .long 3,5,8             # create an array of 32-bit integers
    arrsize = . - array     # in bytes (assemble-time constant, not stored)
    arrlen = (. - array)/4  # in dwords

.section .text
.globl _start
_start:

    mov   $arrlen, %edi
    mov   $60, %eax      # __NR_exit  in asm/unistd_64.h
    syscall              # sys_exit(arrlen)

Assemble and link the program:

gcc -c array.s && ld array.o
  #or
gcc -nostdlib -static array.s

The result is 3, as expected:

>./a.out            # run program in shell
>echo $?            # check the exit status (which is the number of elements in the array)
3
Share:
54,600

Related videos on Youtube

Mike
Author by

Mike

Updated on October 18, 2020

Comments

  • Mike
    Mike over 3 years

    I just started learning Assembly language. In java, if we have an Array, we can always use array.length to get its length. Is there such a thing in assembly? If so, can someone please guide me here?

    Edit:

    My apologies, I know assembly doesn't have arrays, I was trying to simplify things.

    What I meant was, if for example I have a variable

    data DB 1,2,3,5,7,8,9,10
    

    Given that DB can contain any amount of elements, how can i check the total variable it contains?

    Something like java, where use int array to store this

    int data = {1,2,3,4,57,8,9,10};
    

    We can just data.length to find the total amountt of elements.

    • Sebastian Paaske Tørholm
      Sebastian Paaske Tørholm about 13 years
      There is no such thing as an inherent length of arrays. Arrays are just a chunk of memory you put things in. You don't have any way of knowing what size it is; you'll need to keep track of this yourself.
  • Jose Manuel Abarca Rodríguez
    Jose Manuel Abarca Rodríguez almost 8 years
    Compiler EMU8086 doesn't seem to recognize lengthof keyword. What compiler do you use?
  • Jose Manuel Abarca Rodríguez
    Jose Manuel Abarca Rodríguez almost 8 years
    Compiler EMU8086 doesn't seem to recognize lengthof keyword. What compiler do you use?
  • Peter Cordes
    Peter Cordes over 5 years
    You don't need div to divide by 4! Use a right shift. Or better, use an assemble-time operator, like len = (. - array) / 4. Also, the 32-bit int 0x80 ABI take the first arg in EBX, so your code does sys_exit(4). I confirmed on my own desktop. If you were thinking of the 64-bit ABI for syscall, you'd use movzbl %ax, %edi, not %si. (Unless you're using a 32-bit install, gcc -c and ld will make 64-bit code, so What happens if you use the 32-bit int 0x80 Linux ABI in 64-bit code? also applies.)
  • builder-7000
    builder-7000 over 5 years
    Thanks for pointing out the problems with the original answer. I find the updated answer much clearer by avoiding the use of the div instruction.
  • Peter Cordes
    Peter Cordes over 5 years
    BTW, you original also depended on %dx being zero for div to not overflow and raise a #DE exception (causing Linux to deliver a SIGFPE). So it crashed if you linked it as a dynamic executable. But anyway, shr $2, %edi is what you should use for runtime unsigned division by 4 in cases where you want that, never div. (Also, 16-bit operand-size is generally slower, especially in 32 and 64-bit mode. See agner.org/optimize)

Related