10주 차 실습 5: 어셈블러 실습

4049 단어
SPO600(Software Portability and Optimization) 블로그 10주차에 오신 것을 환영합니다. 이번 주에는 랩 5에서 작업할 것입니다. 이 랩에서는 AArch64 및 x86_64 시스템에서 30회 실행되고 0에서 30까지의 숫자를 표시하는 루프를 만드는 간단한 프로그램을 작성합니다.

x86_64 시스템




.text
.globl  _start


_start:

        mov     $0, %r15                        /* Loop counter */
        mov     $0x30, %r12                     /* value of 0 in Ascii */

loop:
        mov     $0, %rdx                        /* clearing reminder for division */
        mov     %r15, %rax                      /* set rax to be divide */
        mov     $10, %r10                       /* set divisor */
        div     %r10                            /* divide */
        mov     %rax, %r14                      /* store quotient */
        mov     %rdx, %r13                      /* store remainder */

        add     $0x30, %r14                     /* quotient to ascii */
        add     $0x30, %r13                     /* remainder to ascii */
        mov     %r13b, msg+7                    /* Modify 1 byte inmsg with remainder */

        cmp     %r12, %r14
        mov     %r14b, msg+6                    /* Modify 1 byte in msg with quotient */

        mov     $len, %rdx                      /* message length */
        mov     $msg, %rsi                      /* message location */
        mov     $1, %rdi                                /* file descriptor stdout */
        mov     $1, %rax                                /* syscall sys_write */
        syscall

        inc     %r15                            /* increment counter */
        cmp     $31, %r15                               /* see if we're done */
        jne     loop                            /* if not, loop */

        mov     $0, %rdi                                /* exit status */
        mov     $60, %rax                       /* syscall sys_exit */
        syscall

.section .data

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



AArch64 시스템




.text
.globl _start
_start:

        mov     x4, 0           /* file descriptor: 1 is stdout */
        mov     w10, 0x3        /* Value of 0 in ascii */

loop:
        add     w24, w4, 0x30   /* Converting iterator to ascii  */
        mov     x11, 10         /* Using 10 as a divider  */
        udiv    x12, x4, x11    /* Getting equitent  */
        msub    x13, x11, x12, x4       /* Getting the remainder  */

        add     w14, w12, 0x30  /* Ascii conversion  */
        add     w15, w13, 0x30  /* Ascii conversion  */

        adr     x16, msg        /* Storing the message  */
        strb    w15, [x16, 7]   /* Storing remainder into msg at byte 7  */
        cmp     w14, w10        /* Is 0  */
        strb    w14, [x16, 6]   /* Storing quotient in msg at byte 6  */

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

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

        add     x4, x4, 1
        cmp     x4, 31          /* Checks if the iterator equals 31  */
        b.ne    loop

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

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


결과는 두 시스템에서 동일합니다.


반사



이 실습은 x86_64 및 AArch64 시스템의 프로그래밍을 실제로 밟는 첫 번째 실습입니다. 우리 그룹과 나는 완전히 익숙하지 않고 프로그래밍 방법을 몰랐기 때문에 상당히 어렵습니다. 그러나 6502 어셈블리 언어와 x86_64 및 AArch64 어셈블리 언어의 차이점을 보는 것은 재미있습니다. 그것들은 꽤 다르지만 어셈블리 언어로서 코딩 스타일과 프로그래밍 논리에서 비슷한 것을 찾는 것은 어렵지 않습니다. 앞으로 더 자세히 알아볼 것입니다.

좋은 웹페이지 즐겨찾기