In assembly language, integer constants refer to fixed numeric values that are used directly in the program. These constants can be expressed in different number systems, and understanding how to define and use them is crucial for working with data in assembly programs. Integer constants are used to perform arithmetic operations, store values, control program flow, and interact with hardware.
Integer constants in assembly language can be written in several numeric bases (radix systems). The most common bases are:
Let's look at how integer constants are represented in these number systems in assembly language.
Decimal integers are the most straightforward and are written as a sequence of digits without any special prefix. These numbers are understood as base 10 by default.
Example:
MOV AX, 123 ; Load the decimal value 123 into the AX register
Explanation:
In this example, 123 is a decimal integer constant, and the instruction moves it into the AX register.
Hexadecimal (or hex) is often used in assembly language because it provides a compact way of representing binary values. In hex, digits range from 0-9 and A-F, where A-F represent the values 10-15.
Prefix for Hexadecimal: 0x or sometimes just a $ sign is used in different assemblers. In MASM (Microsoft Macro Assembler), the 0x prefix is commonly used to represent hexadecimal values.
Example:
MOV AX, 0x1F4 ; Load the hexadecimal value 1F4 (decimal 500) into AX
Explanation:
0x1F4 is a hexadecimal constant. Hex 1F4 is equal to the decimal value 500 (since 116² + 1516¹ + 4*16⁰ = 500).
Binary is the base of machine-level operations. While it can be tedious to work directly with binary numbers, some assemblers allow you to define binary constants.
Prefix for Binary: MASM and other assemblers often use the b suffix or 0b prefix to indicate binary constants.
Example:
MOV AX, 0b101010 ; Load the binary value 101010 (decimal 42) into AX
Explanation:
0b101010 is a binary constant. Binary 101010 is equal to the decimal value 42 (since 12⁵ + 02⁴ + 12³ + 02² + 12¹ + 02⁰ = 42).
Octal numbers are less commonly used today but were historically useful in computer systems. Octal constants use digits 0-7.
Prefix for Octal: Some assemblers use 0 to denote octal constants (in older systems, octal constants were typically prefixed by a leading 0).
Example:
MOV AX, 0750 ; Load the octal value 750 (decimal 490) into AX
Explanation:
0750 is an octal constant. In octal, 750 is equal to the decimal value 490 (since 78² + 58¹ + 0*8⁰ = 490).
In MASM (Microsoft Macro Assembler), the following conventions are typically used to specify the numeric base for constants:
123).0x prefix or the h suffix (e.g., 0x1F4 or 1F4h).b suffix (e.g., 101010b).0 prefix (e.g., 0750).Examples:
.data
dec_val DB 123 ; Decimal integer constant
hex_val DB 0x1F4 ; Hexadecimal integer constant (0x1F4 is 500 in decimal)
bin_val DB 101010b ; Binary integer constant (101010b is 42 in decimal)
oct_val DB 0750 ; Octal integer constant (0750 is 490 in decimal)
In assembly language, constants are often stored in data segments and used throughout the program. In MASM, you can define constants using assembler directives like .data and .equ:
.equ to Define Constants:The .equ directive allows you to define symbolic names for constants, making your code more readable and easier to maintain.
MAX_SIZE EQU 1024 ; Define MAX_SIZE as the constant value 1024
BUFFER_SIZE EQU 0x200 ; Define BUFFER_SIZE as a constant hexadecimal value (512 in decimal)
.data
buffer DB MAX_SIZE ; Reserve space for a buffer of size 1024
In this example:
MAX_SIZE is a constant with a value of 1024.BUFFER_SIZE is a constant defined in hexadecimal (0x200), which is equivalent to 512 in decimal..data segment reserves space for the buffer of size MAX_SIZE.Integer constants are used in arithmetic operations, control flow decisions, and for addressing memory. For example, you might use constants for indexing an array, comparing values, or performing arithmetic calculations.
MOV AX, 50 ; Load the constant 50 into AX
ADD AX, 25 ; Add the constant 25 to AX, result will be 75
MOV BX, 0x1000 ; Load the hexadecimal constant 0x1000 (4096 in decimal) into BX
MOV AX, [BX] ; Access the value stored at memory address 0x1000 and move it into AX
Integer constants in assembly language are crucial for specifying values that are directly used in your program’s logic and operations. These constants can be written in different number systems, such as decimal, hexadecimal, binary, and octal, each having its specific use case. MASM and other assemblers support flexible syntax for representing constants, allowing for clear and efficient assembly code.
By understanding how to use integer constants in various formats, you can better manage data, perform arithmetic, and address memory in your assembly programs.
Open this section to load past papers