Lab 3 - Aarch64 assembly challenge

 Introduction 

This week, I tried to overcome the aarch64 compiler challenge at Lab3. The task is very simple, it is just creating multiply table from 1 to 12 with aarch64 assembly compiler.

Before creating the code, I made it with C language.

int main() {
for(int i = 1; i < 13; i++){
for(int z = 1; z < 13; z++) {
printf("%d X %d = %d\n",z , i, i*z);
}
printf("------------\n");
}
return 0;
} 

In order to make multiplication table, it needs to use double loop.


Assembly Code

once I created above program with C language I tried to make the assembly code
based on the C code.

set the outer index variable to 1.

.text

.globl _start


min=1

max=13

_start:

    mov x19, min       // i = 1


Outer loop start:

initially I set x15, x16 to 10 and 100 respectively to calculate tens and hundreds place. Once I calculate tens place of index in outer loop I converted it to ASCII type and then add it to 'msg'

outer_loop:

    mov x15, 10

    mov x16, 100


    cmp x0, 13      // if i < 13

    b.ge end        // break


    udiv x12, x19, x15

    add x12, x12, '0'


    adr x1, msg

    mov x0, 1


    strb w12, [x1, 1]

    mov x20, 1       // z = 1


Inner loop start:
Once inner loop start, I calculate multiplied value and tried to get hundreds, tens and unit place of the value

inner_loop:

    cmp x1, 13      // if z < 13

    b.ge outer_loop_end // break


    mul x13, x20, x19  // x2 = i * z


    // hundred place

    udiv x21, x13, x16

    add x21, x21, '0'

    strb w21, [x1, 9]


    //tens place

    udiv x22, x13, x15

    add x22, x22, '0'

    strb w22, [x1, 10]


    //unit place

    msub x23, x22, x15, x13

    msub x23, x21, x16, x13

    strb w23, [x1, 11]


    add x20, x20, 1   // z++

    add x20, x20, '0'

    strb w20, [x1, 6]


    udiv x24, x20, x15

    add x24, x24, '0'

    strb w24, [x1, 5]

    b inner_loop


After inner loop outer loop end start so that It increase outer loop index.

outer_loop_end:

    add x19, x19, 1   // i++

    udiv x25, x19, x15

    add x25, x25, '0'

    strb w25, [x1]

    b outer_loop

End of the code and msg format section:

end:

    mov x0, 0

    mov x8, 93

    svc 0


.data

msg:    .ascii " # x  # =   #\n"

len=    . - msg



Conclusion:

Although I spent quite long time, I couldn't find out the solution. In the C, C++ or Python, I usually focus on how to get data by calculating. But in assembly, even I find out calculation formula for answer, I also have to find out how to display the answer in well organized format. And I think the formatting part is more difficult than any other task. After this challenge, I decided to study harder than before.

댓글

이 블로그의 인기 게시물

Lab 1 - Calculate performance & memory usage

Project Stage - 1 GCC for AArch64

Project stage-3 (testing & reflection)