មេរៀនកម្មវិធី Syntax Assembly (ត)
៧- មេរៀន Arithmetic InstructionsINC 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 registerINC [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_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
លទ្ធផល3
Enter a digit:
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
លទ្ធផល7
The sum is:
MUL/IMUL Instruction
syntax សម្រាប់ instructions:
MUL/IMUL multiplier
ឧទាហរណ៍MOV DL, 25
MOV AL, 10
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
លទ្ធផល6
The result is:
DIV/IDIV Instructions
ទម្រង់នៃ DIV/IDIV instruction:
DIV/IDIV divisor
ឧទាហរណ៍global main ;must be declared for using gcc
section .text
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
លទ្ធផល4
The result is:
៨-មេរៀន Logical Instructions
ទម្រង់ instructions ទាំងនេះ
AND operand1, operand2
OR operand1, operand2
XOR operand1, operand2
TEST operand1, operand2
NOT operand1
The AND Instruction
ឧទាហរណ៍Operand2: 0011
Operand1: 0101
—————————-
After AND -> Operand1: 0001
Assuming the number is in AL register, we can write:
AND AL, 01H ; ANDing with 0000 0001
JZ EVEN_NUMBER
ឧទាហរណ៍global main ;must be declared for using gcc
section .text
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
លទ្ធផលChange the value in the ax register with an odd digit, like:
Even Number!
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
ឧទារហណ៍global main ;must be declared for using gcc
section .text
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
ឧទាហរណ៍JE L7 ; If yes, then jump to label L7
CMP DX, 00 ; Compare the DX value with zero
.
.
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/JZ | Jump Equal or Jump Zero | ZF |
JNE/JNZ | Jump not Equal or Jump Not Zero | ZF |
JG/JNLE | Jump Greater or Jump Not Less/Equal | OF, SF, ZF |
JGE/JNL | Jump Greater or Jump Not Less | OF, SF |
JL/JNGE | Jump Less or Jump Not Greater/Equal | OF, SF |
JLE/JNG | Jump Less/Equal or Jump Not Greater | OF, SF, ZF |
Instruction | Description | Flags tested |
JE/JZ | Jump Equal or Jump Zero | ZF |
JNE/JNZ | Jump not Equal or Jump Not Zero | ZF |
JA/JNBE | Jump Above or Jump Not Below/Equal | CF, ZF |
JAE/JNB | Jump Above/Equal or Jump Not Below | CF |
JB/JNAE | Jump Below or Jump Not Above/Equal | CF |
JBE/JNA | Jump Below/Equal or Jump Not Above | AF, CF |
ឧទាហរណ៍
CMP AL, BL
JE EQUAL
CMP AL, BH
JE EQUAL
CMP AL, CL
JE EQUAL
NON_EQUAL: …
EQUAL: …
ឧទាហរណ៍global main ;must be declared for using gcc
section .text
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
លទ្ធផល47
The largest digit is:
១០ - មេរៀន 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
លទ្ធផលមេរៀន Numbers
123456789
កូដបង្ហាញដូចខាងក្រោម:
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
លទ្ធផល7
The sum is:
តំណាង BCD Representation
មានពីរប្រភេទនៃតំណាង BCD:
- Unpacked BCD representation
- Packed BCD representation
01 02 03 04HThere are two instructions for processing these numbers:
- AAM – ASCII Adjust After Multiplication
- AAD – ASCII Adjust Before Division
ឧទាហរណ៍global main ;must be declared for using gcc
section .text
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 ‘ ‘
លទ្ធផល35801
The Sum is:
- មេរៀ Explicitly storing string length
- Using a sentinel character
- Explicitly storing string length
- Using a sentinel character
msg db 'Hello, world!',0xa ;our dear stringlen 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
ឧទាហរណ៍global main ;must be declared for using gcc
section .text
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
លទ្ធផលThe STOS Instruction
Hello, world!
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, worldThe 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
លទ្ធផល១១-មេរៀន Arrays
found!
យើងអាចអោយនិយមន័យមួយពាក្ស 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
ឧទាហរណ៍global main ;must be declared for linker (ld)
section .text
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
លទ្ធផល9
The sum is:
១៣-មេរៀន រង្វិលជុំ 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
លទ្ធផល6
Factorial 3 is:
១៤-មេរៀន 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
លទ្ធផលWelcome to the world of,
Hello, programmers!
Linux assembly programming!
១៥-មេរៀន ការគ្រប់គ្រង Memory
មានពីប្រព័ន្ធ call ក្នុង Linux ដែលអនុញ្ញាត្តប្រ dynamic memory allocation:
- sys_mmap()
- sys_brk()
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!
Post a Comment