Question

I am trying to print all the elements of an array using the write() system call. I haven't used write a whole lot before but from what I understand, the parameters I am passing to write seem to be correct. I know that write takes:

  1. The file descriptor of the file (1 means standard output).
  2. The buffer from where data is to be written into the file.
  3. The number of bytes to be read from the buffer.

Here is my code:

mov     r3, #0                 /*Offset (also acts as LVC)*/
print:  mov     r0, #1         /*Indicates standard output*/
        ldr     r4, =array     /*Set r4 to the address of array*/
        ldr     r5, [r3,r4]    /*Add offset to array address*/
        ldr     r1, [r6]       /*Element of array to write*/
        mov     r2, #1         /*Write 1 byte*/
        bl      write
        add     r3, r3, #1     /*Increase offset each iteration*/
        cmp     r3, #41
        blt     print

Does this look correct? Is it likely that my problem is elsewhere in my program?

Était-ce utile?

La solution

No. You want to pass the address where the data to write are in r1, not the value itself.

Therefore r1 should be set to just <address-of-array> + <index>, i.e.:

    ldr     r4, =array     /*Set r4 to the address of array*/
    add     r1, r3, r4     /*Add offset to point to array item */

It crashed for you, because you tried to read from memory at an invalid address -- the value of the array item. You were reading a word (ldr r5, [r3,r4]), not byte, from the array at index r3, then trying to read another word (not byte) from that address.

It is not relevant in this case, but just for reference, you would use lrdb to read a single byte.

Also the "invalid address" above might be both that it is undefined and falls outside of any mapped region, but also that it is improperly aligned. The ARM architecture disallows reading a word, e.g. a 32 bit value, from address not aligned at those 32-bits (4 bytes). For r3 == 1 in the second iteration, this wouldn't apply (assuming array would start on a 32-bit boundary).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top