SPO600 Lab 4 - Part 1: AArch64
Published: 2022-11-03 04:50:55

In this lab we expriment with the x86_64 and aarch64 platforms with the loop code demonstrated by Chris Tyler.

The following tasks from lab 4 are completed and explained in this blog.
- Connecting to israel server.
- Copy and unpack tarball, which for a windows user like me can be a new experience.
- Explore the files inside the spo600 folder and use the makefiles and make command to build the code.
- Use objdump -d to inspect what was inside the binary file.
- Modify the Hello World! In AArch64 and x82_64 to make it loop as demonstrated in the lab class.
- Modify the loop to run until 30 iterations (which took hours for me)

## Lets start with the lab.
- Connecting to israel server.

```
ssh username@israel.cdot.systems // replace username with your username.
```
- Copy and unpack tarball, which for a windows user like me can be a new experience. 

```
cp /public/filename ~
tar -xvzf filename
```
- Explore the files inside the spo600 folder and use the makefiles and make command to build the code.
Use make `clear `first and then use `make filename` for a particular file or make for all files in the dir.
- Use objdump -d to inspect what was inside the binary file.
Move to the c dir and the use objdump -d to inspect the c code writen.
- Modify the Hello World! In AArch64 and x82_64 to make it loop as demonstrated in the lab class.
- Modify the loop to run until 30 iterations (which took hours for me)

The code of loop that would print the loop counter every iteration from 0 – 10 appending to a statically coded string “Loop”. This task alone took us about a half of the next class. And to even expand it, we had to write code to make the program print 2 digits instead of 1 from every iteration between 0 – 10. So the results would look like: Loop 00, Loop 01, Loop 02, Loop 03,…. Loop 30. And this also took about a half of the class.

My AArch64 final code:
```
.text
.globl _start
min = 0                          
max = 30 // should use 31

_start:

        mov     x15, 10 // for division
        mov     x19, min // value of the counter
        add     x18, x19, '0' //counter
        adr     x17, msg+6
        adr     x20, msg+7

loop:
        cmp x19, x15

        b.lt inside //start from smallest decimals

        udiv x12, x19, x15 // divide x19/x15 and store in x12 // udiv r0,r1,r2     // unsigned - divide r1 by r2, places quotient into r0 - remainder is not calculated (use msub)
        add x14, x12, '0'
        strb w14,[x17]

inside:

        msub x10,x15,x12,x19 //
        msub r0,r1,r2,r3  // load r0 with r3-(r1*r2) (useful for calculating remainders)
        add x10, x10, '0'
        strb w10,[x20]

        //Printing msg

        mov     x0, 1           
        adr     x1, msg         
        mov     x2, len         

        mov     x8, 64          
        svc     0               


        add x19, x19, 1 
        cmp x19, max
        b.ne loop


        //return

        mov     x0, 0           
        mov     x8, 93          
        svc     0               

.data
msg:    .ascii      "Loop: #\n"
len=    . - msg

```


here is my ./loop run on AArch64 on israel SPO Server.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uux9q96p7u78txm6y17t.png)

While debugging, syntax errors are easy to correct but logical errors are much harder to correct in assembly. You have the debugger available to you and it will show you where your program catches a seg-fault and crashes but to understand why it’s causing the seg-fault may not be clear, making it extremely difficult to fix.