Friday 9 February 2018

Experiment with Assembler on the x86_64 and aarch64 platforms.

At  our system Previously, gathering language, we were specifically modifying those "bare metal" equipment. This implies that a number of the compile-time Also run-time checks, slip messages, Furthermore diagnostics that would display in other dialects would not accessible. The machine will take after your guidelines exactly, regardless of they need aid totally bad (like executing data), What's more when something dives wrong, your system won't end until it tries should do something That's not permitted, for example, execute a invalid opcode or endeavor on right An ensured alternately unmapped district for memory. When that happens, those cpu will sign an exception, Also By and large the working framework will close down the culpable procedure.

X86 is a crew for backward-compatible direction book set architectures dependent upon those Intel cpu What's more its Intel variant. Likewise a fully 16-bit development about Intel's 8-bit-based microprocessor, for memory division Concerning illustration an answer to tending to more memory over a chance to be secured by a plain 16-bit deliver.

New direction book set, A64. Need 31 universally useful 64-bit registers. Need committed zero alternately stack pointer register contingent upon direction book. The system counter may be no more straightforwardly receptive Likewise a register. Guidelines are still 32 odds in length What's more basically the same Likewise A32. Need matched loads/stores. No predication for practically educational. The majority educational make 32-bit or 64-bit contentions.
Addresses expected will make 64-bit.

(1) Our first step is to build 3 C versions of the Hello world program for x86_64
# Portable C versions: # write() version
                        # syscall() wrapper version
                        # printf() version
For example: Compiling code gcc -o hello hello.c 
Running the code ./hello
Use the objdump -d name of file command to dump (print) the object code (machine code)  
In Write version we need to print the string using write. In syscall() wrapper version we need to print the string using syscall and in printf version using printf(). After building and running the code for all three version we will notice a bit difference in amount of code. Write version has basically 185 line and amount of code in main is slightly big than printf while printf has 183 lines and has least amount of code in main. Amount of code in main for syscall version is highest compared to both the versions while number of lines in syscall is 187
 Output should be similar to this in x86_64 architecture

183 -lines x86_64 in total
0000000000400594 <main>:
  400594:       a9bf7bfd        stp     x29, x30, [sp, #-16]!
  400598:       910003fd        mov     x29, sp
  40059c:       90000000        adrp    x0, 400000 <_init-0x418>
  4005a0:       9119c000        add     x0, x0, #0x670
  4005a4:       97ffffb7        bl      400480 <printf@plt>////printf()
  4005a8:       52800000        mov     w0, #0x0                        // #0
  4005ac:       a8c17bfd        ldp     x29, x30, [sp], #16
  4005b0:       d65f03c0        ret
  4005b4:       00000000        .inst   0x00000000 ; undefined

 185-lines in total
0000000000400594 <main>:
  400594:       a9bf7bfd        stp     x29, x30, [sp, #-16]!
  400598:       910003fd        mov     x29, sp
  40059c:       90000000        adrp    x0, 400000 <_init-0x418>
  4005a0:       9119e000        add     x0, x0, #0x678
  4005a4:       d28001a2        mov     x2, #0xd                        // #13
  4005a8:       aa0003e1        mov     x1, x0
  4005ac:       52800020        mov     w0, #0x1                        // #1
  4005b0:       97ffffb0        bl      400470 <write@plt>////Write()
  4005b4:       52800000        mov     w0, #0x0                        // #0
  4005b8:       a8c17bfd        ldp     x29, x30, [sp], #16
  4005bc:       d65f03c0        ret

 187-lines in total
0000000000400594 <main>:
  400594:       a9bf7bfd        stp     x29, x30, [sp, #-16]!
  400598:       910003fd        mov     x29, sp
  40059c:       90000000        adrp    x0, 400000 <_init-0x418>
  4005a0:       911a0000        add     x0, x0, #0x680
  4005a4:       528001a3        mov     w3, #0xd                        // #13
  4005a8:       aa0003e2        mov     x2, x0
  4005ac:       52800021        mov     w1, #0x1                        // #1
  4005b0:       d2800800        mov     x0, #0x40                       // #64
  4005b4:       97ffffb3        bl      400480 <syscall@plt>////syscall()
  4005b8:       52800000        mov     w0, #0x0                        // #0
  4005bc:       a8c17bfd        ldp     x29, x30, [sp], #16
  4005c0:       d65f03c0        ret
  4005c4:       00000000        .inst   0x00000000 ; undefined



(2) Our second step would be to Review, build, and run the aarchie64 assembly language programs. Take a look at the code using objdump -d objectfile and compare it to the source code

For example: Compiling code gcc -o hello hello.c 
Running the code ./hello

.text
.globl _start
_start:

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

        mov     x0, 0           /* status -> 0 */
        mov     x8, 93          /* exit is syscall #93 */
        svc     0               /* invoke syscall */

.data
msg:    .ascii      "Hello, world!\n"
len=    . - msg


Use the objdump -d name of file command to dump  the object code 

hello.o:     file format elf64-littleaarch64


Disassembly of section .text:

0000000000000000 <_start>:
   0:   d2800020        mov     x0, #0x1                        // #1
   4:   10000001        adr     x1, 0 <_start>
   8:   d28001c2        mov     x2, #0xe                        // #14
   c:   d2800808        mov     x8, #0x40                       // #64
  10:   d4000001        svc     #0x0
  14:   d2800000        mov     x0, #0x0                        // #0
  18:   d2800ba8        mov     x8, #0x5d                       // #93
  1c:   d4000001        svc     #0x0
 

The goal for this lab may be with execute a basic c circle composed Previously, both x86_64 What's more aarch64 constructing agent dialect. We were given an void x86_64  piece  which loops from 0 will 9, utilizing r15 Similarly as those list (loop control) counter.
Below code is  EMPTY BLOCK


 text
.globl    _start

start = 0                       /* starting value for the loop index; note that this  constant, not a variable */
max = 10                        /* loop exits when the index hits this number loop condition is i<max */

_start:
    mov     $start,%r15         /* loop index */

loop:
    /* ... body of the loop ... do something useful here ... */

    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


x_86_64 & aarch64 Loops


In order to dissemble a binary file, we can use the CL tool objdump -d. When we dissemble a binary, we can examine that specific machines assembler instruction set.

x86_64 9 Iteration Loop Solution
.text
.global        _start

start = 0
max = 10

_start:
    mov         $start,%r15

loop:
    mov         $len,%rdx
    mov         $msg,%rsi
    mov         $1,%rdi
    mov         $1,%rax
    syscall

    inc         %r15
    mov         %r15,%r14
    add         $'0',%r14
    mov         $num,%r13
    mov         %r14b,(%r13)
    cmp         $max,%r15
    jne         loop

    mov         $0,%rdi
    mov         $60,%rax
    syscall

.section .data
        msg: .ascii  "Loop: 0\n"
        len = . - msg
        num = msg + 6






x86_9_loop:     file format elf64-x86-64


Disassembly of section .text:

00000000004000b0 <_start>:
  4000b0:    49 c7 c7 00 00 00 00     mov    $0x0,%r15

00000000004000b7 <loop>:
  4000b7:    48 c7 c2 08 00 00 00     mov    $0x8,%rdx
  4000be:    48 c7 c6 00 01 60 00     mov    $0x600100,%rsi
  4000c5:    48 c7 c7 01 00 00 00     mov    $0x1,%rdi
  4000cc:    48 c7 c0 01 00 00 00     mov    $0x1,%rax
  4000d3:    0f 05                    syscall
  4000d5:    49 ff c7                 inc    %r15
  4000d8:    4d 89 fe                 mov    %r15,%r14
  4000db:    49 83 c6 30              add    $0x30,%r14
  4000df:    49 c7 c5 06 01 60 00     mov    $0x600106,%r13
  4000e6:    45 88 75 00              mov    %r14b,0x0(%r13)
  4000ea:    49 83 ff 0a              cmp    $0xa,%r15
  4000ee:    75 c7                    jne    4000b7 <loop>
  4000f0:    48 c7 c7 00 00 00 00     mov    $0x0,%rdi
  4000f7:    48 c7 c0 3c 00 00 00     mov    $0x3c,%rax
  4000fe:    0f 05                    syscall 


In spite of aarch64 gathering need an alternate punctuation over its x86_64 partner they even now have their likenesses Previously, rationale.


aarch64 9 Iteration Loop Solution
.text
.globl    _start
start = 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) */
ten = 10

_start:
        mov     x19, start
        mov     x22, ten
       
loop:
        adr     x1, msg
        mov     x2, len
        mov     x0, 1
        mov     x8, 64
        svc     0
        adr     x23, msg
        add     x19, x19, 1
        udiv    x20, x19, x22
        msub    x21, x22, x20, x19
        cmp     x20, 0
        beq     skip
        add     x20, x20, '0'
        strb    w20, [x23,6]
skip:
        add     x21, x21, '0'
        strb    w21, [x23,7]
        cmp     x19, max
        bne     loop
        mov     x0, 0
        mov     x8, 93
        svc     0
.data
        msg: .ascii  "Loop:  0\n"
        len = . - msg


aarch_9_loop:     file format elf64-littleaarch64


Disassembly of section .text:

00000000004000b0 <_start>:
  4000b0:    d2800013     mov    x19, #0x0                       // #0
  4000b4:    d2800156     mov    x22, #0xa                       // #10

00000000004000b8 <loop>:
  4000b8:    10080281     adr    x1, 410108 <msg>
  4000bc:    d2800122     mov    x2, #0x9                       // #9
  4000c0:    d2800020     mov    x0, #0x1                       // #1
  4000c4:    d2800808     mov    x8, #0x40                      // #64
  4000c8:    d4000001     svc    #0x0
  4000cc:    100801f7     adr    x23, 410108 <msg>
  4000d0:    91000673     add    x19, x19, #0x1
  4000d4:    9ad60a74     udiv    x20, x19, x22
  4000d8:    9b14ced5     msub    x21, x22, x20, x19
  4000dc:    f100029f     cmp    x20, #0x0
  4000e0:    54000060     b.eq    4000ec <skip>
  4000e4:    9100c294     add    x20, x20, #0x30
  4000e8:    39001af4     strb    w20, [x23,#6]

00000000004000ec <skip>:
  4000ec:    9100c2b5     add    x21, x21, #0x30
  4000f0:    39001ef5     strb    w21, [x23,#7]
  4000f4:    f1002a7f     cmp    x19, #0xa
  4000f8:    54fffe01     b.ne    4000b8 <loop>
  4000fc:    d2800000     mov    x0, #0x0                       // #0
  400100:    d2800ba8     mov    x8, #0x5d                      // #93
400104: d4000001 svc #0x0


To expand on our past 9 cycle circle we were tasked with growing the check with 30. Resonances straightforward sufficient. Simply increment the max quatity.With circle should a double-digit number, you need will change over those circle record number by separating it Eventually  perusing 10.

With print those circle list value, you will have on change over from a basic should digit character. On ASCII/ISO-8859-1/Unicode UTF-8, the digit characters would in the extent 48-57 (0x30-0x39). You will also requirement on amass the message to a chance to be printed for every accordance – you camwood would this by composing those digit under those message support When outputting it should stdout, which will be likely those best approach, alternately you could perform an arrangement of composes to those three parts of the message (‘Loop: ‘, number, ‘n’). You might need to allude of the `manpage` for ascii.

x86_64 30 Iteration Loop Solution
.text
.globl  _start


start = 0
max = 31
ten = 10

_start:

        mov     $start,%r15
        mov     $ten,%r14

loop:

        mov     $len,%rdx
        mov     $msg,%rsi
        mov     $1,%rdi
        mov     $1,%rax
        syscall

        inc     %r15
        mov     $0,%rdx
        mov     %r15,%rax
        div     %r14

        mov     $num,%r12
        mov     %rax,%r13
        cmp     $0,%r13
        je      skip
        add     $'0',%r13
        mov     %r13b,(%r12)
skip:
        inc     %r12
        mov     %rdx,%r13
        add     $'0',%r13
        mov     %r13b,(%r12)

        cmp     $max,%r15
        jne     loop

        mov     $0,%rdi
        mov     $60,%rax
        syscall

.section .data
        msg: .ascii  "Loop:  0\n"
        len = . - msg
        num = msg + 6

86_30_loop:     file format elf64-x86-64


Disassembly of section .text:

00000000004000b0 <_start>:
  4000b0:    49 c7 c7 00 00 00 00     mov    $0x0,%r15
  4000b7:    49 c7 c6 0a 00 00 00     mov    $0xa,%r14

00000000004000be <loop>:
  4000be:    48 c7 c2 09 00 00 00     mov    $0x9,%rdx
  4000c5:    48 c7 c6 28 01 60 00     mov    $0x600128,%rsi
  4000cc:    48 c7 c7 01 00 00 00     mov    $0x1,%rdi
  4000d3:    48 c7 c0 01 00 00 00     mov    $0x1,%rax
  4000da:    0f 05                    syscall
  4000dc:    49 ff c7                 inc    %r15
  4000df:    48 c7 c2 00 00 00 00     mov    $0x0,%rdx
  4000e6:    4c 89 f8                 mov    %r15,%rax
  4000e9:    49 f7 f6                 div    %r14
  4000ec:    49 c7 c4 2e 01 60 00     mov    $0x60012e,%r12
  4000f3:    49 89 c5                 mov    %rax,%r13
  4000f6:    49 83 fd 00              cmp    $0x0,%r13
  4000fa:    74 08                    je     400104 <skip>
  4000fc:    49 83 c5 30              add    $0x30,%r13
  400100:    45 88 2c 24              mov    %r13b,(%r12)

0000000000400104 <skip>:
  400104:    49 ff c4                 inc    %r12
  400107:    49 89 d5                 mov    %rdx,%r13
  40010a:    49 83 c5 30              add    $0x30,%r13
  40010e:    45 88 2c 24              mov    %r13b,(%r12)
  400112:    49 83 ff 1f              cmp    $0x1f,%r15
  400116:    75 a6                    jne    4000be <loop>
  400118:    48 c7 c7 00 00 00 00     mov    $0x0,%rdi
  40011f:    48 c7 c0 3c 00 00 00     mov    $0x3c,%rax
  400126:    0f 05                    syscall

Reflection.
Personally, i discovered working for low level computing construct will a chance to be testing yet all the In the same run through thick, as compensating. Those grammar might make extremely befuddling and dully with worth of effort with. I revel in Taking in over the full purpose of a procedure. All that that happens between A->B. Picking up introduction should low-level gathering modifying need provided for me exactly knowledge under how mind boggling What's more multifaceted attempting with those fittings could make. I’ve also picked up an entire new appreciation for compilers What's more IDE’s.



No comments:

Post a Comment