មេរៀនកម្មវិធី Syntax Assembly

មេរៀនកម្មវិធី Syntax Assembly

មេរៀន មូលដ្ឋានភាសា Assembly


ភាសា Assembly គឺជាភាសាសរសេរកម្មវិធីមានកម្រិតទាបសម្រាប់កុំព្យូទ័រ ឬឧបករណ៍ ផ្សេងទៀតដែលអាចសរសេរកម្មវិធីបានជាក់លាក់ដើម្បីស្ថាបត្យកម្មកុំព្យូទ័រ។ ភាសា Assembly ត្រូវបានបម្លែងទៅជាកូដម៉ាស៊ីនដែលអាចប្រតិបត្តិ កម្មវិធីដោយប្រើឧបករណ៍ ប្រើប្រាស់ កម្មវិធីនេះ ដែលសំដៅជា assembler ដូច NASM មួយ MASM ។ល។
គុណសម្បត្តិនៃភាសា Assembly:
ការយល់ដឹងនៃភាសា ការប្រជុំគ្នាមួយផ្តល់នូវចំនេះដឹង:
- Interface កម្មវិធីជាមួយនឹងប្រព័ន្ធប្រតិបត្តិការដំណើរការ processor & BIOS
- ការតំណាងនៃទិន្នន័យនៅ ក្នុងសតិនិង ឧបករណ៍ខាងក្រៅផ្សេងទៀត
- របៀបនៃការដំណើរការ និងប្រតិបត្តិការ
- របៀប ណែនាំ អំពីការចូលដំណើរការ និង ដំណើរការទិន្នន័យ
- របៀប កម្មវិធីមួយចូលដំណើរ ជាមួយ ឧបករណ៍ខាងក្រៅ
លក្ខណៈពិសេសជាមូលដ្ឋាននៃផ្នែករឹងកុំព្យូទ័រ
Processor បានគាំទ្រដល់ទំហំទិន្នន័យដូចខាងក្រោម:
  • Word: a 2-byte data item
  • Doubleword: a 4-byte (32 bit) data item
  • Quadword: an 8-byte (64 bit) data item
  • Paragraph: a 16-byte (128 bit) area
  • Kilobyte: 1024 bytes
  • Megabyte: 1,048,576 bytes
ប្រព័ន្ធលេខគោលពីរ Binary
Bit value
11111111
Position value as a power of base 2
1286432168421
Bit number
76543210
The value of a binary number is based on the presence of 1 bits and their positional value. So the value of the given binary number is: 1 + 2 + 4 + 8 +16 + 32 + 64 + 128 = 255, which is same as 28 – 1.
ប្រព័ន្ធលេខគោល១៦ Hexadecimal
Decimal number
Binary representation
Hexadecimal representation
000
111
2102
3113
41004
51015
61106
71117
810008
910019
101010A
111011B
121100C
131101D
141110E
151111F
Binary Arithmetic
The following table illustrates four simple rules for binary addition:
(i)
(ii)
(iii)
(iv)



1
0111
+0+0+1+1
=0=1=10=11
Rules (iii) and (iv) shows a carry of a 1-bit into the next left position.
ឧទាហរណ៍:
Decimal
Binary
6000111100
+4200101010
10201100110
ឧទាហរណ៍:
Number 5300110101
Reverse the bits11001010
Add 11
Number -5311001011
 ១- មេរៀន  មូលដ្ឋាន Syntax
កម្មវិធី Assembly ត្រូវបានបែងចែកជាបីផ្នែក:
  • ផ្នែក data
section .bss
  • ផ្នែក bss
section .text
global main
main:
  • ផ្នែក text
Syntax ការប្រកាស Assembly
[label]     mnemonic    [operands]   [;comment]
បន្ទាប់ពីមានឧទហរណ៍ខ្លះ សេចក្តីថ្លែងការណ៍ នៃប្រភេទភាសា Assembly:
INC COUNT  ; Increment the memory variable COUNT
MOV TOTAL, 48  ; Transfer the value 48 in the
; memory variable TOTAL
ADD AH, BH          ; Add the content of the
; BH register into the AH register
AND MASK1, 128 ; Perform AND operation on the
; variable MASK1 and 128
ADD MARKS, 10    ; Add 10 to the variable MARKS
MOV AL, 10          ; Transfer the value 10 to the AL register
កម្មវិធី Assembly បង្ហាញពាក្ស Hello World
section .text
    global  main               ; must be declared for linker (ld)
main:                          ; tells linker entry point
        mov     edx,len        ;message length
        mov     ecx,msg ;message to write
        mov     ebx,1          ;file descriptor (stdout)
        mov     eax,4          ;system call number (sys_write)
        int     0x80           ;call kernel
        mov     eax,1          ;system call number (sys_exit)
        int     0x80           ;call kernel
section .data
msg     db      'Hello, world!', 0xa   ; our dear string
len     equ     $ - msg                ; length of our dear string
លទ្ធផលចេញ: Hello, world!
មេរៀន  Memory  Segments
យើងបានពិភាក្សារួចហើយចំនួនបីផ្នែកនៃកម្មវិធី Assembly ផ្នែកទាំងនេះតំណាងឱ្យចម្រៀកសតិជាច្រើនផងដែរ។
គួរឱ្យចាប់អារម្មណ៍ប្រសិនបើអ្នកជំនួសពាក្យគន្លឹះផ្នែកជាមួយ Segment អ្នកនឹងទទួលបានលទ្ធផលដូចគ្នា។ សូមសាកល្បងកូដដូចខាងក្រោម:
segment  .text         ; code segment
    global main                ; must be declared for linker
main:                          ; tell linker entry point
        mov     edx,len        ; message length
        mov     ecx,msg ; message to write
        mov     ebx,1          ; file descriptor (stdout)
        mov     eax,4          ; system call number (sys_write)
        int     0x80           ; call kernel
        mov     eax,1          ; system call number (sys_exit)
        int     0x80           ; call kernel
segment  .data                 ; data segment
msg     db      'Hello, world!',0xa    ;our dear string
len     equ     $ - msg                ;length of our dear string
លទ្ធផលចេញ: Hello, world!
 ២- មេរៀន  Register
ប្រតិបត្ដិការ processor ដែលភាគច្រើនពាក់ព័ន្ធនឹងទិន្នន័យដែលបានដំណើរការនោះ។ ទិន្នន័យនេះអាចត្រូវបានរក្សាទុកក្នុងសតិនិងចូលដំណើរការ។ ទោះជាយ៉ាងណាការ អានទិន្នន័យពីនិងការរក្សាទុកទិន្នន័យចូលទៅក្នុងសតិ ដំណើរការយឺត នឹងដំណើរការ ដ៏ស្មុគស្មាញនៃការផ្ញើ ទិន្នន័យ តាម bus បញ្ជានិងការចូលទៅក្នុងឯកតាផ្ទុក ការចងចាំ និងការទទួលទិន្នន័យតាមរយៈ Channel ដូចគ្នា។
Processor Registers
មាន 32-bit និងប្រាំមួយ 16-bit processor registers ក្នុង IA-32។ បែងចែកជាបី ប្រភេទ
  • General registers
  • Control registers
  • Segment registers
Register ទូទៅ បែងចែកជាក្រុម:
  • Data registers
  • Pointer registers
  • Index registers
Register ទិន្នន័យ
មានបួន 32-bit ប្រភេទ data registers ត្រូវបានប្រើសម្រាប់គិត, ប្រមាណវិធី logical និងប្រមាណវិធី ផ្សេងទៀត :
  1. 32-bit data registers: EAX, EBX, ECX, EDX.
  2. ទាបជាងនៃ 32-bit registers អាចប្រើបានបួន 16-bit data registers: AX, BX, CX និង DX.
  3. ទាបជាង ឬខ្ពស់ជាង ប្រើ បួន 16-bit registers អាចប្រើបានប្រាំបី 8-bit data registers: AH, AL, BH, BL, CH, CL, DH, និង DL។
Pointer Registers
pointer registers គឺមាន 32-bit EIP, ESP និង EBP registers ហើយឆ្លើយតប 16-bit និង IP, SP និង BP។
Index Registers
32-bit index registers ESI  និង EDI and their 16-bit rightmost portions SI and DI are used for indexed addressing and sometimes used in addition and subtraction. There are two sets of index pointers:
  • Source Index (SI) – it is used as source index for string operations
  • Destination Index (DI) – it is used as destination index for string operations.

  • ឧទាហរណ៍
រកមើលនៅក្នុងកម្មវិធីដ៏សាមញ្ញដូចខាងក្រោមដើម្បីស្វែងយល់ពីការប្រើប្រាស់នៃបញ្ជីនៅក្នុងការសរសេរកម្មវិធី Assembly នេះ។  វិធីនេះបង្ហាញ 9 ផ្កាយនៅលើអេក្រង់រួមជាមួយនឹងសារដ៏សាមញ្ញមួយ។
section .text
global  main       ;must be declared for linker (gcc)
main:                          ;tell linker entry point
mov     edx,len        ;message length
mov     ecx,msg        ;message to write
mov     ebx,1          ;file descriptor (stdout)
mov     eax,4          ;system call number (sys_write)
int     0×80           ;call kernel
mov     edx,9          ;message length
mov     ecx,s2         ;message to write
mov     ebx,1          ;file descriptor (stdout)
mov     eax,4          ;system call number (sys_write)
int     0×80           ;call kernel
mov     eax,1          ;system call number (sys_exit)
int     0×80           ;call kernel
section .data
msg     db      ‘Displaying 9 stars’,0xa              ;a message
len     equ     $ – msg                               ;length of message
s2    times 9 db ‘*’
លទ្ធផលចេញ ;
Displaying 9 stars
*********
៣-មេរៀន  System Calls
ការហៅប្រព័ន្ធ APIs គឺសម្រាប់ interface ប្រើចន្លោះរវាងពីនិងចន្លោះលំហ kernel space ។ យើងបានប្រើរួចហើយប្រព័ន្ធ sys_write និង sys_exit សម្រាប់ការសរសេរ ចូលទៅ ក្នុងអេក្រង់និងចេញពីកម្មវិធីនេះរៀងគ្នា។
មាន ៦ registers ដែលផ្ទុកក្នុងអាគុយម៉ង់នៃប្រព័ន្ធ call ។  មានដូចជា EBX, ECX, EDX, ESI, EDI, និង EBP។
កូដខាងក្រោមបង្ហាញប្រព័ន្ធ system call sys_exit:
mov     eax,1          ; system call number (sys_exit)
int     0×80           ; call kernel
កូដខាងក្រោមបង្ហាញប្រព័ន្ធ system call sys_write::
mov     edx,4          ; message length
mov     ecx,msg        ; message to write
mov     ebx,1          ; file descriptor (stdout)
mov     eax,4          ; system call number (sys_write)
int     0×80           ; call kernel
តារាងបង្ហាញពី system calls តែងតែប្រើ:
%eax
Name
%ebx
%ecx
%edx
%esx
%edi
1sys_exitint----
2sys_forkstruct pt_regs----
3sys_readunsigned intchar *size_t--
4sys_writeunsigned intconst char *size_t--
5sys_openconst char *intint--
6sys_closeunsigned int---
ឧទាហរណ៍
section  .data ;   Data segment
userMsg       db ‘Please enter a number: ‘ ; Ask the user to enter a number
lenUserMsg    equ $-userMsg                      ; The length of the message
dispMsg       db ‘You have entered: ‘
lenDispMsg    equ $-dispMsg
section  .bss        ; Uninitialized data
num    resb 5
section  .text       ; Code Segment
global main
main:
;User prompt
mov eax, 4
mov ebx, 1
mov ecx, userMsg
mov edx, lenUserMsg
int 80h
; Read and store the user input
mov eax, 3
mov ebx, 2
mov ecx, num
mov edx, 5       ; 5 bytes (numeric, 1 for sign) of that information
int 80h
; Output the message ‘The entered number is: ‘
mov eax, 4
mov ebx, 1
mov ecx, dispMsg
mov edx, lenDispMsg
int 80h
; Output the number entered
mov eax, 4
mov ebx, 1
mov ecx, num
mov edx, 5
int 80h
; Exit code
mov eax, 1
mov ebx, 0
int 80h
លទ្ធផល
Please enter a number:
1234
You have entered:1234
៥- មេរៀន   អាសយដ្ឋាន Modes និង MOV
មូលដ្ឋាន mode បីនៃអាសយដ្ឋានគឺ:
  • អាសយដ្ឋាន Register
  • អាសយដ្ឋាន Immediate
  • អាសយដ្ឋាន Memory
អាសយដ្ឋាន Register ឧទាហរណ៍
MOV DX, TAX_RATE   ; Register in first operand
MOV COUNT, CX     ; Register in second operand
MOV EAX, EBX      ; Both the operands are in registers
អាសយដ្ឋាន Immediate
BYTE_VALUE  DB  150            ; A byte value is defined
WORD_VALUE  DW  300            ; A word value is defined
ADD  BYTE_VALUE,  65   ; An immediate operand 65 is added
MOV  AX, 45H           ; Immediate constant 45H is transferred to AX
អាសយដ្ឋាន Memory
ADD     BYTE_VALUE, DL ; Adds the register in the memory location
MOV     BX, WORD_VALUE ; Operand from the memory is added to register
អាសយដ្ឋាន Direct-Offset
BYTE_TABLE DB  14, 15, 22, 45    ; Tables of bytes
WORD_TABLE DW  134, 345, 564, 123 ; Tables of words
The following operations access data from the tables in the memory into registers:
MOV CL, BYTE_TABLE[2]  ; Gets the 3rd element of the BYTE_TABLE
MOV CL, BYTE_TABLE + 2 ; Gets the 3rd element of the BYTE_TABLE
MOV CX, WORD_TABLE[3]  ; Gets the 4th element of the WORD_TABLE
MOV CX, WORD_TABLE + 3 ; Gets the 4th element of the WORD_TABLE
អាសយដ្ឋាន Indirect Memory
The following code snippet shows how to access different elements of the variable.
MY_TABLE TIMES 10 DW 0 ; Allocates 10 words (2 bytes) each initialized to 0
MOV EBX, [MY_TABLE]    ; Effective Address of MY_TABLE in EBX
MOV [EBX], 110         ; MY_TABLE[0] = 110
ADD EBX, 2                     ; EBX = EBX +2
MOV [EBX], 123         ; MY_TABLE[1] = 123
ការណែនាំ MOV
Syntax of the MOV instruction is:
MOV  destination, source
The MOV instruction may have one of the following five forms:
MOV  register, register
MOV  register, immediate
MOV  memory, immediate
MOV  register, memory
MOV  memory, register
The MOV instruction causes ambiguity at times. For example, look at the statements:
MOV  EBX, [MY_TABLE]  ; Effective Address of MY_TABLE in EBX
MOV  [EBX], 110     ; MY_TABLE[0] = 110
Following table shows some of the common type specifiers:
Type Specifier
Bytes addressed
BYTE1
WORD2
DWORD4
QWORD8
TBYTE10
ឧទាហរណ៍
section .text
global  main               ;must be declared for linker (ld)
main:                  ;tell linker entry point
;writing the name ‘Zara Ali’
mov     edx,9          ;message length
mov     ecx, name      ;message to write
mov     ebx,1          ;file descriptor (stdout)
mov     eax,4          ;system call number (sys_write)
int     0×80           ;call kernel
mov     [name],  dword ‘Nuha’    ; Changed the name to Nuha Ali
; writing the name ‘Nuha Ali’
mov     edx,8   ;message length
mov     ecx,name       ;message to write
mov     ebx,1   ;file descriptor (stdout)
mov     eax,4   ;system call number (sys_write)
int     0×80    ;call kernel
mov     eax,1   ;system call number (sys_exit)
int     0×80    ;call kernel
section .data
name    db      ‘Zara Ali ‘
លទ្ធផល
Zara Ali Nuha Ali
មេរៀន ការប្រកាស អថេរ

syntax សម្រាប់ផ្ទុក allocation:
[variable-name]    define-directive    initial-value   [,initial-value]…
There are five basic forms of the define directive:
Directive
Purpose
Storage Space
DBDefine Byteallocates 1 byte
DWDefine Wordallocates 2 bytes
DDDefine Doublewordallocates 4 bytes
DQDefine Quadwordallocates 8 bytes
DTDefine Ten Bytesallocates 10 bytes
Following are some examples of using define directives:
choice         DB      ‘y’
number         DW      12345
neg_number     DW      -12345
big_number     DQ      123456789
real_number1   DD      1.234
real_number2   DQ      123.456
The following program shows use of the define directive:
section .text
global main ;must be declared for linker (gcc)
main:                  ;tell linker entry point
mov     edx,1          ;message length
mov     ecx,choice     ;message to write
mov     ebx,1          ;file descriptor (stdout)
mov     eax,4          ;system call number (sys_write)
int     0×80           ;call kernel
mov     eax,1          ;system call number (sys_exit)
int     0×80           ;call kernel
section .data
choice DB ‘y’
លទ្ធផល​  y
Multiple Definitions
លោកអ្នកមានទិន្នន័យច្រើ data  ប្រកាសក្នុងកម្មវិធីមួយ ឧទាហរណ៍
choice    DB    ‘Y’            ; ASCII of y = 79H
number1   DW    12345          ; 12345D = 3039H
number2   DD   12345679       ; 123456789D = 75BCD15H
Multiple Initializations
marks  TIMES  9  DW  0
The following program displays 9 asterisks on the screen:
section .text
global main                ;must be declared for linker (ld)
main:                          ;tell linker entry point
mov     edx,9          ;message length
mov     ecx, stars     ;message to write
mov     ebx,1          ;file descriptor (stdout)
mov     eax,4          ;system call number (sys_write)
int     0×80           ;call kernel
mov     eax,1          ;system call number (sys_exit)
int     0×80           ;call kernel
section .data
stars    times 9 db ‘*’
លទ្ធផល  *********
៦- មេរៀន  Constants
មានបីប្រភេទ
  • EQU
  • %assign
  • %define
EQU
constant_name EQU expression
ឧទាហរណ៍
total_students equ 50
ប្រើ constant value ក្នុងកូដ
mov  ecx,  total_students
cmp  eax,  total_students
ការប្រកាស EQU statement
length equ 20
width  equ 10
area   equ length * width
ឧទាហរណ៍
SYS_EXIT  equ 1
SYS_WRITE equ 4
STDIN     equ 0
STDOUT    equ 1
section .text
global main         ;must be declared for using gcc
main:   ;tell linker entry point
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg1
mov edx, len1
int 0×80
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg2
mov edx, len2
int 0×80
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg3
mov edx, len3
int 0×80
mov     eax,SYS_EXIT   ;system call number (sys_exit)
int     0×80    ;call kernel
section .data
msg1    db      ‘Hello, programmers!’,0xA,0xD
len1    equ     $ – msg1
msg2    db      ‘Welcome to the world of,’, 0xA,0xD
len2    equ    $ – msg2
msg3    db     ‘Linux assembly programming! ‘
len3    equ    $- msg3
លទ្ធផលចេញ
Hello, programmers!
Welcome to the world of,
Linux assembly programming!
%assign Directive
%assign total 10
Later in the code you can redefine it as:
%assign  total  20
%define Directive
ឧទាហរណ៍
%define ptr [EBP+4]

Related product you might see:

Share this product :

Post a Comment

 
Support : Creating Website | Johny Template | Mas Template
Copyright © 2011. ទឹកអំពៅ - All Rights Reserved
Template Created by Creating Website Published by Mas Template
Proudly powered by Blogger