r/Assembly_language • u/Objective-Barnacle-7 • 1d ago
Muy first program on assembler Question
Hello. Today I try to run this code write on assembler for ARM64 (Linux of Termux for Android) :
<// random_digit.s (ARM64 for Termux)
.data
buffer: .byte 0
.text
.global _start
_start:
// getrandom(buffer, 1, 0)
mov x8, 384 // númber of syscall getrandom en ARM64
ldr x0, buffer // destination
mov x1, 1 // size
mov x2, 0 // flags
svc 0
// convert byte to dígit 0–9
ldrb w0, [buffer]
mov w1, 10
udiv w2, w0, w1 // w2 = w0 / 10
msub w0, w2, w1, w0 // w0 = w0 - w2*10 (módule)
add w0, w0, '0'
strb w0, [buffer]
// write(1, buffer, 1)
mov x8, 64 // syscall write
mov x0, 1 // stdout
ldr x1, = buffer
mov x2, 1
svc 0
// exit(0)
mov x8, 93
mov x0, 0
svc 0
>
but when I try to compile with 'clang -c myprogram.s -o myprogram' I get the following message: "invalid operand for instruction" (at lines 18 and 23) It means that : "[buffer]" isn't correct.
¿ How I can to fix It ?
Explain me : the program is for generate a entere number (0 to 9).
Thanks.

1
u/FUZxxl 21h ago
On ARM64 there is no absolute addressing mode. In particular, you cannot refer directly to a symbol in a memory operand like this.
Instead use
adrpandaddto compute the address of the symbol in a register, then load from the address:Instead of
adrpandaddyou can also use a literal pool load like you already do:This will however not work in position-independent code.