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

Welcome to part 2 of lab 4. This lab goes over some basics of 64-bit assembly languages, specifically in the AArch64 and x86_64 platforms. This specific part will be going over the first half of the lab in the x86_64 system. Links for the other parts are listed here:

Part 1 - First half in AArch64

Part 3 - Second half in AArch64

Part 4 - Second half in x86_64

I'll be outlining the steps in detail as I go along but the full instructions for this lab can be found here.

First we are given a sample loop code that loops 10 times, and a hello world function. We are instructed to combine those two codes to make a function that prints prints Loop 10 times. I had to make sure max was set to 10, and that msg was Loop\n. The final combined code is shown 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:


print:    

    mov     $min,%r15           /* loop index */


loop:

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


.section .data


msg: .ascii "Loop\n"

len = . - msg

And the output of the code is 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 most important part of code for replacing that placeholder is this:

//add digit to message

    mov     %r15,%r14

    add     $'0',%r14

    movb    %r14b,msg+6

This code is right at the beginning of the loop, and what it does first, is move the index value from register 15 to register 14, and in register 14 I add the ascii value of '0' to convert the digit to a character value. Then I simply insert that character into the msg, overriding the placeholder #. The full code is offered 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:


print:    

    mov     $min,%r15           /* loop index */


loop:

//add digit to message

    mov     %r15,%r14

    add     $'0',%r14

    movb    %r14b,msg+6


//print message

    movq    $len,%rdx           /* message length  */

    movq    $msg,%rsi           /* message location  */ 

    movq    $1,%rdi             /* file descriptor stdout  */

    movq    $1,%rax             /* syscall sys_write  */

    syscall


//continue loop

    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


.section .data


msg: .ascii "Loop: #\n"

len = . - msg

First half summary:

Having now completed the first half of the lab in AArch64 and x86_64, I surprising find x86_64 easier. Before doing this lab I found the AArch64 syntax more intuitive, especially with things like the add instruction. AArch64's add works more intuitively as a x+y = z but x86_64 is more like x+=y, which is a bit annoying. But oddly after working with it I find x86_64 a bit cleaner. Otherwise, for this first half, not many more thoughts to add. I'll go into more depth at the end of part 4. Also for the next part, part 3, click here.




Comments

Popular posts from this blog

SPO600 - Final Thoughts

SPO600 Project - Stage 1: Selection

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