summaryrefslogtreecommitdiff
path: root/tests/format/elf/strings.asm
blob: d9a75d3f2145c2950c733dcc07b46879fde3bf98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77

.data

.global msg
.global no_arg_msg
.global got_arg_msg

msg:
    .ascii      "Hello, ARM!\n"

len = . - msg

no_arg_msg:
    .ascii      "No command line argument...\n"

no_arg_len = . - no_arg_msg

got_arg_msg:
    .ascii      "Got command line argument(s)...\n"

got_arg_len = . - got_arg_msg

.text

.global do_syscalls

do_syscalls:

    /**
     * syscall write(int fd, const void *buf, size_t count)
     */

    mov     %r0, $1     /* fd -> stdout */
    ldr     %r1, =msg   /* buf -> msg */
    ldr     %r2, =len   /* count -> len(msg) */
    mov     %r7, $4     /* write is syscall #4 */
    swi     $0          /* invoke syscall */

    /**
     * syscall write(int fd, const void *buf, size_t count)
     */

    mov     %r0, $2     /* fd -> stderr */
    mov     %r7, $4     /* write is syscall #4 */

    ldr     %r3, [sp]   /* argc */
    cmp     %r3, $1

    beq     no_arg

    ldr     %r1, =got_arg_msg   /* buf -> msg */
    ldr     %r2, =got_arg_len   /* count -> len(msg) */

    b       process_arg

no_arg:

    ldr     %r1, =no_arg_msg   /* buf -> msg */
    ldr     %r2, =no_arg_len   /* count -> len(msg) */

process_arg:

    swi     $0          /* invoke syscall */

    /**
     * syscall exit(int status)
     */

    mov     %r0, $123   /* status -> 0 */
    mov     %r7, $1     /* exit is syscall #1 */
    swi     $0          /* invoke syscall */

.global _start

_start:

    bl      do_syscalls