Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The best way to answer this is to use C examples. In C, there are two ways of keeping track of the length of an array:</p> <ol> <li>You store a variable telling you how long you made the array.</li> <li>You do what strings do and have the last element as 0. Then, you can implement a "string" length function that loops over the array until it finds zero.</li> </ol> <p>For the first example, depending on what assembler you're using, you might be able to use some tricks. For example, in nasm you can do this:</p> <pre><code>SECTION .data msg: db "Hello World",10,0 ; the 0-terminated string. len: equ $-msg ; "$" means current address. </code></pre> <p>As you can see, we use the <code>equ</code> operator to get nasm to calculate the difference between the current address and the start of <code>msg</code> which should equal its length. Alternatively, you could just write the length in there as a digit.</p> <p>For the second case, you could easily write a small function to do it. Roughly speaking, if you:</p> <pre><code>SECTION .text global _mystrlen _mystrlen: push ebp ; conform to C calling conventions. mov ebp, esp xor eax, eax lea ecx, [esp+8] ; load the start of the array into ecx jecxz end ; jump if [ecx] is zero. loop: add eax, 1 ; could use inc eax as well. add ecx, 4 ; always increment by (sizeof(int)). Change as appropriate mov edx, [ecx] ; load ecx cmp edx, 0 ; compare with zerp je end ; if ecx is zero, we're done. jmp loop ; if ecx isn't zero, loop until it is. end: leave ; restore stack frame ret ; return. eax is retval </code></pre> <p>Note that I haven't tested that. It's just to give you an idea.</p> <p><strong>Edit</strong> I've tested the <code>x86_64</code> version on Linux, using <code>rdi</code> as param1, passing in <code>int arr[10] = {1,2,3,4,5,6,7,8,9,0};</code>. Returns <code>9</code> as expected. Note that on Linux the underscore preceding <code>mystrlen</code> is unnecessary. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload