Tuesday 27 February 2018

Lab 3 - Loop

In this lab, I implement loops through assembly language based on x86 64 and Aarch64. The loop will expand in the "Hello world" program. "Hello world" code was written as below.
 .text  
 .globl     _start  
   
 _start:  
      movq     $len,%rdx        /* message length */  
      movq      $msg,%rsi       /* message location */  
      movq     $1,%rdi          /* file descriptor stdout */  
      movq     $1,%rax          /* syscall sys_write */  
      syscall  
   
      movq     $0,%rdi          /* exit status */  
      movq     $60,%rax         /* syscall sys_exit */  
      syscall  
   
 .section .rodata  
   
 msg:     .ascii   "Hello, world!\n"  
      len = . - msg  
The first implementation will print a number from 0 to 9 on the screen. This is the code implemented on the x86 64 platform below.
 .text  
 .globl  _start  
   
 start = 0                  /* starting value for the loop */  
 max = 10                   /* ending value of loop */  
   
 _start:  
      mov      $start,%r15     /* loop index */  
   
 loop:  
      mov      %r15,%r14    /* copy loop index */  
      add      $48,%r14                 
      mov      %r14b,msg+6                 
   
      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 */  
   
      movq     $0,%rdi      /* exit status */  
      movq     $60,%rax     /* syscall sys_exit */  
      syscall  
   
 .section .data  
   
 msg:     .ascii   "Loop: !\n"  
      len = . - msg  
The second one will display numbers from 0 to 30 on the screen. 0 to 9 will be preceded by a zero. This is the code implemented on the x86 64 platform below.
 .text  
 .globl     _start  
   
 start = 0                  /* starting value for the loop */  
 max = 31                   /* end loop number */  
   
 _start:  
      mov   $start,%r15     /* loop index */  
      mov   $0x30, %r12                 
   
 loop:  
    mov  $'0',%r14   
    mov  $10,%r13   
    mov  $0,%rdx   
    mov  %r15,%rax   
    div  %r13   
    cmp  $0,%rax   
   
    mov  %rax,%r13       
    add  %r14,%r13   
    mov  %r13,msg+6   
   
    mov  %rdx,%r12   
    add  %r14,%r12   
    mov  %r12,msg+7   
   
    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 */  
   
    movq     $0,%rdi      /* exit status */  
    movq     $60,%rax     /* syscall sys_exit */  
    syscall  
   
 .section .data  
   
 msg: .ascii "Loop:   \n"  
      len = . - msg  
Finally, we implement the same loop code as above in the aarch64 platform.
 .text  
 .globl  _start  
 start = 0  
 max = 31  
 digit = 10  
   
 _start:  
     mov   x9, start  
     mov   x22, digit  
 loop:  
     mov   x0, 0  
     adr   x1, msg  
     mov   x2, len  
   
     mov   x8, 64  
     svc   0  
   
     mov   x10,10  
     adr   x23, msg  
     udiv  x20, x19, x22  
     msub  x21, x22, x20, x19  
     cmp   x9, 10  
   
     add   x20, x20, 0x30  
     strb  w20, [x1,6]  
   
     add   x21, x21, 0x30  
     strb  w21, [x1,7]  
   
     add   x19, x19, 1  
     cmp   x19, max  
     bne   loop  
     mov   x0, 0  
     mov   x8, 93  
     svc   0  
 .data  
     msg: .ascii "Loop: 0\n"  
     len = . - msg  
The x86 64 and Aarch64 platforms should use different commands. We must specify the address value directly or indirectly with the command. Learning the assembler will give a good understanding of the computer system and structure, as well as a better understanding of memory.

No comments:

Post a Comment

SPO600 Project - Stage 3

I chose Redis (Remote Dictionary Server) for my project at stage1. Redis is open source software developed by Salvatore Sanfilippo, a volati...