Week 8: Lab 5 continued

Hello everyone and welcome to my blog!

During this week we continue to study assembly language. We practice programming in assembly on x86_64 and AArch64. In this blog I will talk about my progress on lab 5. Here is the link for the description of the lab 64-bit Assembler Lab

In this lab we had to code two assembly programs – one for an AArch64 machine and another for x86_64 machine – which would loop through the numbers 0-29. As a starting point we were given the assembly code to print “Hello, World!” for each architecture. Then we changed that code to make the loop. The code for the AArch64 loop is as follows:

.text 
.globl _start 

min = 0 
max = 30
 
_start: 
    mov x19, min 
    mov x20, 10 

loop: 
    udiv x22, x19, x20 
    msub x23, x20, x22, x19 
    add w22, w22, 48 
    add w23, w23, 48 
    mov x0, 1 
    adr x1, msg 
    mov x2, len
    cmp x22, 48
    b.eq skipzero 
    adr x21, msg+6 
    strb w22, [x21] 

skipzero: 
    adr x21, msg+7
    strb w23,[x21] 
    mov x8, 64 
    svc 0 
    add x19, x19, 1 
    cmp x19, max
    b.ne loop 
    mov x0, 0 
    mov x8, 93 
    svc 0 

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

In the program above, in the loop we store the constant “min” in register x19 for the start, add 1 to x19, then compare the content of x19 to the constant “max”. It conducts a division operation at the start of the loop using udiv and storing the results in x22. Next we can calculate the remainder with msub. After the remainder is stored in x23. Then we have the two digitals that make up each number to be printed out. Next after we add 48 to each of these digits, they now have the ASCII values for each digit.

What is more, “msg” consist of the word “Loop:” together with several spaces and a '\n' character. We need the spaces, as this is where we are going to insert our digits. Since we are going to remove 0s, the code checks to see if the current value at x22, the 10s digit, is equal to 0. Then if true the space is moved to skipzero, and only the 1s digit is drawn. If not true, the 6th position of msg is loaded into x21, and the ASCII value stored at x22 is inserted into the message. The same process continues for the 7th position of message, and the loop reaches counter. Also we used opcodes that allow the OS to take control and write the message from the loop to the console. For example, mov x0, 1 (write to stdout), adr x1, msg (save the address of the first character of the message in x1), mov x2, len (move the length of the message to x2), mov x8, 64 (write 64 to the syscall number) and svc 0 (invoke the syscall).

.text 
.globl
 
_start 
    min = 0 
    max = 30 

_start: 
    mov $min, %r8 
    mov $10, %r9 

loop: 
    mov %r8, %r15 
    mov %r15, %rax 
    mov $0, %rdx 
    div %r9
    mov %rdx, %r10 
    add $48, %r10 
    mov %r10b, msg+7 
    cmp $0, %rax 
    je skipzero 
    mov %rax,%r10 
    add $48, %r10 
    mov %r10b, msg+6 

skipzero: 
    mov $len,%rdx
    mov $msg,%rsi 
    mov $1,%rdi 
    mov $1,%rax 
    syscall 
    inc %r8 
    cmp $max, %r8 
    jne loop 
    mov $0,%rdi 
    mov $60,%rax
    syscall 

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

The code above generates an identical result on the x86_64 system. The most visible difference between the two programs is the percent symbols to access and use the registers. Many features of the code above are similar to the first version of the program but with different opcodes. There is also a big difference in how the division is done. To conduct division in first program we needed one calculation for division and another for finding the remainder, this code uses one command (div) to do both. 

Conclusions

To sum everything up, I believe that background in 6502 has helped us a lot in working on this lab. Aarch64 and x86_64 assembly languages seem to be more logical to me. Also I feel that it is easier to work with Aarch64 and x86_64 rather than with 6502. These assembly languages have many more opcodes and arguments. Overall, I feel that my experience in this lab was good.


Comments