Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

assembly

What are constants in assembly language?

Rukhshan Haroon

In assembly language, different directives can be used to declare constants. The directive to be used depends on the data-type of the constant.

The three most common types of directives used to declare constants in assembly language are:

  • equ
  • %assign
  • %define

The equ directive

The equ directive is used to declare numerical constants. The value of constants declared using the equ directive can not be changed later in code.

The following snippet of code demonstrates how to use the equ directive to save the length of a string named message in the data segment of the program.

message_length equ $-message

The %assign directive

The %assign directive is used to numeric constants as well. However, the value of these constants may be changed later in the code.

To define a constant using %assign, use the following syntax:

%assign bill 200

The %define directive

The %define directive is used to declare numeric as well as string constants. Constants declared by using this directive may be changed later in code.

The following syntax is used to define a constant using the %define directive:

%define dollar "$"

Example

Declaring constants is helpful when we have to use a single value over and over again. The following snippet of code stores 0x4-the system call number of the write system call- as a constant and assigns write_sys as its identifier. In this way, whenever I have to call the write system call, I can store ‘‘write_sys’’ in eax instead of ‘‘0x4’’. This makes the code more understandable.

On the other hand, it demonstrates how the equ and %define directives may be used in the data segment to declare numeric and text constants.

write_sys equ 0x4

segment .data:
	%define message0 "!"								;defines a constant string

	message1 db "Welcome to Edpresso", message0, 0xA	;defines and concatenates a string, the constant and 0XA which is \n in hex
	message1_length equ $-message1

	message2 db "This is Educative", message0, 0xA		;;defines and concatenates another string, the constant and 0XA which is \n in hex
	message2_length equ $-message2
segment .bss:

segment .text:
	global _start
_start:
  	mov eax, write_sys			;4 is the unique system call number of the write system call
	mov ebx, 1					;1 is the file descriptor for the stdout stream
	mov ecx, message1			;message is the string that needs to be printed on the console
	mov edx, message1_length	;length of message
	int 0x80					;interrupt to run the kernel
	
	mov eax, write_sys			;repeats the above process to
	mov ebx, 1					;print the second message
	mov ecx, message2
	mov edx, message2_length
	int 0x80

	mov eax, 0x1				;1 is the unique system call number of the exit system call
	mov ebx, 0					;argument to exit system call
	int 0x80					;interrupt to run the kernel and exit gracefully

RELATED TAGS

assembly

CONTRIBUTOR

Rukhshan Haroon
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring