DEV Community

Qzhang125
Qzhang125

Posted on • Edited on

Week10 Lab 5: Assembler Lab

Welcome back to week 10 of SPO600(Software Portability and Optimization) blog. This week we will work on lab 5. In this lab, we are going to write a simple program to make a loop that will run 30 times and display a number from 0 to 30 in AArch64 and x86_64 systems.

x86_64 system

.text
.globl  _start


_start:

        mov     $0, %r15                        /* Loop counter */
        mov     $0x30, %r12                     /* value of 0 in Ascii */

loop:
        mov     $0, %rdx                        /* clearing reminder for division */
        mov     %r15, %rax                      /* set rax to be divide */
        mov     $10, %r10                       /* set divisor */
        div     %r10                            /* divide */
        mov     %rax, %r14                      /* store quotient */
        mov     %rdx, %r13                      /* store remainder */

        add     $0x30, %r14                     /* quotient to ascii */
        add     $0x30, %r13                     /* remainder to ascii */
        mov     %r13b, msg+7                    /* Modify 1 byte inmsg with remainder */

        cmp     %r12, %r14
        mov     %r14b, msg+6                    /* Modify 1 byte in msg with quotient */

        mov     $len, %rdx                      /* message length */
        mov     $msg, %rsi                      /* message location */
        mov     $1, %rdi                                /* file descriptor stdout */
        mov     $1, %rax                                /* syscall sys_write */
        syscall

        inc     %r15                            /* increment counter */
        cmp     $31, %r15                               /* see if we're done */
        jne     loop                            /* if not, loop */

        mov     $0, %rdi                                /* exit status */
        mov     $60, %rax                       /* syscall sys_exit */
        syscall

.section .data

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

Enter fullscreen mode Exit fullscreen mode

AArch64 system

.text
.globl _start
_start:

        mov     x4, 0           /* file descriptor: 1 is stdout */
        mov     w10, 0x3        /* Value of 0 in ascii */

loop:
        add     w24, w4, 0x30   /* Converting iterator to ascii  */
        mov     x11, 10         /* Using 10 as a divider  */
        udiv    x12, x4, x11    /* Getting equitent  */
        msub    x13, x11, x12, x4       /* Getting the remainder  */

        add     w14, w12, 0x30  /* Ascii conversion  */
        add     w15, w13, 0x30  /* Ascii conversion  */

        adr     x16, msg        /* Storing the message  */
        strb    w15, [x16, 7]   /* Storing remainder into msg at byte 7  */
        cmp     w14, w10        /* Is 0  */
        strb    w14, [x16, 6]   /* Storing quotient in msg at byte 6  */

        mov     x0, 1           /* file descriptor  */
        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     x4, x4, 1
        cmp     x4, 31          /* Checks if the iterator equals 31  */
        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
Enter fullscreen mode Exit fullscreen mode

The result is same on both systems:
Image description

Reflection

This lab is the first lab that we actually step on the programming of x86_64 and AArch64 systems. It is considerably hard because my group and I are completely unfamiliar with it and we had no idea how to do programming with it. However, it is fun to see the difference between the 6502 assembly language and the x86_64 and AArch64 assembly language. They are quite different but as an assembly language, it is not hard to find something similar from the coding style and programming logic, I will definitely explore more about in the future.

Top comments (0)