Lab 4 - 64-bit Assembly Language Lab - Part 1

Hello, I'm back again for lab 4 of my Software Portability and Optimization course. The core concept of this lab will be to explore 64-bit assembly language through the AArch64 and x86_64 platforms. I'll be outlining the steps in detail as I go along but as before the instructions for this lab can be found here.

This first part will focus on the first half of the lab in AArch64. Links for the other parts are listed here:

Part 2 - First half in x86_64

Part 3 - Second half in AArch64

Part 4 - Second half in x86_64

First we are given this example AArch64 assembler code, which is just a simple empty loop. We are then instructed to modify the code by adding a hello world code (also provided) into the body of the loop so that the code prints Loop 10 times. I had to make sure max was set to 10, and that msg was Loop\n

.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:

//Print Message

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

        mov     x8, 64          /* write is syscall #64 */
        svc     0               /* invoke syscall */

//Proceed with loop

        add     x19, x19, 1
        cmp     x19, max
        b.ne    loop
        
//Exit

        mov     x0, 0           /* status -> 0 */
        mov     x8, 93          /* exit is syscall #93 */
        svc     0               /* invoke syscall */
        
.data
msg:    .ascii  "Loop\n"
len=    . - msg

The output of the code is like so:

Loop
Loop
Loop
Loop
Loop
Loop
Loop
Loop
Loop
Loop

The next step is to change the output so each printed line also includes the loop index value, like so:

Loop: 0
Loop: 1
Loop: 2
Loop: 3
Loop: 4
Loop: 5
Loop: 6
Loop: 7
Loop: 8
Loop: 9

To do this, I first change msg to Loop: #\n. The # is a placeholder character that I will replace in the code with a digit. The snippet of code for replacing that placeholder is this:

//Prepare message by inserting digit

        add     x18, x19, '0'  //Create digit character
        adr     x17, msg+6     //Pointer for where we want to insert digit
        strb    w18, [x17]     //Insert the digit

This code is right at the beginning of the loop, and what it does first is add the ascii value for 0 to the index value and store it in x18. Since the ascii values for 0-9 are all consecutive, this will always give me the index value as a character. I then make a pointer for the location of the placeholder # character in the message by adding 6 to msg (as # is the 6th character in the message). I then insert our newly created digit into the placeholders spot. This provides me with the desire output from above. The full code is displayed below:

.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:

//Prepare message by inserting digit

        add     x18, x19, '0'  //Create digit character
        adr     x17, msg+6     //Pointer for where we want to insert digit
        strb    w18, [x17]     //Insert the digit

//Print Message

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

        mov     x8, 64          /* write is syscall #64 */
        svc     0               /* invoke syscall */

//Proceed with loop

        add     x19, x19, 1
        cmp     x19, max
        b.ne    loop
        
//Exit

        mov     x0, 0           /* status -> 0 */
        mov     x8, 93          /* exit is syscall #93 */
        svc     0               /* invoke syscall */
        
.data
msg:    .ascii  "Loop: #\n"
len=    . - msg

For the next part of the lab where I do these same steps in x86_64, click here.


Comments

Popular posts from this blog

SPO600 Project - Stage 1: Selection

SPO600 - Final Thoughts

Lab 4 - 64-bit Assembly Language Lab - Part 4