lab 3 assembly language

In lab 3, we explored writing assembler programs for different architectures, including x86_64 and AArch64. We unpacked the provided code examples and built and ran C and assembler programs. The goal was to understand the differences in writing and debugging assembler code across these architectures.
Writing assembler code is a unique challenge compared to higher-level languages like C. The need to understand the hardware's instruction set and architecture is crucial. Debugging is more difficult because of the low-level nature of the assembler, where each instruction directly manipulates the CPU's state. For instance, while writing the AArch64 loop program, I had to carefully manage register values and ensure correct syscall invocations. It goes the same for the x86_64 programs with its even richer and more complex instruction set.
I think 6502 Assembler is a simple instruction set, easier to learn but limited in capabilities. x86_64 Assembler is a rich instruction set, complex but powerful, suitable for performance-critical applications. AArch64 Assembler is a modern, efficient, and consistent instruction set.

AArch64 Loop code :
.text
.globl _start
min = 0
max = 31
_start:
    mov     x19, min
loop:
    ldr     x0, =1
    ldr     x1, =msg
    ldr     x2, =7
    mov     x8, 64
    svc     0

    add     x1, x1, #7
    udiv    x20, x19, 10
    msub    x21, x20, 10, x19

    add     x20, x20, '0'
    add     x21, x21, '0'

    strb    w20, [x1]
    add     x1, x1, #1
    strb    w21, [x1]

    mov     x2, 2
    svc     0

    ldr     x1, =newline
    ldr     x2, =1
    svc     0

    sub     x19, x19, '0'
    add     x19, x19, 1
    cmp     x19, max
    b.ne    loop
    mov     x0, 0
    mov     x8, 93
    svc     0

msg: .asciz "Loop: "
newline: .asciz "\n"

x86_64 loop code:
.text
.globl _start
min = 0
max = 31
_start:
    mov     $min,%r15           /* loop index */
loop:
    /* Print "Loop: " */
    mov     $1,%rax          
    mov     $1,%rdi           
    lea     msg(%rip),%rsi    
    mov     $7,%rdx          
    syscall

    /* Print loop index */
    add     $7,%rsi           
    mov     %r15,%rbx
    mov     $10,%rax
    div     %rbx            
    add     $'0,%rdx       
    movb    %dl,(%rsi)     
    inc     %rsi
    add     $'0,%rax          
    movb    %al,(%rsi)     
    mov     $2,%rdx           
    syscall

    /* Print newline */
    lea     newline(%rip),%rsi
    mov     $1,%rdx
    syscall

    /* Increment loop index */
    inc     %r15                /* increment index */
    cmp     $max,%r15           /* see if we're done */
    jne     loop                /* loop if we're not */
    mov     $0,%rdi             /* exit status */
    mov     $60,%rax            /* syscall sys_exit */
    syscall

msg: .asciz "Loop: "
newline: .asciz "\n"

Through this lab, I gained a deeper understanding of AArch64 and x86_64 assembler.

Comments

Popular posts from this blog

Project stage 3 Integrate, Tidy, & Wrap

Project stage 1 GCC build

lab 1 mob programming