Latest Products
Showing posts with label មេរៀនកម្មវិធី Assembly. Show all posts
Showing posts with label មេរៀនកម្មវិធី Assembly. Show all posts
មេរៀនកម្មវិធី Syntax Assembly (ត)

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

៧- មេរៀន  Arithmetic Instructions
INC Instruction
INC destination
The operand destination could be an 8-bit, 16-bit or 32-bit operand.
ឧទាហរណ៍
INC EBX     ; Increments 32-bit register
INC DL      ; Increments 8-bit register
INC [count]    ; Increments the count variable
DEC Instruction
DEC destination
The operand destination could be an 8-bit, 16-bit or 32-bit operand.
ឧទាហរណ៍
segment .data
count dw       0
value db       15
segment .text
inc [count]
dec     [value]
mov     ebx, count
inc word [ebx]
mov esi, value
dec byte [esi]
ADD និង SUB Instructions
syntax:
ADD/SUB destination, source
The ADD/SUB instruction can take place between:
  • Register to register
  • Memory to register
  • Register to memory
  • Register to constant data
  • Memory to constant data
SYS_EXIT  equ 1
SYS_READ  equ 3
SYS_WRITE equ 4
STDIN     equ 0
STDOUT    equ 1
segment .data
msg1 db “Enter a digit “, 0xA,0xD
len1 equ $- msg1
msg2 db “Please enter a second digit”, 0xA,0xD
len2 equ $- msg2
msg3 db “The sum is: ”
len3 equ $- msg3
segment .bss
num1 resb 2
num2 resb 2
res resb 1
segment .text
global main
main:
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg1
mov edx, len1
int 0×80
mov eax, SYS_READ
mov ebx, STDIN
mov ecx, num1
mov edx, 2
int 0×80
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg2
mov edx, len2
int 0×80
mov eax, SYS_READ
mov ebx, STDIN
mov ecx, num2
mov edx, 2
int 0×80
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg3
mov edx, len3
int 0×80
; moving the first number to eax register and second number to ebx
; and subtracting ascii ’0′ to convert it into a decimal number
mov eax, [number1]
sub eax, ’0′
mov ebx, [number2]
sub ebx, ’0′
; add eax and ebx
add eax, ebx
; add ’0′ to to convert the sum from decimal to ASCII
add eax, ’0′
; storing the sum in memory location res
mov [res], eax
; print the sum
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, res
mov edx, 1
int 0×80
exit:
mov eax, SYS_EXIT
xor ebx, ebx
int 0×80
លទ្ធផល
Enter a digit:
3
Please enter a second digit:
4
The sum is:
7
The program with hardcoded variables:
section .text
global main         ;must be declared for using gcc
main:   ;tell linker entry point
mov     eax,’3′
sub     eax, ’0′
mov     ebx, ’4′
sub     ebx, ’0′
add     eax, ebx
add     eax, ’0′
mov     [sum], eax
mov     ecx,msg
mov     edx, len
mov     ebx,1   ;file descriptor (stdout)
mov     eax,4   ;system call number (sys_write)
int     0×80    ;call kernel
nwln
mov     ecx,sum
mov     edx, 1
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 “The sum is:”, 0xA,0xD
len equ $ – msg
segment .bss
sum resb 1
លទ្ធផល
The sum is:
7
MUL/IMUL Instruction
syntax សម្រាប់ instructions:
MUL/IMUL multiplier
ឧទាហរណ៍
MOV AL, 10
MOV DL, 25
MUL DL

MOV DL, 0FFH   ; DL= -1
MOV AL, 0BEH   ; AL = -66
IMUL DL
ឧទាហរណ៍
section .text
global main         ;must be declared for using gcc
main:   ;tell linker entry point
mov     al,’3′
sub     al, ’0′
mov     bl, ’2′
sub     bl, ’0′
mul     bl
add     al, ’0′
mov     [res], al
mov     ecx,msg
mov     edx, len
mov     ebx,1   ;file descriptor (stdout)
mov     eax,4   ;system call number (sys_write)
int     0×80    ;call kernel
nwln
mov     ecx,res
mov     edx, 1
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 “The result is:”, 0xA,0xD
len equ $- msg
segment .bss
res resb 1
លទ្ធផល
The result is:
6
DIV/IDIV Instructions
ទម្រង់នៃ DIV/IDIV instruction:
DIV/IDIV       divisor
ឧទាហរណ៍
section .text
global main         ;must be declared for using gcc
main:   ;tell linker entry point
mov     ax,’8′
sub     ax, ’0′
mov     bl, ’2′
sub     bl, ’0′
div     bl
add     ax, ’0′
mov     [res], ax
mov     ecx,msg
mov     edx, len
mov     ebx,1   ;file descriptor (stdout)
mov     eax,4   ;system call number (sys_write)
int     0×80    ;call kernel
nwln
mov     ecx,res
mov     edx, 1
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 “The result is:”, 0xA,0xD
len equ $- msg
segment .bss
res resb 1
លទ្ធផល
The result is:
4
៨-មេរៀន  Logical Instructions
ទម្រង់ instructions ទាំងនេះ
AND     operand1, operand2
OR      operand1, operand2
XOR     operand1, operand2
TEST    operand1, operand2
NOT     operand1
The AND Instruction
ឧទាហរណ៍
Operand1:      0101
Operand2:         0011
—————————-
After AND -> Operand1: 0001
Assuming the number is in AL register, we can write:
AND     AL, 01H ; ANDing with 0000 0001
JZ      EVEN_NUMBER
ឧទាហរណ៍
section .text
global main                ;must be declared for using gcc
main:                         ;tell linker entry point
mov  ax, 8h            ; getting 8 in the ax
and     ax, 1              ; and ax with 1
jz      evnn
mov     eax, 4          ;system call number (sys_write)
mov     ebx, 1          ;file descriptor (stdout)
mov     ecx, odd_msg    ;message to write
mov     edx, len2       ;length of message
int     0×80            ;call kernel
jmp     outprog
evnn:   mov ah, 09h
mov     eax, 4         ;system call number (sys_write)
mov     ebx, 1          ;file descriptor (stdout)
mov     ecx, even_msg   ;message to write
mov     edx, len1       ; length of message
int     0×80            ;call kernel
outprog:
mov     eax,1          ;system call number (sys_exit)
int     0×80           ;call kernel
section .data
even_msg db   ‘Even Number!’  ; message showing even number
len1    equ     $ – even_msg
odd_msg db   ‘Odd Number!’  ; message showing odd number
len2    equ     $ – odd_msg
 លទ្ធផល
Even Number!
Change the value in the ax register with an odd digit, like:
mov  ax, 9h    ; getting 9 in the ax
The program would display:
Odd Number!
The OR Instruction
Operand1: 0101
Operand2:         0011
—————————-
After OR -> Operand1:  0111
ឧទារហណ៍
section .text
global main                ;must be declared for using gcc
main:                 ;tell linker entry point
mov     al, 5   ; getting 5 in the al
mov     bl, 3   ; getting 3 in the bl
or      al, bl  ; or al and bl registers, result should be 7
add     al, byte ’0′  ; converting decimal to ascii
mov     [result],  al
mov     eax, 4
mov     ebx, 1
mov     ecx, result
mov     edx, 1
int     0×80
outprog:
mov     eax,1          ;system call number (sys_exit)
int     0×80           ;call kernel
section .bss
result resb 1
លទ្ធផល  7
The XOR Instruction
Operand1:         0101
Operand2:         0011
—————————-
After XOR -> Operand1: 0110
XORing an operand with itself changes the operand to 0. This is used to clear a register.
XOR     EAX, EAX
The TEST Instruction
TEST    AL, 01H
JZ      EVEN_NUMBER
The NOT Instruction
Operand1:      0101 0011
After NOT -> Operand1: 1010 1100
៩- មេរៀន Conditions
The CMP Instruction
syntax:
CMP     destination, source
ឧទាហរណ៍
CMP     DX,     00      ; Compare the DX value with zero
JE      L7             ; If yes, then jump to label L7
.
.
L7: …
condition:
INC     EDX
CMP     EDX, 10 ; Compares whether the counter has reached 10
JLE     LP1     ; If it is less than or equal to 10, then jump to LP1
Unconditional Jump
syntax នៃ JMP instruction គឺ:
JMP     label
កូដបង្ហាញ JMP instruction:
MOV  AX, 00            ; Initializing AX to 0
MOV  BX, 00            ; Initializing BX to 0
MOV  CX, 01            ; Initializing CX to 1
L20:
ADD  AX, 01            ; Increment AX
ADD  BX, AX            ; Add AX to BX
SHL  CX, 1             ; shift left CX, this in turn doubles the CX value
JMP  L20               ; repeats the statements
Conditional Jump
arithmetic operations:
Instruction
Description
Flags tested
JE/JZJump Equal or Jump ZeroZF
JNE/JNZJump not Equal or Jump Not ZeroZF
JG/JNLEJump Greater or Jump Not Less/EqualOF, SF, ZF
JGE/JNLJump Greater or Jump Not LessOF, SF
JL/JNGEJump Less or Jump Not Greater/EqualOF, SF
JLE/JNGJump Less/Equal or Jump Not GreaterOF, SF, ZF
logical operations:
Instruction
Description
Flags tested
JE/JZJump Equal or Jump ZeroZF
JNE/JNZJump not Equal or Jump Not ZeroZF
JA/JNBEJump Above or Jump Not Below/EqualCF, ZF
JAE/JNBJump Above/Equal or Jump Not BelowCF
JB/JNAEJump Below or Jump Not Above/EqualCF
JBE/JNAJump Below/Equal or Jump Not AboveAF, CF
The syntax for the J<condition> set of instructions:
ឧទាហរណ៍
CMP     AL, BL
JE      EQUAL
CMP     AL, BH
JE      EQUAL
CMP     AL, CL
JE      EQUAL
NON_EQUAL: …
EQUAL: …
ឧទាហរណ៍
section .text
global main         ;must be declared for using gcc
main:   ;tell linker entry point
mov   ecx, [num1]
cmp   ecx, [num2]
jg    check_third_num
mov   ecx, [num3]
check_third_num:
cmp   ecx, [num3]
jg    _exit
mov   ecx, [num3]
_exit:
mov [largest], word ecx
mov      ecx,msg
mov     edx, len
mov     ebx,1   ;file descriptor (stdout)
mov     eax,4   ;system call number (sys_write)
int     0×80    ;call kernel
nwln
mov     ecx,largest
mov     edx, 2
mov     ebx,1   ;file descriptor (stdout)
mov     eax,4   ;system call number (sys_write)
int     0×80    ;call kernel
mov   eax, 1
int   80h
section .data
msg db “The largest digit is: “, 0xA,0xD
len equ $- msg
num1 dd ’47′
num2 dd ’22′
num3 dd ’31′
segment .bss
largest resb 2
លទ្ធផល
The largest digit is:
47
១០ -  មេរៀន Loops
MOV     CL, 10
L1:
<LOOP-BODY>
DEC     CL
JNZ     L1
The basic LOOP instruction has the following syntax:
LOOP    label
he above code snippet could be written as:
mov ECX,10
l1:
<loop body>
loop l1
ឧទាហរណ៍ បង្ហាញ print ចេញលេខ 1 ដល់ 9 លើអេក្រង់:
section .text
global main         ;must be declared for using gcc
main:   ;tell linker entry point
mov ecx,10
mov eax, ’1′
l1:
mov [num], eax
mov eax, 4
mov ebx, 1
push ecx
mov ecx, num
mov edx, 1
int 0×80
mov eax, [num]
sub eax, ’0′
inc eax
add eax, ’0′
pop ecx
loop l1
mov     eax,1   ;system call number (sys_exit)
int     0×80    ;call kernel
section .bss
num resb 1
លទ្ធផល
123456789
មេរៀន  Numbers
កូដបង្ហាញដូចខាងក្រោម:
section .text
global main         ;must be declared for using gcc
main:   ;tell linker entry point
mov     eax,’3′
sub     eax, ’0′
mov     ebx, ’4′
sub     ebx, ’0′
add     eax, ebx
add     eax, ’0′
mov     [sum], eax
mov     ecx,msg
mov     edx, len
mov     ebx,1   ;file descriptor (stdout)
mov     eax,4   ;system call number (sys_write)
int     0×80    ;call kernel
nwln
mov     ecx,sum
mov     edx, 1
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 “The sum is:”, 0xA,0xD
len equ $ – msg
segment .bss
sum resb 1
លទ្ធផល
The sum is:
7
តំណាង BCD Representation
មានពីរប្រភេទនៃតំណាង BCD:
  • Unpacked BCD representation
  • Packed BCD representation
ឧទាហរណ៍ចំនួន 1234 ត្រូវបានផ្ទុក:
01      02      03      04H
There are two instructions for processing these numbers:
  • AAM – ASCII Adjust After Multiplication
  • AAD – ASCII Adjust Before Division
ឧទាហរណ៍
section .text
global main         ;must be declared for using gcc
main:   ;tell linker entry point
mov     esi, 4  ; pointing to the rightmost digit
mov     ecx, 5  ; num of digits
clc
add_loop:
mov     al, [num1 + esi]
adc     al, [num2 + esi]
aaa
pushf
or      al, 30h
popf
mov     [sum + esi], al
dec     esi
loop    add_loop
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,5   ;message length
mov     ecx,sum ;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      ‘The Sum is:’,0xa
len     equ     $ – msg
num1  db   ’12345′
num2  db   ’23456′
sum   db   ‘     ‘
លទ្ធផល
The Sum is:
35801
  • មេរៀ Explicitly storing string length
  • Using a sentinel character
  • Explicitly storing string length
  • Using a sentinel character
ដំណើរការ String Processing
 msg  db  'Hello, world!',0xa ;our dear string
len  equ  $ – msg            ;length of our dear string
$-msg gives the length of the string
msg  db  ‘Hello, world!’,0xa ;our dear string
len  equ  13            ;length of our dear string
special character that does not appear within a string.
message    DB  ‘I am loving it!’, 0
The MOVS Instruction
ឧទាហរណ៍
section .text
global main         ;must be declared for using gcc
main:   ;tell linker entry point
mov     ecx, len
mov     esi, s1
mov     edi, s2
cld
rep     movsb
mov     edx,20  ;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
s1      db      ‘Hello, world!’,0  ; string 1
len     equ $-s1
section .bss
s2 resb 20  ; destination
លទ្ធផល
Hello, world!
The STOS Instruction
LODS និង STOS instruction ប្តូរទៅជា string ទាប តម្លៃតូច:
section .text
global main         ;must be declared for using gcc
main:   ;tell linker entry point
mov     ecx, len
mov     esi, s1
mov     edi, s2
loop_here:
lodsb
or al, 20h
stosb
loop loop_here
cld
rep     movsb
mov     edx,20  ;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
s1      db      ‘HELLO, WORLD’, 0  ; source
len     equ $-s1
section .bss
s2 resb 20   ; destination
 លទ្ធផល
hello, world
The CMPS Instruction
ប្រៀបធៀបពី strings ដែលប្រើ CMPS instruction:
section .text
global main         ;must be declared for using gcc
main:   ;tell linker entry point
mov esi, s1
mov edi, s2
mov ecx, lens2
cld
repe  cmpsb
jecxz  equal ;  jump when ecx is zero
; If not equal then the follwoing code
mov eax, 4
mov ebx, 1
mov ecx, msg_neq
mov edx, len_neq
int 80h
jmp exit
equal:
mov eax, 4
mov ebx, 1
mov ecx, msg_eq
mov edx, len_eq
int 80h
exit:
mov eax, 1
mov ebx, 0
int 80h
section .data
s1      db      ‘Hello, world!’,0      ;our first string
lens1   equ   $-s1
s2      db      ‘Hello, there!’, 0     ;our second string
lens2   equ   $-s2
msg_eq  db      ‘Strings are equal!’, 0xa
len_eq  equ    $-msg_eq
msg_neq db      ‘Strings are not equal!’
len_neq  equ   $-msg_neq
លទ្ធផល
Strings are not equal!
The SCAS Instruction
មើលកម្មវិធីស្វ័យយល់ជាមូលដ្ឋានខាងក្រោម:
section .text
global main         ;must be declared for using gcc
main:   ;tell linker entry point
mov ecx,len
mov edi,my_string
mov al , ‘e’
cld
repne scasb
je found ; when found
; If not not then the follwoing code
mov eax,4
mov ebx,1
mov ecx,msg_notfound
mov edx,len_notfound
int 80h
jmp exit
found:
mov eax,4
mov ebx,1
mov ecx,msg_found
mov edx,len_found
int 80h
exit:
mov eax,1
mov ebx,0
int 80h
section .data
my_string      db  ‘hello world’, 0
len equ $-my_string
msg_found      db      ‘found!’, 0xa
len_found  equ $-msg_found
msg_notfound   db      ‘not found!’
len_notfound  equ      $-msg_notfound
លទ្ធផល
found!
១១-មេរៀន Arrays
យើងអាចអោយនិយមន័យមួយពាក្ស word variable:
MONTHS  DW      12
MONTHS  DW      0CH
MONTHS  DW      0110B
define a one dimensional array of numbers.
NUMBERS DW  34,  45,  56,  67,  75, 89
You can define an array named inventory of size 8, and initialize all the values with zero, as:
INVENTORY   DW 0
DW 0
DW 0
DW 0
DW 0
DW 0
DW 0
DW 0
អាចសង្ខេប:
INVENTORY   DW  0, 0 , 0 , 0 , 0 , 0 , 0 , 0
Using TIMES, the INVENTORY array can be defined as
INVENTORY TIMES 8 DW 0
ឧទាហរណ៍
section .text
global main ;must be declared for linker (ld)
main:
mov  eax,3     ; number bytes to be summed
mov  ebx,0     ; EBX will store the sum
mov  ecx, x    ; ECX will point to the current
; element to be summed
top:  add  ebx, [ecx]
add  ecx,1     ; move pointer to next element
dec  eax        ; decrement counter
jnz  top         ; if counter not 0, then loop again
done:
add   ebx, ’0′
mov  [sum],byte ebx  ; done, store result in “sum”
display:
mov      edx,1       ;message length
mov       ecx, sum       ;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
global x
x:
db      2
db      4
db      3
sum:
db  0
លទ្ធផល   9
១២-មេរៀន  Procedures
CALL instruction ត្រូវបានប្រើសម្រាប់ដំណើរការ ដែលមាន format:
CALL proc_name
Let us write a very simple procedure named sum that adds the variables stored in the ECX and EDX register and returns the sum in the EAX register:
section .text
global main         ;must be declared for using gcc
main:   ;tell linker entry point
mov     ecx,’4′
sub     ecx, ’0′
mov     edx, ’5′
sub     edx, ’0′
call    sum
mov     [res], eax
mov     ecx, msg
mov     edx, len
mov     ebx,1   ;file descriptor (stdout)
mov     eax,4   ;system call number (sys_write)
int     0×80    ;call kernel
nwln
mov     ecx, res
mov     edx, 1
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
sum:
mov     eax, ecx
add      eax, edx
add eax, ’0′
ret
section .data
msg db “The sum is:”, 0xA,0xD
len equ $- msg
segment .bss
res resb 1
លទ្ធផល
The sum is:
9
១៣-មេរៀន  រង្វិលជុំ Recursion
Recursion នឹងយកមកប្រើក្នុងក្បួនគណិតវិទ្យា mathematical algorithms. ឧទាហរហ៍សម្រាប់គណនា ចំនួន factorial ។
Factorial នៃចំនួនដែលផ្តល់អោយមានសមីការ equation:
Fact (n) = n * fact (n-1) for n > 0
For example: factorial of 5 is 1 x 2 x 3 x 4 x 5 = 5 x factorial of 4
ដើម្បីរក្សាកម្មវិធីងាយ, យើងនឹងគណនា calculate factorial 3.
section .text
global main         ;must be declared for using gcc
main:   ;tell linker entry point
mov bx, 3   ; for calculating factorial 3
call proc_fact
add   ax, 30h
mov  [fact], ax
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,1  ;message length
mov     ecx,fact       ;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
proc_fact:
cmp  bl, 1
jg   do_calculation
mov  ax, 1
ret
do_calculation:
dec bl
call  proc_fact
inc bl
mul bl   ; ax = al * bl
ret
section .data
msg     db      ‘Factorial 3 is:’,0xa
len     equ     $ – msg
section .bss
fact resb 1
លទ្ធផល
Factorial 3 is:
6
១៤-មេរៀន Macros
macro ចាប់ផ្តើមជាមួយ %macro directive និងបញ្ចប់ %endmacro directive.
និយមន័យ Syntax សម្រាប់ macro:
%macro macro_name  number_of_params
<macro body>
%endmacro
sequence of instructions:
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
Following example shows defining and using macros:
; A macro with two parameters
; Implements the write system call
%macro write_string 2
mov   eax, 4
mov   ebx, 1
mov   ecx, %1
mov   edx, %2
int   80h
%endmacro
section .text
global main         ;must be declared for using gcc
main:   ;tell linker entry point
write_string msg1, len1
write_string msg2, len2
write_string msg3, len3
mov     eax,1   ;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!
១៥-មេរៀន ការគ្រប់គ្រង Memory
មានពីប្រព័ន្ធ call ក្នុង Linux ដែលអនុញ្ញាត្តប្រ dynamic memory allocation:
  • sys_mmap()
  • sys_brk()
កម្មវិធីខាងក្រោម allocates 16kb នៃ memory ដែលប្រើ sys_brk() system call:
section .text
global main         ;must be declared for using gcc
main:   ;tell linker entry point
mov     eax, 45        ; sys_brk
xor     ebx, ebx
int     80h
add     eax, 16384     ; number of bytes to be reserved
mov     ebx, eax
mov     eax, 45        ; sys_brk
int     80h
cmp     eax, 0
jl      exit    ; exit, if error
mov     edi, eax       ; EDI = highest available address
sub     edi, 4         ; pointing to the last DWORD
mov     ecx, 4096      ; number of DWORDs allocated
xor     eax, eax       ; clear eax
std                    ; backward
rep     stosd          ; repete for entire allocated area
cld                    ; put DF flag to normal state
mov     eax, 4
mov     ebx, 1
mov     ecx, msg
mov     edx, len
int     80h            ; print a message
exit:
mov     eax, 1
xor     ebx, ebx
int     80h
section .data
msg    db      “Allocated 16 kb of memory!”, 10
len     equ    $ – msg
លទ្ធផល
Allocated 16 kb of memory!
៧- មេរៀន  Arithmetic Instructions
INC Instruction
INC destination
The operand destination could be an 8-bit, 16-bit or 32-bit operand.
ឧទាហរណ៍
INC EBX     ; Increments 32-bit register
INC DL      ; Increments 8-bit register
INC [count]    ; Increments the count variable
DEC Instruction
DEC destination
The operand destination could be an 8-bit, 16-bit or 32-bit operand.
ឧទាហរណ៍
segment .data
count dw       0
value db       15
segment .text
inc [count]
dec     [value]
mov     ebx, count
inc word [ebx]
mov esi, value
dec byte [esi]
ADD និង SUB Instructions
syntax:
ADD/SUB destination, source
The ADD/SUB instruction can take place between:
  • Register to register
  • Memory to register
  • Register to memory
  • Register to constant data
  • Memory to constant data
SYS_EXIT  equ 1
SYS_READ  equ 3
SYS_WRITE equ 4
STDIN     equ 0
STDOUT    equ 1
segment .data
msg1 db “Enter a digit “, 0xA,0xD
len1 equ $- msg1
msg2 db “Please enter a second digit”, 0xA,0xD
len2 equ $- msg2
msg3 db “The sum is: ”
len3 equ $- msg3
segment .bss
num1 resb 2
num2 resb 2
res resb 1
segment .text
global main
main:
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg1
mov edx, len1
int 0×80
mov eax, SYS_READ
mov ebx, STDIN
mov ecx, num1
mov edx, 2
int 0×80
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg2
mov edx, len2
int 0×80
mov eax, SYS_READ
mov ebx, STDIN
mov ecx, num2
mov edx, 2
int 0×80
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg3
mov edx, len3
int 0×80
; moving the first number to eax register and second number to ebx
; and subtracting ascii ’0′ to convert it into a decimal number
mov eax, [number1]
sub eax, ’0′
mov ebx, [number2]
sub ebx, ’0′
; add eax and ebx
add eax, ebx
; add ’0′ to to convert the sum from decimal to ASCII
add eax, ’0′
; storing the sum in memory location res
mov [res], eax
; print the sum
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, res
mov edx, 1
int 0×80
exit:
mov eax, SYS_EXIT
xor ebx, ebx
int 0×80
លទ្ធផល
Enter a digit:
3
Please enter a second digit:
4
The sum is:
7
The program with hardcoded variables:
section .text
global main         ;must be declared for using gcc
main:   ;tell linker entry point
mov     eax,’3′
sub     eax, ’0′
mov     ebx, ’4′
sub     ebx, ’0′
add     eax, ebx
add     eax, ’0′
mov     [sum], eax
mov     ecx,msg
mov     edx, len
mov     ebx,1   ;file descriptor (stdout)
mov     eax,4   ;system call number (sys_write)
int     0×80    ;call kernel
nwln
mov     ecx,sum
mov     edx, 1
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 “The sum is:”, 0xA,0xD
len equ $ – msg
segment .bss
sum resb 1
លទ្ធផល
The sum is:
7
MUL/IMUL Instruction
syntax សម្រាប់ instructions:
MUL/IMUL multiplier
ឧទាហរណ៍
MOV AL, 10
MOV DL, 25
MUL DL

MOV DL, 0FFH   ; DL= -1
MOV AL, 0BEH   ; AL = -66
IMUL DL
ឧទាហរណ៍
section .text
global main         ;must be declared for using gcc
main:   ;tell linker entry point
mov     al,’3′
sub     al, ’0′
mov     bl, ’2′
sub     bl, ’0′
mul     bl
add     al, ’0′
mov     [res], al
mov     ecx,msg
mov     edx, len
mov     ebx,1   ;file descriptor (stdout)
mov     eax,4   ;system call number (sys_write)
int     0×80    ;call kernel
nwln
mov     ecx,res
mov     edx, 1
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 “The result is:”, 0xA,0xD
len equ $- msg
segment .bss
res resb 1
លទ្ធផល
The result is:
6
DIV/IDIV Instructions
ទម្រង់នៃ DIV/IDIV instruction:
DIV/IDIV       divisor
ឧទាហរណ៍
section .text
global main         ;must be declared for using gcc
main:   ;tell linker entry point
mov     ax,’8′
sub     ax, ’0′
mov     bl, ’2′
sub     bl, ’0′
div     bl
add     ax, ’0′
mov     [res], ax
mov     ecx,msg
mov     edx, len
mov     ebx,1   ;file descriptor (stdout)
mov     eax,4   ;system call number (sys_write)
int     0×80    ;call kernel
nwln
mov     ecx,res
mov     edx, 1
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 “The result is:”, 0xA,0xD
len equ $- msg
segment .bss
res resb 1
លទ្ធផល
The result is:
4
៨-មេរៀន  Logical Instructions
ទម្រង់ instructions ទាំងនេះ
AND     operand1, operand2
OR      operand1, operand2
XOR     operand1, operand2
TEST    operand1, operand2
NOT     operand1
The AND Instruction
ឧទាហរណ៍
Operand1:      0101
Operand2:         0011
—————————-
After AND -> Operand1: 0001
Assuming the number is in AL register, we can write:
AND     AL, 01H ; ANDing with 0000 0001
JZ      EVEN_NUMBER
ឧទាហរណ៍
section .text
global main                ;must be declared for using gcc
main:                         ;tell linker entry point
mov  ax, 8h            ; getting 8 in the ax
and     ax, 1              ; and ax with 1
jz      evnn
mov     eax, 4          ;system call number (sys_write)
mov     ebx, 1          ;file descriptor (stdout)
mov     ecx, odd_msg    ;message to write
mov     edx, len2       ;length of message
int     0×80            ;call kernel
jmp     outprog
evnn:   mov ah, 09h
mov     eax, 4         ;system call number (sys_write)
mov     ebx, 1          ;file descriptor (stdout)
mov     ecx, even_msg   ;message to write
mov     edx, len1       ; length of message
int     0×80            ;call kernel
outprog:
mov     eax,1          ;system call number (sys_exit)
int     0×80           ;call kernel
section .data
even_msg db   ‘Even Number!’  ; message showing even number
len1    equ     $ – even_msg
odd_msg db   ‘Odd Number!’  ; message showing odd number
len2    equ     $ – odd_msg
 លទ្ធផល
Even Number!
Change the value in the ax register with an odd digit, like:
mov  ax, 9h    ; getting 9 in the ax
The program would display:
Odd Number!
The OR Instruction
Operand1: 0101
Operand2:         0011
—————————-
After OR -> Operand1:  0111
ឧទារហណ៍
section .text
global main                ;must be declared for using gcc
main:                 ;tell linker entry point
mov     al, 5   ; getting 5 in the al
mov     bl, 3   ; getting 3 in the bl
or      al, bl  ; or al and bl registers, result should be 7
add     al, byte ’0′  ; converting decimal to ascii
mov     [result],  al
mov     eax, 4
mov     ebx, 1
mov     ecx, result
mov     edx, 1
int     0×80
outprog:
mov     eax,1          ;system call number (sys_exit)
int     0×80           ;call kernel
section .bss
result resb 1
លទ្ធផល  7
The XOR Instruction
Operand1:         0101
Operand2:         0011
—————————-
After XOR -> Operand1: 0110
XORing an operand with itself changes the operand to 0. This is used to clear a register.
XOR     EAX, EAX
The TEST Instruction
TEST    AL, 01H
JZ      EVEN_NUMBER
The NOT Instruction
Operand1:      0101 0011
After NOT -> Operand1: 1010 1100
៩- មេរៀន Conditions
The CMP Instruction
syntax:
CMP     destination, source
ឧទាហរណ៍
CMP     DX,     00      ; Compare the DX value with zero
JE      L7             ; If yes, then jump to label L7
.
.
L7: …
condition:
INC     EDX
CMP     EDX, 10 ; Compares whether the counter has reached 10
JLE     LP1     ; If it is less than or equal to 10, then jump to LP1
Unconditional Jump
syntax នៃ JMP instruction គឺ:
JMP     label
កូដបង្ហាញ JMP instruction:
MOV  AX, 00            ; Initializing AX to 0
MOV  BX, 00            ; Initializing BX to 0
MOV  CX, 01            ; Initializing CX to 1
L20:
ADD  AX, 01            ; Increment AX
ADD  BX, AX            ; Add AX to BX
SHL  CX, 1             ; shift left CX, this in turn doubles the CX value
JMP  L20               ; repeats the statements
Conditional Jump
arithmetic operations:
Instruction
Description
Flags tested
JE/JZJump Equal or Jump ZeroZF
JNE/JNZJump not Equal or Jump Not ZeroZF
JG/JNLEJump Greater or Jump Not Less/EqualOF, SF, ZF
JGE/JNLJump Greater or Jump Not LessOF, SF
JL/JNGEJump Less or Jump Not Greater/EqualOF, SF
JLE/JNGJump Less/Equal or Jump Not GreaterOF, SF, ZF
logical operations:
Instruction
Description
Flags tested
JE/JZJump Equal or Jump ZeroZF
JNE/JNZJump not Equal or Jump Not ZeroZF
JA/JNBEJump Above or Jump Not Below/EqualCF, ZF
JAE/JNBJump Above/Equal or Jump Not BelowCF
JB/JNAEJump Below or Jump Not Above/EqualCF
JBE/JNAJump Below/Equal or Jump Not AboveAF, CF
The syntax for the J<condition> set of instructions:
ឧទាហរណ៍
CMP     AL, BL
JE      EQUAL
CMP     AL, BH
JE      EQUAL
CMP     AL, CL
JE      EQUAL
NON_EQUAL: …
EQUAL: …
ឧទាហរណ៍
section .text
global main         ;must be declared for using gcc
main:   ;tell linker entry point
mov   ecx, [num1]
cmp   ecx, [num2]
jg    check_third_num
mov   ecx, [num3]
check_third_num:
cmp   ecx, [num3]
jg    _exit
mov   ecx, [num3]
_exit:
mov [largest], word ecx
mov      ecx,msg
mov     edx, len
mov     ebx,1   ;file descriptor (stdout)
mov     eax,4   ;system call number (sys_write)
int     0×80    ;call kernel
nwln
mov     ecx,largest
mov     edx, 2
mov     ebx,1   ;file descriptor (stdout)
mov     eax,4   ;system call number (sys_write)
int     0×80    ;call kernel
mov   eax, 1
int   80h
section .data
msg db “The largest digit is: “, 0xA,0xD
len equ $- msg
num1 dd ’47′
num2 dd ’22′
num3 dd ’31′
segment .bss
largest resb 2
លទ្ធផល
The largest digit is:
47
១០ -  មេរៀន Loops
MOV     CL, 10
L1:
<LOOP-BODY>
DEC     CL
JNZ     L1
The basic LOOP instruction has the following syntax:
LOOP    label
he above code snippet could be written as:
mov ECX,10
l1:
<loop body>
loop l1
ឧទាហរណ៍ បង្ហាញ print ចេញលេខ 1 ដល់ 9 លើអេក្រង់:
section .text
global main         ;must be declared for using gcc
main:   ;tell linker entry point
mov ecx,10
mov eax, ’1′
l1:
mov [num], eax
mov eax, 4
mov ebx, 1
push ecx
mov ecx, num
mov edx, 1
int 0×80
mov eax, [num]
sub eax, ’0′
inc eax
add eax, ’0′
pop ecx
loop l1
mov     eax,1   ;system call number (sys_exit)
int     0×80    ;call kernel
section .bss
num resb 1
លទ្ធផល
123456789
មេរៀន  Numbers
កូដបង្ហាញដូចខាងក្រោម:
section .text
global main         ;must be declared for using gcc
main:   ;tell linker entry point
mov     eax,’3′
sub     eax, ’0′
mov     ebx, ’4′
sub     ebx, ’0′
add     eax, ebx
add     eax, ’0′
mov     [sum], eax
mov     ecx,msg
mov     edx, len
mov     ebx,1   ;file descriptor (stdout)
mov     eax,4   ;system call number (sys_write)
int     0×80    ;call kernel
nwln
mov     ecx,sum
mov     edx, 1
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 “The sum is:”, 0xA,0xD
len equ $ – msg
segment .bss
sum resb 1
លទ្ធផល
The sum is:
7
តំណាង BCD Representation
មានពីរប្រភេទនៃតំណាង BCD:
  • Unpacked BCD representation
  • Packed BCD representation
ឧទាហរណ៍ចំនួន 1234 ត្រូវបានផ្ទុក:
01      02      03      04H
There are two instructions for processing these numbers:
  • AAM – ASCII Adjust After Multiplication
  • AAD – ASCII Adjust Before Division
ឧទាហរណ៍
section .text
global main         ;must be declared for using gcc
main:   ;tell linker entry point
mov     esi, 4  ; pointing to the rightmost digit
mov     ecx, 5  ; num of digits
clc
add_loop:
mov     al, [num1 + esi]
adc     al, [num2 + esi]
aaa
pushf
or      al, 30h
popf
mov     [sum + esi], al
dec     esi
loop    add_loop
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,5   ;message length
mov     ecx,sum ;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      ‘The Sum is:’,0xa
len     equ     $ – msg
num1  db   ’12345′
num2  db   ’23456′
sum   db   ‘     ‘
លទ្ធផល
The Sum is:
35801
  • មេរៀ Explicitly storing string length
  • Using a sentinel character
  • Explicitly storing string length
  • Using a sentinel character
ដំណើរការ String Processing
 msg  db  'Hello, world!',0xa ;our dear string
len  equ  $ – msg            ;length of our dear string
$-msg gives the length of the string
msg  db  ‘Hello, world!’,0xa ;our dear string
len  equ  13            ;length of our dear string
special character that does not appear within a string.
message    DB  ‘I am loving it!’, 0
The MOVS Instruction
ឧទាហរណ៍
section .text
global main         ;must be declared for using gcc
main:   ;tell linker entry point
mov     ecx, len
mov     esi, s1
mov     edi, s2
cld
rep     movsb
mov     edx,20  ;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
s1      db      ‘Hello, world!’,0  ; string 1
len     equ $-s1
section .bss
s2 resb 20  ; destination
លទ្ធផល
Hello, world!
The STOS Instruction
LODS និង STOS instruction ប្តូរទៅជា string ទាប តម្លៃតូច:
section .text
global main         ;must be declared for using gcc
main:   ;tell linker entry point
mov     ecx, len
mov     esi, s1
mov     edi, s2
loop_here:
lodsb
or al, 20h
stosb
loop loop_here
cld
rep     movsb
mov     edx,20  ;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
s1      db      ‘HELLO, WORLD’, 0  ; source
len     equ $-s1
section .bss
s2 resb 20   ; destination
 លទ្ធផល
hello, world
The CMPS Instruction
ប្រៀបធៀបពី strings ដែលប្រើ CMPS instruction:
section .text
global main         ;must be declared for using gcc
main:   ;tell linker entry point
mov esi, s1
mov edi, s2
mov ecx, lens2
cld
repe  cmpsb
jecxz  equal ;  jump when ecx is zero
; If not equal then the follwoing code
mov eax, 4
mov ebx, 1
mov ecx, msg_neq
mov edx, len_neq
int 80h
jmp exit
equal:
mov eax, 4
mov ebx, 1
mov ecx, msg_eq
mov edx, len_eq
int 80h
exit:
mov eax, 1
mov ebx, 0
int 80h
section .data
s1      db      ‘Hello, world!’,0      ;our first string
lens1   equ   $-s1
s2      db      ‘Hello, there!’, 0     ;our second string
lens2   equ   $-s2
msg_eq  db      ‘Strings are equal!’, 0xa
len_eq  equ    $-msg_eq
msg_neq db      ‘Strings are not equal!’
len_neq  equ   $-msg_neq
លទ្ធផល
Strings are not equal!
The SCAS Instruction
មើលកម្មវិធីស្វ័យយល់ជាមូលដ្ឋានខាងក្រោម:
section .text
global main         ;must be declared for using gcc
main:   ;tell linker entry point
mov ecx,len
mov edi,my_string
mov al , ‘e’
cld
repne scasb
je found ; when found
; If not not then the follwoing code
mov eax,4
mov ebx,1
mov ecx,msg_notfound
mov edx,len_notfound
int 80h
jmp exit
found:
mov eax,4
mov ebx,1
mov ecx,msg_found
mov edx,len_found
int 80h
exit:
mov eax,1
mov ebx,0
int 80h
section .data
my_string      db  ‘hello world’, 0
len equ $-my_string
msg_found      db      ‘found!’, 0xa
len_found  equ $-msg_found
msg_notfound   db      ‘not found!’
len_notfound  equ      $-msg_notfound
លទ្ធផល
found!
១១-មេរៀន Arrays
យើងអាចអោយនិយមន័យមួយពាក្ស word variable:
MONTHS  DW      12
MONTHS  DW      0CH
MONTHS  DW      0110B
define a one dimensional array of numbers.
NUMBERS DW  34,  45,  56,  67,  75, 89
You can define an array named inventory of size 8, and initialize all the values with zero, as:
INVENTORY   DW 0
DW 0
DW 0
DW 0
DW 0
DW 0
DW 0
DW 0
អាចសង្ខេប:
INVENTORY   DW  0, 0 , 0 , 0 , 0 , 0 , 0 , 0
Using TIMES, the INVENTORY array can be defined as
INVENTORY TIMES 8 DW 0
ឧទាហរណ៍
section .text
global main ;must be declared for linker (ld)
main:
mov  eax,3     ; number bytes to be summed
mov  ebx,0     ; EBX will store the sum
mov  ecx, x    ; ECX will point to the current
; element to be summed
top:  add  ebx, [ecx]
add  ecx,1     ; move pointer to next element
dec  eax        ; decrement counter
jnz  top         ; if counter not 0, then loop again
done:
add   ebx, ’0′
mov  [sum],byte ebx  ; done, store result in “sum”
display:
mov      edx,1       ;message length
mov       ecx, sum       ;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
global x
x:
db      2
db      4
db      3
sum:
db  0
លទ្ធផល   9
១២-មេរៀន  Procedures
CALL instruction ត្រូវបានប្រើសម្រាប់ដំណើរការ ដែលមាន format:
CALL proc_name
Let us write a very simple procedure named sum that adds the variables stored in the ECX and EDX register and returns the sum in the EAX register:
section .text
global main         ;must be declared for using gcc
main:   ;tell linker entry point
mov     ecx,’4′
sub     ecx, ’0′
mov     edx, ’5′
sub     edx, ’0′
call    sum
mov     [res], eax
mov     ecx, msg
mov     edx, len
mov     ebx,1   ;file descriptor (stdout)
mov     eax,4   ;system call number (sys_write)
int     0×80    ;call kernel
nwln
mov     ecx, res
mov     edx, 1
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
sum:
mov     eax, ecx
add      eax, edx
add eax, ’0′
ret
section .data
msg db “The sum is:”, 0xA,0xD
len equ $- msg
segment .bss
res resb 1
លទ្ធផល
The sum is:
9
១៣-មេរៀន  រង្វិលជុំ Recursion
Recursion នឹងយកមកប្រើក្នុងក្បួនគណិតវិទ្យា mathematical algorithms. ឧទាហរហ៍សម្រាប់គណនា ចំនួន factorial ។
Factorial នៃចំនួនដែលផ្តល់អោយមានសមីការ equation:
Fact (n) = n * fact (n-1) for n > 0
For example: factorial of 5 is 1 x 2 x 3 x 4 x 5 = 5 x factorial of 4
ដើម្បីរក្សាកម្មវិធីងាយ, យើងនឹងគណនា calculate factorial 3.
section .text
global main         ;must be declared for using gcc
main:   ;tell linker entry point
mov bx, 3   ; for calculating factorial 3
call proc_fact
add   ax, 30h
mov  [fact], ax
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,1  ;message length
mov     ecx,fact       ;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
proc_fact:
cmp  bl, 1
jg   do_calculation
mov  ax, 1
ret
do_calculation:
dec bl
call  proc_fact
inc bl
mul bl   ; ax = al * bl
ret
section .data
msg     db      ‘Factorial 3 is:’,0xa
len     equ     $ – msg
section .bss
fact resb 1
លទ្ធផល
Factorial 3 is:
6
១៤-មេរៀន Macros
macro ចាប់ផ្តើមជាមួយ %macro directive និងបញ្ចប់ %endmacro directive.
និយមន័យ Syntax សម្រាប់ macro:
%macro macro_name  number_of_params
<macro body>
%endmacro
sequence of instructions:
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
Following example shows defining and using macros:
; A macro with two parameters
; Implements the write system call
%macro write_string 2
mov   eax, 4
mov   ebx, 1
mov   ecx, %1
mov   edx, %2
int   80h
%endmacro
section .text
global main         ;must be declared for using gcc
main:   ;tell linker entry point
write_string msg1, len1
write_string msg2, len2
write_string msg3, len3
mov     eax,1   ;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!
១៥-មេរៀន ការគ្រប់គ្រង Memory
មានពីប្រព័ន្ធ call ក្នុង Linux ដែលអនុញ្ញាត្តប្រ dynamic memory allocation:
  • sys_mmap()
  • sys_brk()
កម្មវិធីខាងក្រោម allocates 16kb នៃ memory ដែលប្រើ sys_brk() system call:
section .text
global main         ;must be declared for using gcc
main:   ;tell linker entry point
mov     eax, 45        ; sys_brk
xor     ebx, ebx
int     80h
add     eax, 16384     ; number of bytes to be reserved
mov     ebx, eax
mov     eax, 45        ; sys_brk
int     80h
cmp     eax, 0
jl      exit    ; exit, if error
mov     edi, eax       ; EDI = highest available address
sub     edi, 4         ; pointing to the last DWORD
mov     ecx, 4096      ; number of DWORDs allocated
xor     eax, eax       ; clear eax
std                    ; backward
rep     stosd          ; repete for entire allocated area
cld                    ; put DF flag to normal state
mov     eax, 4
mov     ebx, 1
mov     ecx, msg
mov     edx, len
int     80h            ; print a message
exit:
mov     eax, 1
xor     ebx, ebx
int     80h
section .data
msg    db      “Allocated 16 kb of memory!”, 10
len     equ    $ – msg
លទ្ធផល
Allocated 16 kb of memory!
មេរៀនកម្មវិធី Syntax Assembly (ត)
View detail
មេរៀនកម្មវិធី 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]

មេរៀន មូលដ្ឋានភាសា 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]
មេរៀនកម្មវិធី Syntax Assembly
View detail
មេរៀន INTEL 8088 Proccessor

មេរៀន INTEL 8088 Proccessor

មាននៅក្នុង​​ CPU 8088 បានចែកចេញជា​​ 2 ផ្នែកគឺ EU និង​  BIU  ។  Opcode  Buffer  ជាUnit មួយដែលមិនត្រឹមតែមានតួនាទីសំរាប់ផ្នុក opcode ដើម្បីបញ្ជូលទៅ CU ឬ​ Bus ALU ( មានប្រវែង 4 byte សំរាប់ 8088 និង​6 byte សំរាប់ 8088) ទេថែមទាំង ជាកន្លែង ដែលតភ្ជាប់រវាង  EUនិង BIU ដែលបង្កើតល្បឿនយ៉ាងលឿនដល់ CPU ក្នុងការដោះ ដូរទិន្ន័យ​ ។


+ Function  ចំបងរបស់ BIU គឺបញ្ជួន address, អាន opcode ពី memory និង Read/-write data   from/to port/memory  ឬ​  គេអាចនិយាយបានថា  BIU  មានភារ;កិច្ចសំរាប់បញ្ជូន address ទៅ Bus ឬ​ និងដោះដូរទិន្ន័យជាមួយ Bus ។
ចំនែកក្នុង EU វិញគេឃើញមាន Control Unit (CU) ដែលជា​ Unit មួយមានសៀគ្វី opco-de decoder ។ opcode ត្រូវបានអានចេញពី memory ហើយបញ្ជួនទៅដល់  gate  Input  របស់Decoder បន្ទាប់មក Instruction ដែលទទួលបានពី   Decoder   ត្រូវបញ្ជួនទៅផ្នែកបង្កើតសញ្ញា បញ្ជា (ផ្នែកមួយដែលស្ថិតនៅក្នុង Cu ដែរ)​ ដើម្បីបញ្ជួនសញ្ញាទាំងនោះទៅបញ្ជាផ្នែកផ្សេងៗក្នុង EU យើងទៅតាម Instruction  នោះ ។​ ប្រសិនបើលទ្ធផល ដែលបានពី Opcode decoder ត្រូវការធ្វើប្រមាណវិធីពេលនោះសៀគ្វីដែលមានតួនាទីបង្កើតសញ្ញាបញ្ជាខាងក្នុង CPU ឬ EU នោះក៏បង្កើតជាមុខសញ្ញាបញ្ជារួចបញ្ជួនទៅផ្នែក ALU ។
ALU ជា​ មូនដ្ឋានមួយសំរាប់អនុវត្តន៏បណ្តាប្រមាណវិធីរួមមានទាំងប្រមាណលេខនិងប្រមាណវិធីតក្ក; ។ Opcode Buffer ជាអង្កភាពមួយប្រើសំរាប់ផ្ទុកបណ្តាអស្សន; Opcode ដែលបានអាន ចេញពី memory ដើម្បីរងចាំផ្នែក EU អនុវត្តន៏ ។

. Memory addressing BIU របស់ 8088 បានបង្ហាញថាលើ Bus address មាន​ 20bit address(20ខ្សែ address)ដូច្នេះ =>8088 មានលទ្ធភាពអាចវិភាគ address បាន 2  = 1.048.576= 1M ថត memory ដែលថតនីមួយមានប្រវែង​ 1byte ។​ នៅក្នុងលំហរ memory 1M byte របស់ 8088 ត្រូវបានគេចែកទៅជា​ segment   ( កំនាត់ៗ)  ដែលប្រើវិធីនេះវាផ្តល់ប្រយោជន៏យ៉ាងសំខាន់ក្នុងពេលដែលគេត្រូវការប្រើច្រើនការងារក្នុងពេលតែមួយ (multitaslcing) ។ Segment និមួយៗត្ូវបានគេប្រើសំរាប់មុខងារផ្សេងៗគ្នាដូចជា;
-     ផ្ទុក code programming
-     ផ្ទុក​ Data និង Result របស់កម្មវិធី
-     បង្កើត Segment ពិសេសមួយហៅថា stack ប្រើសំរាប់គ្រប់គ្រងប៉ារ៉ាមែត្ររបស់ CPU ពេលហៅកម្មវិធីរង (SP) ឬត្រលប់ចេញពីកម្មវិធីរងវិញ ។
ការរួមផ្សំគ្នារវាង  Segment  និង  Offsets  address  នាំអោយយើងទទួលបាននូវទីតាំង(address)  memory ពិតមួយរឺហៅថា Physical address ។ Segment តំបូងត្រូវបានគេកំនត់ចាបពី Beginning address រហូតដល់ 64 KB បន្ទាប់ទើបចាប់ផ្តើម Segment បន្ទាប់ទៀត ឬ  យើងនិយាយថាគេកំនត់យក 1 segment មានប្រវែង 64 KByte ។ Offset address គឺជា address លំអៀងក្នុង Segment ណាមួយ
Registers introduction:
ក.  Multiperpos Register<Multitasking Register> ជាបណ្តា Register ដែលមានប្រវែង 16 bit រួមមាន; AX, BX, CX, DX ។  លក្ខណ;ពិសេសរបស់ Register ទាងនេះគឺនៅពេលដែលត្រូវការផ្ទុកទិន្ន័យ  8 bit  នោះ  Register  ទាំងនោះអាចផ្តាច់ចេញជាពីរផ្នែកគឺ; AU, AL, BU, BL, CH, CL, DH, DL Register និមួយៗត្រូវបានគេប្រើសំរាប់ផ្ទុកទិន្ន័យប្រភេទផ្សេងៗគ្នាពោលគឺគេប្រើវាទៅ
តាមមុខងាររៀងៗខ្លួន;
▪ AX (Accumulator): បន្ទះផ្ទុកទិន្ន័យសំរាប់យកទៅធ្វើប្រមាណវិធី ឬ ក៏ជាកន្លែងសំរាប់
ផ្ទុកលទ្ធផលប្រមាណវិធីដែរ ។ ប្រសិនបើលទ្ធផល 8 bit នោះបន្ទះ AL ត្រូវបានប្រើ
▪​​​ BX (Base) បន្ទះមូលដ្ឋានតែងត្រូវគេប្រើសំរាប់ផ្ទុក address base ។
▪ CX (Count): CX ត្រូវគេប្រើសំរាប់ផ្ទុកតំលៃចំនួន loop នៅពេលដែលជួប Loop statement ។
▪​ DX (Data): បន្ទះផ្ទុកទិន្ន័យ ។ DX រួមជាមួយ AX ដើម្បីអនុវត្តន៏ប្រមាណវិធីគុណ ឬចែក
ក្រៅពីនេះ DX ត្រូវបានគេប្រើដើម្បីផ្ទុក address របស់ port (IN/out) ។
Pointer and Index Register ក្នុង 8088 មាន 3 Pointer Register និង 2 Index Register  ។  Register  និមួយៗសុទ្ធតែមានប្រវែង 16 bit និងអាចប្រើដូចជា  multipurpose  Register  ដែរ លើកលែងតែ  Register ​ IP ចេញ ។ ក្រៅពីអាចប្រើ ដូចខាងលើនេះ Register និមួយៗនៅមានមុខងាររៀងៗខ្លួនទៀតដូចជា
+ IP : Instruction Pointer តែងតែចង្អុរទៅ    Instruction  បន្ទាប់ទៀតដែលនិងត្រូវចូល មកអនុវត្តន៏ជាបន្ត (Instruction ទាំងអស់នេះនៅក្នុង Code segment) ។ Address របស់ Instru-ction បន្ទាប់ត្រូវបានកំនត់ដោយ CS:IP ។
+ BP: Base pointer ជានិច្ចជាកាលចង្អុរទៅ Data  មួយក្នុង  stack  segment  ដែលមាន address SS:BP (address របស់ Data នេះ)
+ SP: Stack pointer ជា pointer ដែលជានិច្ចកាលចង្អុរទៅរក stack top ក្នុង stack seg-ment ។ address របស់ stack top គឺ SS:SP ។
+ SI: Source Index, SI ចង្អុរទៅទិន្ន័យមួយដែលនៅក្នុង Data Segment ហើយដែលទិន្ន័យនោះមាន address ត្រូវបានកំនត់ដោយ DS:SI ។
+ DI: Destination Index, DI ចង្អុរទៅរកទិន្ន័យមួយដែលនៅក្នុង Data segment ដែរហើយដែល address របស់ Data នោះគឺត្រូវបានកំនត់ដោយ DS:DI ។
- Segment Registers: ការផ្សំគ្នារវាង Segment Registers ជាមួយនិង Register មួយចំនួនទៀតនៅក្នុង mP  នាំ អោយទទួលបាននូវ Address ពិតមួយដោយគណនាតាមរូបមន្ត;
Real addr = Segment x 16 + Offset
ដែលក្នុងនោះ offset គឺជា address ដែលត្រូវបានរក្សាទុកក្នុង  Register  មួយចំនួនដូចជា ; IP, BP, SP, SI, DI ។
+ CS: Code Segment គឺជាផ្នែកមួយនៃ memory ដែលប្រើសំរាប់គ្រប់គ្រង ឬ ផ្ទុក  Codeprograme ឬ code procedure ។   ក្នុង 8088 ដល់ 80286 code segment   ត្រូវបានគេកំនត់ត្រឹម  64K bytes  ។
+ DS: Data segment ជាផ្នែក memory ដែលគេប្រើសំរាប់ផ្ទុកទិន្ន័យដែលត្រូវប្រើសំរាប់កម្មវិធី ។​ (Source Data)
+ ES: Extra segment   ជាតំបន់  memory  មួយដែលត្រូវគេពង្រីកបន្ថែមសំរាប់បំរើដល់
ប្រយោជន៏ជំនួយដល់ DS ក្នុងការរក្សាទុក Destination data ។
+ SS: Stack segment ជាបណ្តុំ memory មួយផ្នែកដែលត្រូវគេប្រើសំរាប់ផ្ទុក Data ឬ ប៉ារ៉ាមែត្ររបស់ Procedure ។ SS ផ្សំជាមួយ SP ឬ BP នាំអោយកំនត់បាន   address   មួយនៅក្នុង stack segment ។
Flags Register ជា Register ពិសេសក្នុង  CPU  bit  និមួយៗរបស់វាត្រូវបានគេ ប្រើសំរាប់បញ្ជាក់បង្ហាញនូវសភាពណា មួយក្នុងតំណើរការណ៏អនុវត្តន៏ប្រមាណវិធីរបស់ ALU ឬ សកម្មភាពរបស់ EU ។ដោយពឹងផ្អែកទៅលើ Flag អស់ទាំងនេះ=> អ្នកសរសេរកម្មវិធី   ( Programer)  អាចឈានទៅបង្កើតជា statement បន្តទៀតអោយ mP ។ Register នេះមាន  16 bit  ក៏ប៉ុន្តែត្រូចគេប្រើអស់តែ 9 bit ប៉ុណ្ណោះដើម្បីធ្វើជា Bit flag ។
X : ជា Bit ដែលពុំទាន់ត្រូវបានគេប្រើប្រាស់ក្នុង 8088/8086
Register នេះមានប្រវែង 32 bit ចាប់ពី 80286 ឡើងទៅ
+ C ឬ CF: Carry Flag , CF=1 បានន័យថាមានត្រាទុកឬបានខ្ចីអំពី HSB ។
+ P ឬ PF: Parity Flag, PF= 1 បានន័យថាចំនួនសរុបរបស់ bit 1 ក្នុងលទ្ធផលប្រមាណវិធីជាចំនួនគូ
+ A ឬ AF: Auxiliary Flag, AF=1 នៅពេលដែលមានត្រាទុកពេលធ្វើវិធីបូកឬខ្ចីពេលធ្វើប្រមាណ
វិធិដករវាង Bit ទី​ 3 និង Bit ទី 4
+ Z ឬ ZF: Zero Flag, ZF=1 ពេលដែលលទ្ធផលប្រមាណវិធីស្មើសូន្យ ។
+ S ឬ SF: Signal Flag, SF=1 ពេលដែលលទ្ធផលប្រមាណវិធីជាតំលៃអវិជ្ជមាន
+ T ឬ TF: Trap Flag: ប្រសិនបើ TF=1=> mP បញ្ឍប់ឬផ្អាកដំនើរការ    Programe  ដើម្បីស្វែងរកកំហុសក្នុង Register ។
+ I ឬ IF: Interrupt enable Flag: ប្រសិនបើ IF=1  នោះ  CPU  អនុញ្ញាត្តិអោយ  Interrupt សកម្មភាព​។ Interrupt មានន័យថា ជាសញ្ញាមួយដែលបញ្ជួនទៅ mP ដើម្បីប្រកាសប្រាប់ថាមានព្រតិ្តការណ៏មួមបានកើតឡើងស្មើ CPU ប្រុងប្រៀប​និង CPU ពេលទទួលបានសញ្ញានេះត្រូចផ្អាកសកម្មភាពកំពុងដំនើរការជា បណ្តោះអាសន្នដើម្បី អនុវត្តន៏ ការងាររបស់ព្រឹត្តិការណ៏ថ្មីនោះ ។ ក្រោយពេលព្រត្តិការណ៏ថ្មីនោះចប់=> CPU បន្តសកម្មភាពចាស់ជាបន្តទៀត​​ ។
+ D ឬ DF: Direction Flag ជា​ Flag មួយប្រើសំរាប់កំនត់ទឹសអោយ DI និង SI ក្នុងខណ:ដែលកំពុងប្រតិ- បត្តិការណ៏សេរី Instruction ។ ប្រសិនបើ DF=0 នោះតំលៃរបស់ DI និង  SI  ត្រូវកើនឡើងជាស្វ័យប្រវត្តិ តែផ្ទុយ
ទៅវិញបើ DF=0=> តំលៃរបស់ DI និង SI ត្រូវថយចុះទៅវិញ ។
+ O ឬ OF: Overflow Flag, ប្រសិនបើ OF=1 មានន័យថាលទ្ធផលរបស់ប្រមាណវិធីបូក 127 បូកនិង 1 ហើយលទ្ធផលត្រូវយកទៅរក្សាទុកក្នុង Memory មាន capacity=8bit ។ ដូច្នេះលទ្ធផលរបស់ផលបូកគឺ 128=> memory ដែលមានទំហំតែ 8 bit ពុំអាចទុកតំលៃនេះបានទេ ពេលនេះ=> OF=1 រីឯbit ទាំង 8 ខាងលើនេះផ្ទុក តំលៃ 0 ទាំងអស់ ។
àà ភាពដូចគ្នានិងខុសគ្នារវាង 8088 និង 8086
8086 ជា​ mP ដែលធ្វើការជាមួយ 16 bit Data ទាំងមូលចំនែក 8088 វិញផ្នែកខាងក្នុង CPU គឺធ្វើការជាមួយ 16 bit data (Bus ក្នុង) ក៏ប៉ុន្តែពេលបញ្ចេញមកខាងក្រៅគឺមានតែ 8 bit ប៉ុណ្ណោះ (Bus ក្រៅ)​ ។ ផ្នែកខាងក្នុងរបស់ 8088 និង 8086 ដូចគ្នាទាំងអស់ខុសត្រង់ Instruction cashe ប៉ុណ្ណោះដែល 8088 មានប្រវែង 4 byte ចំនែក 80-86 មានប្រវែង 6 byte ដែលបញ្ហានេះនាំទៅដល់ការប៉ះពាល់ន្បឿនប្រតិបត្តិការណ៏របស់ CPU ដូច្នេះហើយដែល 8088 មានតំលៃថោកជាង 8086 ។
មាននៅក្នុង​​ CPU 8088 បានចែកចេញជា​​ 2 ផ្នែកគឺ EU និង​  BIU  ។  Opcode  Buffer  ជាUnit មួយដែលមិនត្រឹមតែមានតួនាទីសំរាប់ផ្នុក opcode ដើម្បីបញ្ជូលទៅ CU ឬ​ Bus ALU ( មានប្រវែង 4 byte សំរាប់ 8088 និង​6 byte សំរាប់ 8088) ទេថែមទាំង ជាកន្លែង ដែលតភ្ជាប់រវាង  EUនិង BIU ដែលបង្កើតល្បឿនយ៉ាងលឿនដល់ CPU ក្នុងការដោះ ដូរទិន្ន័យ​ ។


+ Function  ចំបងរបស់ BIU គឺបញ្ជួន address, អាន opcode ពី memory និង Read/-write data   from/to port/memory  ឬ​  គេអាចនិយាយបានថា  BIU  មានភារ;កិច្ចសំរាប់បញ្ជូន address ទៅ Bus ឬ​ និងដោះដូរទិន្ន័យជាមួយ Bus ។
ចំនែកក្នុង EU វិញគេឃើញមាន Control Unit (CU) ដែលជា​ Unit មួយមានសៀគ្វី opco-de decoder ។ opcode ត្រូវបានអានចេញពី memory ហើយបញ្ជួនទៅដល់  gate  Input  របស់Decoder បន្ទាប់មក Instruction ដែលទទួលបានពី   Decoder   ត្រូវបញ្ជួនទៅផ្នែកបង្កើតសញ្ញា បញ្ជា (ផ្នែកមួយដែលស្ថិតនៅក្នុង Cu ដែរ)​ ដើម្បីបញ្ជួនសញ្ញាទាំងនោះទៅបញ្ជាផ្នែកផ្សេងៗក្នុង EU យើងទៅតាម Instruction  នោះ ។​ ប្រសិនបើលទ្ធផល ដែលបានពី Opcode decoder ត្រូវការធ្វើប្រមាណវិធីពេលនោះសៀគ្វីដែលមានតួនាទីបង្កើតសញ្ញាបញ្ជាខាងក្នុង CPU ឬ EU នោះក៏បង្កើតជាមុខសញ្ញាបញ្ជារួចបញ្ជួនទៅផ្នែក ALU ។
ALU ជា​ មូនដ្ឋានមួយសំរាប់អនុវត្តន៏បណ្តាប្រមាណវិធីរួមមានទាំងប្រមាណលេខនិងប្រមាណវិធីតក្ក; ។ Opcode Buffer ជាអង្កភាពមួយប្រើសំរាប់ផ្ទុកបណ្តាអស្សន; Opcode ដែលបានអាន ចេញពី memory ដើម្បីរងចាំផ្នែក EU អនុវត្តន៏ ។

. Memory addressing BIU របស់ 8088 បានបង្ហាញថាលើ Bus address មាន​ 20bit address(20ខ្សែ address)ដូច្នេះ =>8088 មានលទ្ធភាពអាចវិភាគ address បាន 2  = 1.048.576= 1M ថត memory ដែលថតនីមួយមានប្រវែង​ 1byte ។​ នៅក្នុងលំហរ memory 1M byte របស់ 8088 ត្រូវបានគេចែកទៅជា​ segment   ( កំនាត់ៗ)  ដែលប្រើវិធីនេះវាផ្តល់ប្រយោជន៏យ៉ាងសំខាន់ក្នុងពេលដែលគេត្រូវការប្រើច្រើនការងារក្នុងពេលតែមួយ (multitaslcing) ។ Segment និមួយៗត្ូវបានគេប្រើសំរាប់មុខងារផ្សេងៗគ្នាដូចជា;
-     ផ្ទុក code programming
-     ផ្ទុក​ Data និង Result របស់កម្មវិធី
-     បង្កើត Segment ពិសេសមួយហៅថា stack ប្រើសំរាប់គ្រប់គ្រងប៉ារ៉ាមែត្ររបស់ CPU ពេលហៅកម្មវិធីរង (SP) ឬត្រលប់ចេញពីកម្មវិធីរងវិញ ។
ការរួមផ្សំគ្នារវាង  Segment  និង  Offsets  address  នាំអោយយើងទទួលបាននូវទីតាំង(address)  memory ពិតមួយរឺហៅថា Physical address ។ Segment តំបូងត្រូវបានគេកំនត់ចាបពី Beginning address រហូតដល់ 64 KB បន្ទាប់ទើបចាប់ផ្តើម Segment បន្ទាប់ទៀត ឬ  យើងនិយាយថាគេកំនត់យក 1 segment មានប្រវែង 64 KByte ។ Offset address គឺជា address លំអៀងក្នុង Segment ណាមួយ
Registers introduction:
ក.  Multiperpos Register<Multitasking Register> ជាបណ្តា Register ដែលមានប្រវែង 16 bit រួមមាន; AX, BX, CX, DX ។  លក្ខណ;ពិសេសរបស់ Register ទាងនេះគឺនៅពេលដែលត្រូវការផ្ទុកទិន្ន័យ  8 bit  នោះ  Register  ទាំងនោះអាចផ្តាច់ចេញជាពីរផ្នែកគឺ; AU, AL, BU, BL, CH, CL, DH, DL Register និមួយៗត្រូវបានគេប្រើសំរាប់ផ្ទុកទិន្ន័យប្រភេទផ្សេងៗគ្នាពោលគឺគេប្រើវាទៅ
តាមមុខងាររៀងៗខ្លួន;
▪ AX (Accumulator): បន្ទះផ្ទុកទិន្ន័យសំរាប់យកទៅធ្វើប្រមាណវិធី ឬ ក៏ជាកន្លែងសំរាប់
ផ្ទុកលទ្ធផលប្រមាណវិធីដែរ ។ ប្រសិនបើលទ្ធផល 8 bit នោះបន្ទះ AL ត្រូវបានប្រើ
▪​​​ BX (Base) បន្ទះមូលដ្ឋានតែងត្រូវគេប្រើសំរាប់ផ្ទុក address base ។
▪ CX (Count): CX ត្រូវគេប្រើសំរាប់ផ្ទុកតំលៃចំនួន loop នៅពេលដែលជួប Loop statement ។
▪​ DX (Data): បន្ទះផ្ទុកទិន្ន័យ ។ DX រួមជាមួយ AX ដើម្បីអនុវត្តន៏ប្រមាណវិធីគុណ ឬចែក
ក្រៅពីនេះ DX ត្រូវបានគេប្រើដើម្បីផ្ទុក address របស់ port (IN/out) ។
Pointer and Index Register ក្នុង 8088 មាន 3 Pointer Register និង 2 Index Register  ។  Register  និមួយៗសុទ្ធតែមានប្រវែង 16 bit និងអាចប្រើដូចជា  multipurpose  Register  ដែរ លើកលែងតែ  Register ​ IP ចេញ ។ ក្រៅពីអាចប្រើ ដូចខាងលើនេះ Register និមួយៗនៅមានមុខងាររៀងៗខ្លួនទៀតដូចជា
+ IP : Instruction Pointer តែងតែចង្អុរទៅ    Instruction  បន្ទាប់ទៀតដែលនិងត្រូវចូល មកអនុវត្តន៏ជាបន្ត (Instruction ទាំងអស់នេះនៅក្នុង Code segment) ។ Address របស់ Instru-ction បន្ទាប់ត្រូវបានកំនត់ដោយ CS:IP ។
+ BP: Base pointer ជានិច្ចជាកាលចង្អុរទៅ Data  មួយក្នុង  stack  segment  ដែលមាន address SS:BP (address របស់ Data នេះ)
+ SP: Stack pointer ជា pointer ដែលជានិច្ចកាលចង្អុរទៅរក stack top ក្នុង stack seg-ment ។ address របស់ stack top គឺ SS:SP ។
+ SI: Source Index, SI ចង្អុរទៅទិន្ន័យមួយដែលនៅក្នុង Data Segment ហើយដែលទិន្ន័យនោះមាន address ត្រូវបានកំនត់ដោយ DS:SI ។
+ DI: Destination Index, DI ចង្អុរទៅរកទិន្ន័យមួយដែលនៅក្នុង Data segment ដែរហើយដែល address របស់ Data នោះគឺត្រូវបានកំនត់ដោយ DS:DI ។
- Segment Registers: ការផ្សំគ្នារវាង Segment Registers ជាមួយនិង Register មួយចំនួនទៀតនៅក្នុង mP  នាំ អោយទទួលបាននូវ Address ពិតមួយដោយគណនាតាមរូបមន្ត;
Real addr = Segment x 16 + Offset
ដែលក្នុងនោះ offset គឺជា address ដែលត្រូវបានរក្សាទុកក្នុង  Register  មួយចំនួនដូចជា ; IP, BP, SP, SI, DI ។
+ CS: Code Segment គឺជាផ្នែកមួយនៃ memory ដែលប្រើសំរាប់គ្រប់គ្រង ឬ ផ្ទុក  Codeprograme ឬ code procedure ។   ក្នុង 8088 ដល់ 80286 code segment   ត្រូវបានគេកំនត់ត្រឹម  64K bytes  ។
+ DS: Data segment ជាផ្នែក memory ដែលគេប្រើសំរាប់ផ្ទុកទិន្ន័យដែលត្រូវប្រើសំរាប់កម្មវិធី ។​ (Source Data)
+ ES: Extra segment   ជាតំបន់  memory  មួយដែលត្រូវគេពង្រីកបន្ថែមសំរាប់បំរើដល់
ប្រយោជន៏ជំនួយដល់ DS ក្នុងការរក្សាទុក Destination data ។
+ SS: Stack segment ជាបណ្តុំ memory មួយផ្នែកដែលត្រូវគេប្រើសំរាប់ផ្ទុក Data ឬ ប៉ារ៉ាមែត្ររបស់ Procedure ។ SS ផ្សំជាមួយ SP ឬ BP នាំអោយកំនត់បាន   address   មួយនៅក្នុង stack segment ។
Flags Register ជា Register ពិសេសក្នុង  CPU  bit  និមួយៗរបស់វាត្រូវបានគេ ប្រើសំរាប់បញ្ជាក់បង្ហាញនូវសភាពណា មួយក្នុងតំណើរការណ៏អនុវត្តន៏ប្រមាណវិធីរបស់ ALU ឬ សកម្មភាពរបស់ EU ។ដោយពឹងផ្អែកទៅលើ Flag អស់ទាំងនេះ=> អ្នកសរសេរកម្មវិធី   ( Programer)  អាចឈានទៅបង្កើតជា statement បន្តទៀតអោយ mP ។ Register នេះមាន  16 bit  ក៏ប៉ុន្តែត្រូចគេប្រើអស់តែ 9 bit ប៉ុណ្ណោះដើម្បីធ្វើជា Bit flag ។
X : ជា Bit ដែលពុំទាន់ត្រូវបានគេប្រើប្រាស់ក្នុង 8088/8086
Register នេះមានប្រវែង 32 bit ចាប់ពី 80286 ឡើងទៅ
+ C ឬ CF: Carry Flag , CF=1 បានន័យថាមានត្រាទុកឬបានខ្ចីអំពី HSB ។
+ P ឬ PF: Parity Flag, PF= 1 បានន័យថាចំនួនសរុបរបស់ bit 1 ក្នុងលទ្ធផលប្រមាណវិធីជាចំនួនគូ
+ A ឬ AF: Auxiliary Flag, AF=1 នៅពេលដែលមានត្រាទុកពេលធ្វើវិធីបូកឬខ្ចីពេលធ្វើប្រមាណ
វិធិដករវាង Bit ទី​ 3 និង Bit ទី 4
+ Z ឬ ZF: Zero Flag, ZF=1 ពេលដែលលទ្ធផលប្រមាណវិធីស្មើសូន្យ ។
+ S ឬ SF: Signal Flag, SF=1 ពេលដែលលទ្ធផលប្រមាណវិធីជាតំលៃអវិជ្ជមាន
+ T ឬ TF: Trap Flag: ប្រសិនបើ TF=1=> mP បញ្ឍប់ឬផ្អាកដំនើរការ    Programe  ដើម្បីស្វែងរកកំហុសក្នុង Register ។
+ I ឬ IF: Interrupt enable Flag: ប្រសិនបើ IF=1  នោះ  CPU  អនុញ្ញាត្តិអោយ  Interrupt សកម្មភាព​។ Interrupt មានន័យថា ជាសញ្ញាមួយដែលបញ្ជួនទៅ mP ដើម្បីប្រកាសប្រាប់ថាមានព្រតិ្តការណ៏មួមបានកើតឡើងស្មើ CPU ប្រុងប្រៀប​និង CPU ពេលទទួលបានសញ្ញានេះត្រូចផ្អាកសកម្មភាពកំពុងដំនើរការជា បណ្តោះអាសន្នដើម្បី អនុវត្តន៏ ការងាររបស់ព្រឹត្តិការណ៏ថ្មីនោះ ។ ក្រោយពេលព្រត្តិការណ៏ថ្មីនោះចប់=> CPU បន្តសកម្មភាពចាស់ជាបន្តទៀត​​ ។
+ D ឬ DF: Direction Flag ជា​ Flag មួយប្រើសំរាប់កំនត់ទឹសអោយ DI និង SI ក្នុងខណ:ដែលកំពុងប្រតិ- បត្តិការណ៏សេរី Instruction ។ ប្រសិនបើ DF=0 នោះតំលៃរបស់ DI និង  SI  ត្រូវកើនឡើងជាស្វ័យប្រវត្តិ តែផ្ទុយ
ទៅវិញបើ DF=0=> តំលៃរបស់ DI និង SI ត្រូវថយចុះទៅវិញ ។
+ O ឬ OF: Overflow Flag, ប្រសិនបើ OF=1 មានន័យថាលទ្ធផលរបស់ប្រមាណវិធីបូក 127 បូកនិង 1 ហើយលទ្ធផលត្រូវយកទៅរក្សាទុកក្នុង Memory មាន capacity=8bit ។ ដូច្នេះលទ្ធផលរបស់ផលបូកគឺ 128=> memory ដែលមានទំហំតែ 8 bit ពុំអាចទុកតំលៃនេះបានទេ ពេលនេះ=> OF=1 រីឯbit ទាំង 8 ខាងលើនេះផ្ទុក តំលៃ 0 ទាំងអស់ ។
àà ភាពដូចគ្នានិងខុសគ្នារវាង 8088 និង 8086
8086 ជា​ mP ដែលធ្វើការជាមួយ 16 bit Data ទាំងមូលចំនែក 8088 វិញផ្នែកខាងក្នុង CPU គឺធ្វើការជាមួយ 16 bit data (Bus ក្នុង) ក៏ប៉ុន្តែពេលបញ្ចេញមកខាងក្រៅគឺមានតែ 8 bit ប៉ុណ្ណោះ (Bus ក្រៅ)​ ។ ផ្នែកខាងក្នុងរបស់ 8088 និង 8086 ដូចគ្នាទាំងអស់ខុសត្រង់ Instruction cashe ប៉ុណ្ណោះដែល 8088 មានប្រវែង 4 byte ចំនែក 80-86 មានប្រវែង 6 byte ដែលបញ្ហានេះនាំទៅដល់ការប៉ះពាល់ន្បឿនប្រតិបត្តិការណ៏របស់ CPU ដូច្នេះហើយដែល 8088 មានតំលៃថោកជាង 8086 ។
មេរៀន INTEL 8088 Proccessor
View detail
មេរៀន មូលដ្ឋាន Assembly

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

a
. រចនាសម្ព័ន្ធកម្មវិធី
1.Syntax
- គ្មានបែងចែក Lower case or Upper case
- 1 standard ត្រូវសរសេរលើ 1 បន្ទាត់
- Ful name ដែលគេប្រើសំរាប់ដាក់ឈ្មោះអថេរ Proc , label ត្រូវមានប្រវែងចាប់ពី 1 -> 31 character និង លើកលែងសញ្ញា Underscore => សញ្ញាពិសេសផ្សេងទៀតពុំអាចប្រើបានដូចជា (? , @ , # , $ , % ) ។
-     ប្រើ ; សំរាប់ការអធិប្បាយ ( Comment)
2. Constance and Variable; បណ្តាលេខ ក្នុង Assembly យើងអាចប្រើបានតែប្រព័ន្ធលេខ Binary , Hexa , Decimal ។
Binary;
01101B => ត្រូវ
01012B => ខុស
01101 => ជាលេខ Dec ពុំមែនជា Bin ទេ
លេខ Hexa;
01h => ត្រូវ
9h => ត្រូវ
0ABh => ត្រូវ
0Bh =>ត្រូវ
Fh => ខុស
សំរាប់ Hexa ត្រូវនាំមុខដោយលេខ ( 0…9) ទើបត្រូវ
Decimal
-> យើងប្រើ D ខាងក្រោយតំលៃលេខដើម្បីបញ្ជាក់ថាលេខ Dec ក្នុងករណីដែលពុំ បាន បញ្ជាក់ថា B , H , D តាម ក្រោយនោះ => Complier យល់ថាជាលេខ Decimal ។
** យើងអាចប្រកាស់អថេរដោយប្រភេទផ្សេងៗគ្នាដូចជា;
DB : Byte
DW : Word
DD : Double word ; 4 byte
DO : 4 word =8 byte
DT : 10 byte
ឧទាហរណ៏;   X1  DB ?
=> X1 ជាអថេរប្រភេទ Byte និងពុំទាន់មានផ្ទុកតំលៃចាប់ផ្តើម X2  DW  2
=> X2 ជាអថេរប្រភេទ Word ដែលមានតំលៃចាប់ផ្តើម =2
3- Array ដោយ Assemply មាន 5 ប្រភេទទិន្នន័យ => មាន 5 ប្រភេទ array ដែរ ។ ក្នុង Assembly array គ្រាន់តែ ជាស៊េរីនៃ Byte memory ឬ word memory ។ ឧទាហរណ៏ array 5 byte ដែលមានតំលៃចាប់ផ្តើម 10 ,20 , 30
=> យើងទទួលបាន;
Element   Address   Content
X             0200h      10
X+1          0201h       20
X+2          0202h      30
ឬ       X             X  DB     ?  ?  ?
=> ត្រៀម 3 byte សំរាប់ X  , X +1 , X+2
4- Constance យើងដឹងថា Constance -> តំលៃរបស់វាពុំអាចផ្លាស់ប្តូរបានទេ
ឧទាហរណ៏;   X     EOU   10
ពេលនេះ X ជាចំនួនថេរដែលមានតំលៃ = 10
5- Structure  Assembly program រួមមាន 3 srgment ដូចជា code seg , Data seg , Stack seg និង Segement និមួយៗ ត្រូវបាន Compile ដាក់បញ្ជូលទៅ Segment Memory រៀងខ្លួន ។
+ Mode Memory;
គឺជាការកំនត់ mode memory អោយ compiler;
▪ model ប្រភេទ                អត្ថាធិប្បាយ
​​SMAL                          Code program  1 seg
Data                1 seg
MEDIUM                  Code Program ច្រើនជាង 1 seg
Data               1 seg
COMPACT            Code program 1 seg
Data ច្រើនជាង 1 seg
LAGE                       Code  program ច្រើនជាង 1 seg
Data                 ច្រើនជាង 1 seg
គ្មាន array ណាច្រើនជា  64K byte
HUGE                    Code program ច្រើនជាង 1 seg
Data  ច្រើនជាង 1 seg
Array អាចធំជាង  64 K byte
+ Data Segement
DS របស់ program គឺជាកន្លែងផ្ទុកការប្រកាសអថេរចំនួនថេរ ។
­ STACK Value
មុខងារកំនត់ទំហំ memory សំរាប់ Stack segment ទៅតាមតំលៃ Value ។ Ex; value=100h => 100h byte សំរាប់ STACK
+ Code segment
គឺជាកន្លែងផ្ទុក Code របស់កម្មវិធីទាំងមូល
­ CODE
……
……
នៅក្នុង code Seg => ត្រូវតែមាន main proc ព្រោះដូច C ដែរដែលវាជា Function មួយសំរាប់កំនត់ទីតាំង
ចាប់ផ្តើមតំបូងបំផុតនៃតំណើរការណ៏កម្មវិធី ។
ឧទាហរណ៏;   កម្មវិធីខាងក្រោមមានមុខងារបង្ហាញ “ A” លើ screen ;
­ madel  Small
­ stack  100h
­ Code
main proc
MOV  AH , 02h
MOV  DL , ‘A’
INT  21h
Main endp
END  MAIN
. Input/Out put Data and Controll/ Looping
+ ប្រើ Function 01 របស់DOS សំរាប់បញ្ជូន 1 character
+ ប្រើ Function 02 របស់DOS សំរាប់បង្ហាញ 1 character
+ ប្រើ Function 09 របស់DOS សំរាប់ out put string on screen
ឧទាហរណ៏កម្មវិធីខាងក្រោមសរសេរបញ្ជូលទិន្នន័យ 1 character អោយទៅអថេរ  x  បន្ទាប់មកបង្ហាញ
ទិន្នន័យរបស់ X ដែលទើបទទួលបានលើ Screen;
­ model small
­ stack 100h
­ Data
X  DB ?
Msg 1 DB  Input a value ; $
Msg 2 DB char was ; $
­ Code
mov dx , @ data , ចាប់ផ្តើមបញ្ជូល Code ក្នុង DS ចូល
mov ds , dx        , ទៅ memory ដើម្បី Register memory
LEA dx , msg 1
Int 21h                            បង្ហាញ msg 1 លើ screen
Mov ah , 01h
Mov ah , 01h
Int 21h                            បញ្ជូលទិន្នន័យ 1 char -> AL បន្ទាប់មកផ្ទេរពី AL -> X
Mov X , al
LEA  dx , msge
Mov oh , 09h          បង្ហាញ string msgz
Int 21h
Mov  dl , x
Mov  ah , 02h                   ផ្ទេរ X ចូល AL ដើម្បីអនុវត្តន៏ Func 2 <- DOS សមរាប់បង្ហាញ content , DL លើ
Int 21h                                                                                                                  screen
Mov  Ah , 4ch
Int  21h                           បញ្ឍប់កម្មវិធី
Main endp
End main
à LEA dx , msg មានន័យថាគណនា Real address msg ( ដែល msg កំពុងចង្អុរទៅកាន់ក្បាល address នៃ array ) រួចផ្ទុក address នោះក្នុង dx ដើម្បីអនិវត្តន៏ Functiong របស់ DOS ។
1> Condiction;
ការត្រួតពិនិត្យល/ខគឺធ្វើឡើងដោយ CMP និង Jump
+ IF  THEN  ELSE
ឧទាហរណ៏;ប្រសិនបើ DL=’A’ នោះត្រូវបង្ហាញ DL លើ Screen ផ្ទុយទៅវិញត្រូវបានបង្ហា្ញញលើ Screen នូវ ‘X’
+ CASE
Case (n)
N=1 ; ការងារ 1
N=2 ; ការងារ 2
;
n=m ; ការងារ m
End case
ឧទាហរណ៏:   ប្រសិនបើ AX=0 -> Out put screen “A”
AX<0 -> Out put screen “B”
AX>0 -> Out put screen “C”
, CASE  AX
CMP  AX , 0
JL  NE GATIVE
JE  ZERO
JG  POSITIVE
­ NE GATIVE;
MOV  DL , ‘B’
­ ZERO;
MOV  DL , “A”
­ POSITIVE;
MOV  DL , “C”
MOV  AH , 02h
INT  21h
; ENDCase
+ ការប្រើ ប្រមាណវិធី Logic ក្នុងការគណនាល/ខ
ដូចជា    Exp 1  AND  Exp2

Exp 1   OR  Exp2
­ AND;
ឧទាហរណ៏;   យើងប្រៀបធៀកន្សោមរបស់ល/ខ ខាងក្រោម
IF (‘A’ <= ch) AND (ch<= “2”)  Then
Display  ch
END IF
MOV  AL , ch
:        ; IF(‘A’ <= AL) and (AL <= ‘Z’) Then
CMP  AL , ‘A’
JNGE  AL . ‘Z’
JNLE  END IF
; Then  Display  AL
MOV  DL , AL
MOV  AH , 02h
INT  21h
END-IF;
­ OR;
ឧទាហរណ៏;   យើងប្រៀបធៀបកន្សោមរបស់ល/ខខាងក្រោម
IF (‘A’ <= ch) OR (ch <= ‘Z’) Then
Display  ch
END IF
MOV  AL , ch
; IF (‘A’ <= AL) OR (AL <= ’Z’) Then
CMP  AL , ‘A’
JAE  THEN-
CMP  AL , ‘Z’ ប្រៀបធៀបល/ខ
JBE  THEN-
JMP  ELSE-
THEN;
MOV  AH , 2h
MOV  DL , AL
INT  21h       ការងារក្នុង Then
JMP  END-IF
ELSE;
MOV  AH , 4ch
INT  21h       ឬការងារផ្សេង
END-IF;
………….
…………
2> Loop
+ FOR;
For ចំនួនជុំ DO
Standards
END FOR
Syntax;
MOV  CX , 80 , CX ជា Reg ប្រើសំរាប់ផ្ទុកចំនួនជុំ​=> 80 ជុំ
MOV  AH , 2 , Out put screen
MOV  DL , ‘#’ , Char for out putting
TOP;
INT  21h ,
LOOP  TOP
Loop TOP នេះមានតួនាទីលោតទៅរក Label ដែលនៅផ្នែកខាងលើ ។ ពេលលោតទៅ TOP វាថែមទាំង អនិវត្តន៏ការងារ DEC CX ដោយស្វ័យប្រវត្តិរហូតដល់ពេលដែល CX ផ្ទុកតំលៃ 0 ទើប Loop ឈប់លោតទៅរក TOP និង អនិវត្តន៏បន្ត Statement បន្ទាប់ទៀត​។
ក្នុងករណ៏ CX ផ្ទុក 0 -> ពេលជួប Loop នោះ CX ត្រូវបានដក 1 ទៀត => CX ផ្ទុក  FFFFh  ដែលវានឹងត្រូវ Loop​ចំនួន 65535 ជុំទៀត ។
a
. រចនាសម្ព័ន្ធកម្មវិធី
1.Syntax
- គ្មានបែងចែក Lower case or Upper case
- 1 standard ត្រូវសរសេរលើ 1 បន្ទាត់
- Ful name ដែលគេប្រើសំរាប់ដាក់ឈ្មោះអថេរ Proc , label ត្រូវមានប្រវែងចាប់ពី 1 -> 31 character និង លើកលែងសញ្ញា Underscore => សញ្ញាពិសេសផ្សេងទៀតពុំអាចប្រើបានដូចជា (? , @ , # , $ , % ) ។
-     ប្រើ ; សំរាប់ការអធិប្បាយ ( Comment)
2. Constance and Variable; បណ្តាលេខ ក្នុង Assembly យើងអាចប្រើបានតែប្រព័ន្ធលេខ Binary , Hexa , Decimal ។
Binary;
01101B => ត្រូវ
01012B => ខុស
01101 => ជាលេខ Dec ពុំមែនជា Bin ទេ
លេខ Hexa;
01h => ត្រូវ
9h => ត្រូវ
0ABh => ត្រូវ
0Bh =>ត្រូវ
Fh => ខុស
សំរាប់ Hexa ត្រូវនាំមុខដោយលេខ ( 0…9) ទើបត្រូវ
Decimal
-> យើងប្រើ D ខាងក្រោយតំលៃលេខដើម្បីបញ្ជាក់ថាលេខ Dec ក្នុងករណីដែលពុំ បាន បញ្ជាក់ថា B , H , D តាម ក្រោយនោះ => Complier យល់ថាជាលេខ Decimal ។
** យើងអាចប្រកាស់អថេរដោយប្រភេទផ្សេងៗគ្នាដូចជា;
DB : Byte
DW : Word
DD : Double word ; 4 byte
DO : 4 word =8 byte
DT : 10 byte
ឧទាហរណ៏;   X1  DB ?
=> X1 ជាអថេរប្រភេទ Byte និងពុំទាន់មានផ្ទុកតំលៃចាប់ផ្តើម X2  DW  2
=> X2 ជាអថេរប្រភេទ Word ដែលមានតំលៃចាប់ផ្តើម =2
3- Array ដោយ Assemply មាន 5 ប្រភេទទិន្នន័យ => មាន 5 ប្រភេទ array ដែរ ។ ក្នុង Assembly array គ្រាន់តែ ជាស៊េរីនៃ Byte memory ឬ word memory ។ ឧទាហរណ៏ array 5 byte ដែលមានតំលៃចាប់ផ្តើម 10 ,20 , 30
=> យើងទទួលបាន;
Element   Address   Content
X             0200h      10
X+1          0201h       20
X+2          0202h      30
ឬ       X             X  DB     ?  ?  ?
=> ត្រៀម 3 byte សំរាប់ X  , X +1 , X+2
4- Constance យើងដឹងថា Constance -> តំលៃរបស់វាពុំអាចផ្លាស់ប្តូរបានទេ
ឧទាហរណ៏;   X     EOU   10
ពេលនេះ X ជាចំនួនថេរដែលមានតំលៃ = 10
5- Structure  Assembly program រួមមាន 3 srgment ដូចជា code seg , Data seg , Stack seg និង Segement និមួយៗ ត្រូវបាន Compile ដាក់បញ្ជូលទៅ Segment Memory រៀងខ្លួន ។
+ Mode Memory;
គឺជាការកំនត់ mode memory អោយ compiler;
▪ model ប្រភេទ                អត្ថាធិប្បាយ
​​SMAL                          Code program  1 seg
Data                1 seg
MEDIUM                  Code Program ច្រើនជាង 1 seg
Data               1 seg
COMPACT            Code program 1 seg
Data ច្រើនជាង 1 seg
LAGE                       Code  program ច្រើនជាង 1 seg
Data                 ច្រើនជាង 1 seg
គ្មាន array ណាច្រើនជា  64K byte
HUGE                    Code program ច្រើនជាង 1 seg
Data  ច្រើនជាង 1 seg
Array អាចធំជាង  64 K byte
+ Data Segement
DS របស់ program គឺជាកន្លែងផ្ទុកការប្រកាសអថេរចំនួនថេរ ។
­ STACK Value
មុខងារកំនត់ទំហំ memory សំរាប់ Stack segment ទៅតាមតំលៃ Value ។ Ex; value=100h => 100h byte សំរាប់ STACK
+ Code segment
គឺជាកន្លែងផ្ទុក Code របស់កម្មវិធីទាំងមូល
­ CODE
……
……
នៅក្នុង code Seg => ត្រូវតែមាន main proc ព្រោះដូច C ដែរដែលវាជា Function មួយសំរាប់កំនត់ទីតាំង
ចាប់ផ្តើមតំបូងបំផុតនៃតំណើរការណ៏កម្មវិធី ។
ឧទាហរណ៏;   កម្មវិធីខាងក្រោមមានមុខងារបង្ហាញ “ A” លើ screen ;
­ madel  Small
­ stack  100h
­ Code
main proc
MOV  AH , 02h
MOV  DL , ‘A’
INT  21h
Main endp
END  MAIN
. Input/Out put Data and Controll/ Looping
+ ប្រើ Function 01 របស់DOS សំរាប់បញ្ជូន 1 character
+ ប្រើ Function 02 របស់DOS សំរាប់បង្ហាញ 1 character
+ ប្រើ Function 09 របស់DOS សំរាប់ out put string on screen
ឧទាហរណ៏កម្មវិធីខាងក្រោមសរសេរបញ្ជូលទិន្នន័យ 1 character អោយទៅអថេរ  x  បន្ទាប់មកបង្ហាញ
ទិន្នន័យរបស់ X ដែលទើបទទួលបានលើ Screen;
­ model small
­ stack 100h
­ Data
X  DB ?
Msg 1 DB  Input a value ; $
Msg 2 DB char was ; $
­ Code
mov dx , @ data , ចាប់ផ្តើមបញ្ជូល Code ក្នុង DS ចូល
mov ds , dx        , ទៅ memory ដើម្បី Register memory
LEA dx , msg 1
Int 21h                            បង្ហាញ msg 1 លើ screen
Mov ah , 01h
Mov ah , 01h
Int 21h                            បញ្ជូលទិន្នន័យ 1 char -> AL បន្ទាប់មកផ្ទេរពី AL -> X
Mov X , al
LEA  dx , msge
Mov oh , 09h          បង្ហាញ string msgz
Int 21h
Mov  dl , x
Mov  ah , 02h                   ផ្ទេរ X ចូល AL ដើម្បីអនុវត្តន៏ Func 2 <- DOS សមរាប់បង្ហាញ content , DL លើ
Int 21h                                                                                                                  screen
Mov  Ah , 4ch
Int  21h                           បញ្ឍប់កម្មវិធី
Main endp
End main
à LEA dx , msg មានន័យថាគណនា Real address msg ( ដែល msg កំពុងចង្អុរទៅកាន់ក្បាល address នៃ array ) រួចផ្ទុក address នោះក្នុង dx ដើម្បីអនិវត្តន៏ Functiong របស់ DOS ។
1> Condiction;
ការត្រួតពិនិត្យល/ខគឺធ្វើឡើងដោយ CMP និង Jump
+ IF  THEN  ELSE
ឧទាហរណ៏;ប្រសិនបើ DL=’A’ នោះត្រូវបង្ហាញ DL លើ Screen ផ្ទុយទៅវិញត្រូវបានបង្ហា្ញញលើ Screen នូវ ‘X’
+ CASE
Case (n)
N=1 ; ការងារ 1
N=2 ; ការងារ 2
;
n=m ; ការងារ m
End case
ឧទាហរណ៏:   ប្រសិនបើ AX=0 -> Out put screen “A”
AX<0 -> Out put screen “B”
AX>0 -> Out put screen “C”
, CASE  AX
CMP  AX , 0
JL  NE GATIVE
JE  ZERO
JG  POSITIVE
­ NE GATIVE;
MOV  DL , ‘B’
­ ZERO;
MOV  DL , “A”
­ POSITIVE;
MOV  DL , “C”
MOV  AH , 02h
INT  21h
; ENDCase
+ ការប្រើ ប្រមាណវិធី Logic ក្នុងការគណនាល/ខ
ដូចជា    Exp 1  AND  Exp2

Exp 1   OR  Exp2
­ AND;
ឧទាហរណ៏;   យើងប្រៀបធៀកន្សោមរបស់ល/ខ ខាងក្រោម
IF (‘A’ <= ch) AND (ch<= “2”)  Then
Display  ch
END IF
MOV  AL , ch
:        ; IF(‘A’ <= AL) and (AL <= ‘Z’) Then
CMP  AL , ‘A’
JNGE  AL . ‘Z’
JNLE  END IF
; Then  Display  AL
MOV  DL , AL
MOV  AH , 02h
INT  21h
END-IF;
­ OR;
ឧទាហរណ៏;   យើងប្រៀបធៀបកន្សោមរបស់ល/ខខាងក្រោម
IF (‘A’ <= ch) OR (ch <= ‘Z’) Then
Display  ch
END IF
MOV  AL , ch
; IF (‘A’ <= AL) OR (AL <= ’Z’) Then
CMP  AL , ‘A’
JAE  THEN-
CMP  AL , ‘Z’ ប្រៀបធៀបល/ខ
JBE  THEN-
JMP  ELSE-
THEN;
MOV  AH , 2h
MOV  DL , AL
INT  21h       ការងារក្នុង Then
JMP  END-IF
ELSE;
MOV  AH , 4ch
INT  21h       ឬការងារផ្សេង
END-IF;
………….
…………
2> Loop
+ FOR;
For ចំនួនជុំ DO
Standards
END FOR
Syntax;
MOV  CX , 80 , CX ជា Reg ប្រើសំរាប់ផ្ទុកចំនួនជុំ​=> 80 ជុំ
MOV  AH , 2 , Out put screen
MOV  DL , ‘#’ , Char for out putting
TOP;
INT  21h ,
LOOP  TOP
Loop TOP នេះមានតួនាទីលោតទៅរក Label ដែលនៅផ្នែកខាងលើ ។ ពេលលោតទៅ TOP វាថែមទាំង អនិវត្តន៏ការងារ DEC CX ដោយស្វ័យប្រវត្តិរហូតដល់ពេលដែល CX ផ្ទុកតំលៃ 0 ទើប Loop ឈប់លោតទៅរក TOP និង អនិវត្តន៏បន្ត Statement បន្ទាប់ទៀត​។
ក្នុងករណ៏ CX ផ្ទុក 0 -> ពេលជួប Loop នោះ CX ត្រូវបានដក 1 ទៀត => CX ផ្ទុក  FFFFh  ដែលវានឹងត្រូវ Loop​ចំនួន 65535 ជុំទៀត ។
មេរៀន មូលដ្ឋាន Assembly
View detail
 
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