DEV Community

Tecca Yu
Tecca Yu

Posted on • Edited on

LAB4 - Aarch64 VS. X84_64

Hi this is Tecca, and in this post I will be demonstrating my findings with assembly language in the AArch64, an ARM architecture, and x86_64, an x86 architecture.

The goal of this lab is to generate output like the following in both AArch64 and x86_64:

Loop: 0
Loop: 1
Loop: 2
Loop: 3
Loop: 4
...
Loop: 30
etc.
Enter fullscreen mode Exit fullscreen mode

Source code for this lab in x86_64

.text
.globl  _start


_start:


print:  mov     $0,%r15                 /* loop index */

loop:   mov %r15,%r14
    add $'0',%r14
    movb    %r14b,msg+6

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

        inc     %r15                /* increment the index */
        cmp     $10,%r15           /* check if we're done */
        jne     loop                /* keep looping if we're not */

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

.section .data

msg:    .ascii      "Loop: #\n"
        len = . - msg
Enter fullscreen mode Exit fullscreen mode

Result:
Image description

Moving onto AArch64 source code:

.text
.globl _start

_start:

        mov     x19, 0

loop:
        add     x20, x19, '0'   // Create digit character
        adr     x17, msg+6      // Get a pointer to desired location of digit
        strb    w20, [x17]      // Put digit to desired location

        mov     x0, 1           /* file descriptor: 1 is stdout */
        adr     x1, msg         /* message location (memory address) */
        mov     x2, len         /* message length (bytes) */

        mov     x8, 64          
        svc     0               /* invoke syscall */

        add     x19, x19, 1
        cmp     x19, 10
        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

This is done from storing value defined from the given range into a register, an ASCII value is then converted from the value within the register.
The value ‘0’ is stored into a register, which in turn is converted into an ASCII value of 48 in decimal. After the loop incremented it to 10, the program would not print an integer character as expected, there is no associated ASCII reference for '10'.

I've tried adding 10 to the ASCII value of ‘0’, the resulting ASCII value prints out a colon “:” instead of the expected number 10 because 48+10=58 and 58 is a ":".
Image description

I'll focus on making it loop past 10 and onward in the next post.

Top comments (0)