In assembly language, data representation and conversion are critical concepts that deal with how data is stored, manipulated, and transformed from one format to another. Different types of data are represented in the computer system in various ways depending on the system architecture, the instruction set, and the nature of the data itself. These representations are used to store integers, floating-point numbers, characters, strings, and other complex data types.
Understanding how data is represented and how conversions between different representations work is crucial for writing efficient assembly code and for ensuring that data is processed correctly.
At the lowest level, all data in a computer is represented in binary (base 2), meaning that everything is stored as sequences of 0s and 1s. The binary system is the foundation of all data representation in computers.
0 or 1.For example:
Signed Integer: In the binary system, signed integers are typically represented using two's complement notation, which allows for negative numbers.
0xF7 (which is -9 in decimal using two's complement representation).Unsigned Integer: For unsigned integers, all bits represent the magnitude of the number, ranging from 0 to 2^n - 1, where n is the number of bits.
Floating-point numbers are typically represented using the IEEE 754 standard, which divides the number into:
This allows for representing very large or very small numbers.
ASCII (American Standard Code for Information Interchange) is a standard that represents characters as 7 or 8-bit values. For example:
A is 0x41 (or 01000001 in binary).a is 0x61 (or 01100001 in binary).Unicode: For non-English characters and symbols, Unicode provides a larger set of values to represent characters, often using multiple bytes (16 bits or more per character).
Data conversion involves transforming data from one representation to another. This process is necessary when interacting with different formats or when performing operations on data types that require specific representations.
Integer to Binary Conversion: In assembly, integers are typically stored in binary form. When converting an integer to binary, you simply express the number as a series of 0s and 1s.
Example: Decimal 13 is represented as binary 1101.
Binary to Integer Conversion: To convert binary back to decimal, you sum the values of the bits that are set (i.e., the 1 bits) based on their place value.
Example: 1101 in binary = 1*(2^3) + 1*(2^2) + 0*(2^1) + 1*(2^0) = 8 + 4 + 0 + 1 = 13 in decimal.
Decimal numbers are converted to binary using a method of successive division by 2.
Decimal to Binary (Unsigned Integer):
Example: Convert decimal 19 to binary:
So, 19 in binary is 10011.
Binary to Decimal: To convert binary to decimal, multiply each bit by the power of 2 corresponding to its position, then sum the results.
Example: Convert binary 10011 to decimal:
Hexadecimal (base 16) is another common representation used in assembly because it is more compact than binary. It is also easier for humans to read. Each hexadecimal digit represents exactly 4 binary digits (bits).
Decimal to Hexadecimal: Divide the decimal number by 16, noting the quotient and remainder. The remainder gives you the hexadecimal digits.
Example: Convert decimal 255 to hexadecimal:
F in hexadecimal.F in hexadecimal.So, 255 in hexadecimal is FF.
Hexadecimal to Decimal: Multiply each hexadecimal digit by the corresponding power of 16.
Example: Convert FF in hexadecimal to decimal:
When converting between signed and unsigned integers, you need to consider whether the number uses two's complement representation (signed) or just the magnitude of the number (unsigned).
Signed to Unsigned: When converting a signed number to unsigned, you need to ensure that the sign bit is properly interpreted. For instance, if a 32-bit signed integer 0xFFFFFFFF (which is -1 in two's complement) is treated as an unsigned integer, it becomes 0xFFFFFFFF, which is 4,294,967,295 in decimal.
Unsigned to Signed: When converting an unsigned number to a signed one, you need to check if the value exceeds the maximum value for the signed type (e.g., 0x80000000 in a 32-bit signed integer would be considered negative).
Signed Integers: Typically represented using two's complement in modern systems, which allows negative values to be represented using the most significant bit (MSB) as a sign bit.
Unsigned Integers: Represented directly with all bits used for magnitude.
Floating Point Numbers: Use the IEEE 754 standard, which breaks the number into a sign, exponent, and mantissa.
Characters: Stored using ASCII (or Unicode) codes, where each character corresponds to a unique numerical value.
MASM provides a number of tools and instructions that allow you to convert between different data formats.
For example, converting between decimal and binary numbers in MASM can be done using the DEC and BIN directives. These can be used in conjunction with printf-like functions to display numbers in different bases.
mov ax, 10 ; Store decimal number
mov bx, 0x0A ; Store hexadecimal number
mov cx, 100 ; Store binary number
Example of Converting Integer to String in MASM:
.data
num db 10 ; decimal 10
str db 6 dup(0)
.code
mov ax, num ; Load number into ax
call itoa ; Convert integer to ASCII string
In this example, itoa (integer to ASCII) is a function that converts an integer to its string representation.
Understanding data representation and conversion is key for working
Open this section to load past papers