Friday, March 13, 2020

Lab 5 - Aarch64



Through this Lab5 we can compare with  Aarch64 and x86-64.  we will print "Hello, World" 30 times with order number.

First part: Aarch64 assembler

1. Use the "objdump -d" command and find <main>

"hello"
this hello object code comes from below c code.

"hello2"


this hello2 object code comes from below c code.

"hello3"


this hello3 object code comes from below c code.


2. How to solve ...

To add order number after "Hello, World" we need to know the order number location and how to add ASCII character as the order number.

so, we use digit_1, and digit_2 subroutines and loop and print subroutines.
digit_1 : add one-digit number behind the "Hello, World"
digit_2 : calculate the number of two-digit behind the "Hello, World"
print: print the "Hello, World" and increased the order number until max
loop: compare the order number whether is one_digit or two_digitnumbers

3. whole code

.text
.globl _start

_start:
        mov     x19,min         /*store the min value into x19 as a loop index*/
        mov     x18,division    /*store the division value(10) into x18*/
loop:
        mov     x0, 1           /* file descriptor: 1 is stdout */

        cmp     x19, 9          /* compare x19(loop index value) with 9*/
        b.gt    digit_2         /* if the value is greater than 9(2-digit), go to the subroutine digit_2*/
        bl      digit_1         /* if the value is less or equal than 9(1-digit), go to the subroutine digit_1*/

digit_1:
        add     x20, x19, '0'   /* ascii number character*/

        adr     x30, msg+14     /* the digit location within string */
        strb    w20, [x30]      /* store the digit at the location */
        bl      print           /* go to the print subroutine */


digit_2:
        udiv    x25, x19, x18   /* divide the value by 10 and store the value into the x25 */
        msub    x26, x25, x18, x19 /* store the remainder into x26 */

        add     x21, x25, '0'   /* ascii number character */
        add     x20, x26, '0'   /* ascii number character */

        adr     x25, msg+14     /* the digit location within string */
        strb    w21, [x25]      /* store the digit at the location */
        adr     x26, msg+15     /* the digit location within string */
        strb    w20, [x26]      /* store the digit at the location */

        bl      print

print:
        adr     x1, msg         /* store the locatio of message */
        mov     x2, len         /* store the string length into x2 */

        mov     x8, 64          /* write is syscall #64 */
        svc     0               /* invoke syscall */

        add     x19, x19, 1     /* increment x19 value which is loop index */
        cmp     x19, max        /* compare x19 with max value */

        b.ne    loop            /* if the value is not equal to max value, loop it again */

        mov     x0, 0           /* status -> 0 */
        mov     x8, 93          /* exit is syscall #93 */
        svc     0               /* invoke syscall */

.data
msg:    .ascii      "Hello World!:   \n"
len= .- msg
min = 0
max = 30
division=10

3. The result




























4. What I learn..

Through this lab5 I learned another assembler language Aarch64. It is similar to 6502.  I was familiar with this coding process because of 6502. Still not easy but I will try.

No comments:

Post a Comment