Lecturenote 683574061Chapter Two
Lecturenote 683574061Chapter Two
Directive Purpose
RESB Reserve a Byte
RESW Reserve a Word
RESD Reserve a Doubleword
RESQ Reserve a Quadword
REST Reserve a Ten Bytes
• Multiple Definitions
– You can have multiple data definition statements in a program. For
example:
• choice DB 'Y' ;ASCII of y = 79H
• number1 DW 12345 ;12345D = 3039H
• number2 DD 12345679 ;123456789D = 75BCD15H
• The assembler allocates contiguous memory for multiple
variable definitions.
• Multiple Initializations
• The TIMES directive allows multiple initializations to the same
value.
• For example, an array named marks of size 9 can be defined
and initialized to zero using the following statement:
– marks TIMES 9 DW 0
• The TIMES directive is useful in defining arrays and tables.
Compiled by Yonas A. and Hailemariam M.
27
[2010]
CONSTANTS
• There are several directives provided by NASM that define constants. Some of them
are :
– EQU
– %assign
– %define
• The EQU Directive
– The EQU directive is used for defining constants.
– The syntax of the EQU directive is as follows:
• CONSTANT_NAME EQU expression
– For example, TOTAL_STUDENTS equ 50
– You can then use this constant value in your code, like:
• mov ecx, TOTAL_STUDENTS
• cmp eax, TOTAL_STUDENTS
• The operand of an EQU statement can be an expression:
• LENGTH equ 20
• WIDTH equ 10
• AREA equ length * width Above code segment would define AREA as 200.
CONSTANTS
• The %assign Directive
– The %assign directive can be used to define numeric constants like the EQU
directive. This directive allows redefinition.
– For example, you may define the constant TOTAL as:
• %assign TOTAL 10
• Later in the code, you can redefine it as:
• %assign TOTAL 20
– This directive is case-sensitive.
• The %define Directive
– The %define directive allows defining both numeric and string constants. This
directive is similar to the #define in C.
– For example, you may define the constant PTR as:
• %define PTR [EBP+4]
– The above code replaces PTR by [EBP+4].
– This directive also allows redefinition and it is case-sensitive.
29
Linux System Calls
• System calls are APIs for the interface between the user space and the
kernel space.
• You can make use of Linux system calls in your assembly programs. You
need to take the following steps for using Linux system calls in your
program:
– Put the system call number in the EAX register.
– Store the arguments to the system call in the registers EBX, ECX, etc.
– Call the relevant interrupt (80h).
– The result is usually returned in the EAX register.
• There are six registers that store the arguments of the system call used.
These are the EBX, ECX, EDX, ESI, EDI, and EBP. These registers take the
consecutive arguments, starting with the EBX register.
• The following code snippet shows the use of the system call sys_exit:
– mov eax,1; system call number (sys_exit)
Compiled by Yonas A. and Hailemariam M.
– int 0x80 ; call kernel [2010]
30
Linux System Calls
here are some of system calls commonly used
1. section.data
2. msg db 'Hello World!', 0Ah; assign msg variable with your message
string
3. SECTION .text
4. global _start
5. _start:
6. mov edx, 13 ; number of bytes to write - one for each letter plus 0Ah
(line feed character)
7. mov ecx, msg ; move the memory address of our message string into
ecx
8. mov ebx, 1 ; write to the STDOUT file
9. mov eax, 4 ; invoke SYS_WRITE (kernel opcode 4)
10. int 80h ; intrrupt Compiled by Yonas A. and
Hailemariam M. [2010]
33
Some practical Examples
section .data
SYS_EXIT equ 1
SYS_READ equ 3
SYS_WRITE equ 4
STDIN equ 0
STDOUT equ 1
msg1 db "Enter Your Name", 0xA,0xD
len1 equ $- msg1
msg2 db "Hello! "
len2 equ $- msg2
segment .bss
name resb 20;reserving a memory for name
Compiled by Yonas A. and
34
Hailemariam M. [2010]
Some practical Examples
section .text
global _start ;must be declared for using gcc
_start: ;tell linker entry point
;prompting for message 1
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg1
mov edx, len1
int 0x80
;reading name
mov eax, SYS_READ
mov ebx, STDIN
mov ecx, name
mov edx, 20
int 0x80
Compiled by Yonas A. and
35
Hailemariam M. [2010]
Some practical Examples
;displaying the message
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, msg2
mov edx, len2
int 0x80
; print the Message
mov eax, SYS_WRITE
mov ebx, STDOUT
mov ecx, name
mov edx, 20
int 0x80
;exit:
mov eax, SYS_EXIT
Compiled by Yonas A. and
int 0x80 Hailemariam M. [2010]
36
Some practical Examples
mov ecx,sum ;
mov edx,10 ; size of the sum
mov ebx,1 ; stdout
mov eax,4 ;sys_write
int 0x80
section .data
msg db "The sum is:",0xA,0xD
len equ $-msg
section .bss
sum resb 10
Compiled by Yonas A. and
41
Hailemariam M. [2010]