Here is a basic loop in AArch64 assembler - this loops from 0 to 9, using r19 as the index (loop control) counter:
.text .globl _start min = 0 /* starting value for the loop index; note that this is a symbol (constant), not a variable */ max = 30 /* loop exits when the index hits this number (loop condition is i<max) */ _start: mov x19, min loop: /* ... body of the loop ... do something useful here ... */ add x19, x19, 1 cmp x19, max b.ne loop mov x0, 0 /* status -> 0 */ mov x8, 93 /* exit is syscall #93 */ svc 0 /* invoke syscall */
This code doesn't actually do anything while looping, because the body of the loop is empty. On an AArch64 machine, combine this code with code from the "Hello World" assembley-language example, so that it prints a word each time it loops:
Loop Loop Loop Loop Loop Loop Loop Loop Loop Loop
Then modify the message so that it includes the loop index values, showing each digit from 0 to 9 like this:
Loop: 0 Loop: 1 Loop: 2 Loop: 3 Loop: 4 Loop: 5 Loop: 6 Loop: 7 Loop: 8 Loop: 9
Now we had a list of servers we could choose from so we decided to go with aarchie.
Then after taking reference from both the loop example given and the hello world program we were able to print the iterations of loop without numbers. Here is the code:
.text
.globl _start
min = 0 /* starting value for the loop index; note that this is a symbol (constant), not a variable */
max = 30 /* loop exits when the index hits this number (loop condition is i<max) */
_start:
mov x19, min
loop:
mov x0, 1 /* file descriptor: 1 is stdout */
adr x1, msg /* message location (memory address) */
mov x2, len /* message length (bytes) */
mov x8, 64 /* write is syscall #64 */
svc 0 /* invoke syscall */
add x19, x19, 1
cmp x19, max
b.ne loop
mov x0, 0 /* status -> 0 */
mov x8, 93 /* exit is syscall #93 */
svc 0 /* invoke syscall */
.data
msg: .ascii "loop \n"
len= . - msg
Though the syntax looks a bit scary for now for this architecture it is just a matter of time before we get used to these instructions and then everything should be fine.
No comments:
Post a Comment