Asm Lec#5
Asm Lec#5
CHAPTER
Assembly Variables
N ASM provides various define directives for reserving storage space for variables. The define
assembler directive is used for allocation of storage space. It can be used to reserve as well as initialize one or
more bytes.
Where, variable-name is the identifier for each storage space. The assembler associates an offset value for each
variable name defined in the data segment.
There are five basic forms of the define directive:
choice DB 'y'
number DW 12345
neg_number DW -12345
big_number DQ 123456789
real_number1 DD 1.234
real_number2 DQ 123.456
Each decimal value is automatically converted to its 16-bit binary equivalent and stored as a hexadecimal
number
TUTORIALS POINT
Simply Easy Learning
Processor uses the little-endian byte ordering
Short and long floating-point numbers are represented using 32 or 64 bits, respectively
section .text
global main ;must be declared for linker (gcc)
main: ;tell linker entry point
section .data
choice DB 'y'
When the above code is compiled and executed, it produces following result:
Directive Purpose
Multiple Definitions
You can have multiple data definition statements in a program. For example:
TUTORIALS POINT
Simply Easy Learning
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. 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 0x80 ;call kernel
section .data
stars times 9 db '*'
When the above code is compiled and executed, it produces following result:
*********
TUTORIALS POINT
Simply Easy Learning
9
CHAPTER
Assembly Constants
T here are several directives provided by NASM that define constants. We have already used the EQU
EQU
%assign
%define
For example,
TOTAL_STUDENTS equ 50
You can then use this constant value in your code, like:
LENGTH equ 20
WIDTH equ 10
AREA equ length * width
Example:
The following example illustrates the use of the EQU directive:
SYS_EXIT equ 1
SYS_WRITE equ 4
TUTORIALS POINT
Simply Easy Learning
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 0x80
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
When the above code is compiled and executed, it produces following result:
Hello, programmers!
Welcome to the world of,
Linux assembly programming!
%assign TOTAL 10
%assign TOTAL 20
TUTORIALS POINT
Simply Easy Learning
The above code replaces PTR by [EBP+4].
TUTORIALS POINT
Simply Easy Learning