64-Bit Assembly Language Lab
After successfully setting up the servers, namely
AArch64 and x86_64.
Step 1 involved reviewing AArch64 assembly code, including examining the code with objdump to understand the correspondence between source code and machine instructions
Step 2 introduced a basic loop structure in AArch64 assembly, looping from 0 to 9 using register x19 as the index counter. The code provided a skeleton loop structure but did not perform any meaningful actions within the loop.
.text
.globl _start
min = 0 /* starting value for the loop index; **note that this is a symbol (constant)**, not a variable */
max = 10 /* loop exits when the index hits this number (loop condition is i<max) */
_start:
mov x19, min
loop:
/* ... body of the loop ... do something useful here ... */
add x19, x19, 1
cmp x19, max
b.ne loop
mov x0, 0 /* status -> 0 */
mov x8, 93 /* exit is syscall #93 */
svc 0 /* invoke syscall */
The code to include loop index values, showing each digit from 0 to 30 in AArch64 assembly language:
.text
.globl _start
start = 0 /* starting value for the loop index; note that this is a symbol (constant), not a variable */
max = 31 /* loop exits when the index hits this number (loop condition is i<max) */
_start:
mov x20,start /* loop index */
loop:
mov x22,10
udiv x23,x20,x22 /* divider */
msub x24,x22,x23,x20 /* remainder */
cmp x23,0 /* see if quotient is greater than 0 */
b.eq msgChanger
/* print divider */
add x23,x23,48
adr x26,msg+6
strb w23,[x26]
msgChanger:
add x24,x24,48 /* added r24+48 to r24 */
adr x27,msg+7 /* because it does not have immediate mode */
strb w24,[x27] /* added number after 'Loop' */
mov x2,len /* message length */
adr x1,msg /* message location */
mov x0,1 /* file descriptor stdout */
mov x8,64 /* syscall sys_write */
svc 0
add x20,x20,1 /* increment index */
cmp x20,max /* see if we're done */
b.ne loop /* loop if we're not */
mov x0,0 /* exit status */
mov x8,93 /* syscall sys_exit */
svc 0
.section .data
msg: .ascii "Loop: \n"
len = . - msg
The code to include loop index values, showing each digit from 0 to 30 in x86_64:
.section .text
.global _start
.equ start, 0 /* starting value for the loop index */
.equ max, 31 /* loop exits when the index hits this number (loop condition is i<max) */
_start:
mov $start, %rax /* loop index */
.loop:
mov $10, %rbx /* divisor */
xor %rdx, %rdx /* clear the upper part of the dividend */
div %rbx /* divide %rax by %rbx, quotient in %rax, remainder in %rdx */
cmp $0, %rax /* see if quotient is greater than 0 */
je .msgChanger /* jump if quotient is 0 */
/* print quotient */
add $48, %rax /* convert to ASCII */
mov %al, msg+6(%rip) /* store ASCII character in message */
.msgChanger:
add $48, %rdx /* convert remainder to ASCII */
mov %dl, msg+7(%rip) /* store ASCII character in message */
mov $len, %rdx /* message length */
lea msg(%rip), %rsi /* message location */
mov $1, %rdi /* file descriptor stdout */
mov $1, %rax /* syscall number for sys_write */
syscall /* invoke syscall */
add $1, %rax /* increment index */
cmp $max, %rax /* see if we're done */
jne .loop /* loop if we're not */
mov $0, %rdi /* exit status */
mov $60, %rax /* syscall number for sys_exit */
syscall /* invoke syscall */
.section .data
msg: .ascii "Loop: \n"
.set len, . - msg
In conclusion, delving into AArch64 and x86_64 assembly languages provided a profound understanding of low-level programming concepts and intricacies. Through practical exercises, I gained insights into loop structures, register usage, and system calls specific to each architecture. Debugging assembly code offered a unique challenge, requiring meticulous attention to detail and a deep understanding of hardware-level operations. Comparing 6502, x86_64, and AArch64 assembly languages highlighted their respective strengths and complexities. While 6502 felt rudimentary, x86_64 and AArch64 offered more sophisticated features and optimizations. Writing in assembly illuminated the inner workings of computer systems, fostering a deeper appreciation for higher-level languages' abstractions. Despite its complexity, mastering assembly language empowers programmers to optimize performance-critical code and gain a deeper understanding of computer architecture. Overall, the experience underscored the importance of understanding low-level programming for building efficient and robust software systems.
Comments
Post a Comment