Friday, 21 June 2013

Storing data to an element of a vector

We write a constant to the second element of the vector and then read and return it.

# movtest4.s – An example of indirect addressing
.equ datum_size,1
.globl _start
.align 2
.section .data
values:
   .byte 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60
.text
.align 2
_start:
 ldr r4, =values
 mov r5, #100
 strb r5, [r4, #datum_size]!            @ store 100 to the second element of values
                                        @ we update the pointer r4 via the trailing !
 ldrb r0, [r4]                          @ load that value to r0 for return to OS
        mov     r7, #1                  @ set r7 to 1 - the syscall for exit
        swi     0                       @ then invoke the syscall from linux


We write a constant to the second element of the vector and then read and return it.

bob@poland:~/www/examples$ make movtest4
/usr/bin/as -gstabs -o movtest4.o movtest4.s
/usr/bin/ld -o movtest4 movtest4.o
bob@poland:~/www/examples$ ./movtest4 
bob@poland:~/www/examples$ echo $?
100
bob@poland:~/www/examples$

Again, it can be instructive to walk through the code with gdb.

(gdb) b _start
Breakpoint 1 at 0x8078: file movtest4.s, line 12.
(gdb) run
Starting program: /home/bob/src/professional_assembly_language/arm/chap05/movtest4 

Breakpoint 1, _start () at movtest4.s:12
12  mov r5, #100
(gdb) i r r5
r5             0x0 0
(gdb) x/11b &values
0x10090 : 10 15 20 25 30 35 40 45
0x10098 : 50 55 60
(gdb) s
13  strb r5, [r4, #datum_size]!       @ write back the incremented data pointer
(gdb) i r r5
r5             0x64 100
(gdb) x/11b &values
0x10090 : 10 15 20 25 30 35 40 45
0x10098 : 50 55 60
(gdb) s
14  ldrb r0, [r4]                    @ load that value to r0 for return to OS
(gdb) x/11b &values
0x10090 : 10 100 20 25 30 35 40 45
0x10098 : 50 55 60
(gdb) i r r0
r0             0x0 0
(gdb) s
15         mov     r7, #1            @ set r7 to 1 - the syscall for exit
(gdb) i r r0
r0             0x64 100
(gdb) 

No comments:

Post a Comment